简体   繁体   中英

Laravel 5 Eloquent model error

im having this error with Eloquent models while trying to make multiple model joins:

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

These are my models:

 <?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');
        }
    }

use Illuminate\Database\Eloquent\Model;

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');
    }
}

and this is my controller query:

<?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
{
    // Consulta para generar la solicitud de permisos
    public function nuevasolicitud($ced)
    {       
        $consulta = srh_inf_persona::select('infp_cedula')->adminis()
        ->where('infp_cedula',$ced)
        ->first();

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

}

Any idea what the problem is? Thanks alot.

Change:

   $consulta = srh_inf_persona::select('infp_cedula')->adminis()
    ->where('infp_cedula',$ced)
    ->first();

to:

   $consulta = srh_inf_persona::select('infp_cedula')
    ->where('infp_cedula',$ced)
    ->first()->adminis();

This will resolve your error because you are lazy loading. When you do so , you need to call it on an instance of the eloquent model not the builder.

If you want to eager load it then you can do it like this:

   $consulta = srh_inf_persona::select('infp_cedula')
    ->where('infp_cedula',$ced)
    ->with(['adminis'])
    ->first();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM