Laravel 5 Eloquent Model error

0

Hello Stackoverflow community, I have this error while I try to use query between models and relationships in Laravel 5.3:

BadMethodCallException in Builder.php line 2450: Call to undefined method Illuminate \ Database \ Query \ Builder :: adminis ()

These are my models:

  

One to Many Relationship with respect to the second.

    <?php

    namespace App\Modelos;

    use Illuminate\Database\Eloquent\Model;

    class srh_inf_persona extends Model
    {
        protected $table = 'srh_inf_personas';

        protected $primaryKey = ['infp_cedula'];

        public function adminis()
        {
            return $this->hasMany('App\Modelos\srh_infa_admini');
        }
    }
  

And this the other model in inverse relationship from One to Many.

    <?php

    class srh_inf_admini extends Model
{
    protected $table = 'srh_inf_adminis';
    public $timestamps = 'false';

    protected $primaryKey = ['infa_cedula'];

    public function persona()
    {
        return $this->belongsTo('App\Modelos\srh_inf_persona');
    }
}
  

This is the driver where I ask:

    <?php

namespace App\Http\Controllers\Controladores;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Modelos\srh_inf_persona; // Llamado del modelo SRH_INF_PERSONA
use App\Modelos\srh_inf_admini; // Llamado del modelo SRH_INF_PERSONA

class SolicitudController extends Controller
{
    public function nuevasolicitud($ced)
    {       
        $consulta = srh_inf_persona::select('infp_cedula')->adminis()
        ->where('infp_cedula',$ced)
        ->first();

        return view('permisos.solicitud',[ 'personas' => $consulta ]);
    }

}

What could it be? I do not know if it's something in relationships or it's a bad consultation. Thank you very much.

    
asked by Daniel 06.04.2017 в 22:11
source

1 answer

0

This is incorrect what you want to do. I think what you want to do is use a scope query. What you need to do to make it work is to add a method in the "srh_inf_person" class in the following way.

public function scopeAdminis($query)
{
    // Restricciones que quieres agregar
    return $query;
}

Adding this method there should be no error.

Or if what you want to do is load the relationship, you have to do it with the method with

$consulta = srh_inf_persona::select('infp_cedula')
        ->with('adminis')
        ->where('infp_cedula',$ced)
        ->first();
    
answered by 06.04.2017 в 23:23