如何在 Laravel 中显示模型关系?

我有这样的数据库表:job,department,pcn和该表pcn具有性能job_id及department_id在其上。因此,在 Laravel 中,我对其等效模型有以下定义:


class Job extends Model

{

    public function pcns(){return $this->hasMany('App\Pcn', 'id');}

}

class Department extends Model

{

    public function pcns(){return $this->hasMany('App\Pcn', 'id');}

}

class Pcn extends Model

{

    public function job(){return $this->belongsTo('App\Job');}


    public function department(){return $this->belongsTo('App\Department');}

}

我现在的问题是显示 Pcn 列表的 pcn 索引给了我这个错误:


Trying to get property 'name' of non-object (View: C:\wamp\www\bookersystem\resources\views\pcn\index.blade.php)

其中我index.blade.php有这个:


@foreach($pcns as $key => $value)

    <td>{{ $value->control_number }}</td>

    <td>{{ $value->job->name }}</td>

    <td>{{ $value->department->name }}</td>

@endforeach

在我的 Pcn 控制器上:


public function index()

{


    $pcns = Pcn::paginate(50);


    return view('pcn.index', compact('pcns'));


}

至于我的迁移,我有这个定义:


public function up()

{

    Schema::create('pcn', function (Blueprint $table) {

        $table->engine = "InnoDB";

        $table->charset = 'utf8mb4';

        $table->collation = 'utf8mb4_general_ci';

        $table->bigIncrements('id');

        $table->unsignedBigInteger('department_id');

        //$table->foreign('department_id')->references('id')->on('department');

        $table->unsignedBigInteger('job_id');

        //$table->foreign('job_id')->references('id')->on('job');

        $table->string('control_number', 45);

        $table->string('center', 5);

        $table->string('status', 8)->nullable();

        $table->unsignedBigInteger('mrf_id')->nullable();

        $table->string('degree', 25)->default('Not Recruiting');

        $table->timestamps();

    });

}

我在这里做错了吗?


慕仙森
浏览 172回答 2
2回答

慕标琳琳

首先,最好id从关系定义中删除或声明right foreign key:class Job extends Model{&nbsp; &nbsp; //this&nbsp; &nbsp; public function pcns(){return $this->hasMany('App\Pcn');}&nbsp; &nbsp; //or this&nbsp; &nbsp; public function pcns(){return $this->hasMany('App\Pcn', 'job_id', 'id');}}class Department extends Model{&nbsp; &nbsp; //this&nbsp; &nbsp; public function pcns(){return $this->hasMany('App\Pcn');}&nbsp; &nbsp; //or this&nbsp;&nbsp; &nbsp; public function pcns(){return $this->hasMany('App\Pcn', 'department_id', 'id');}}第二步:最好预先加载关系以减少所需的查询数量:public function index(){&nbsp; &nbsp; $pcns = Pcn::with(['job', 'department'])->paginate(50);&nbsp; &nbsp; return view('pcn.index', compact('pcns'));}之后,在你看来,你并不真正需要$key=>$value的@foreach:@foreach($pcns as $pcn)&nbsp; &nbsp; <td>{{ $pcn->control_number }}</td>&nbsp; &nbsp; <td>{{ $pcn->job->name }}</td>&nbsp; &nbsp; <td>{{ $pcn->department->name }}</td>@endforeach

jeck猫

paginate()方法不返回的集合Pcn,它返回一个LengthAwarePaginator,当变成一个数组时,它有作为索引 ( total, per_page, next_link....)您需要从 LengthAwarePaginator@foreach($pcns as $pcn)&nbsp; &nbsp; <td>{{ $pcn->control_number }}</td>&nbsp; &nbsp; <td>{{ $pcn->job->name }}</td>&nbsp; &nbsp; <td>{{ $pcn->department->name }}</td>@endforeach顺便说一下,您应该在查询中预加载job和department关系以仅向数据库发送3 个请求而不是101。$pcns = Pcn::with(['job', 'department'])->paginate(50);
打开App,查看更多内容
随时随地看视频慕课网APP