为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕仙森 2021-12-03 10:33:01
我有这样的数据库表: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();    });}我在这里做错了吗?
查看完整描述

2 回答

?
慕标琳琳

TA贡献1830条经验 获得超9个赞

首先,最好id从关系定义中删除或声明right foreign key:


class Job extends Model

{

    //this

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

    //or this

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

}


class Department extends Model

{

    //this

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

    //or this 

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

}

第二步:最好预先加载关系以减少所需的查询数量:


public function index()

{

    $pcns = Pcn::with(['job', 'department'])->paginate(50);


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

}

之后,在你看来,你并不真正需要$key=>$value的@foreach:


@foreach($pcns as $pcn)

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

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

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

@endforeach


查看完整回答
反对 回复 2021-12-03
?
jeck猫

TA贡献1909条经验 获得超7个赞

paginate()方法不返回的集合Pcn,它返回一个LengthAwarePaginator,当变成一个数组时,它有作为索引 ( total, per_page, next_link....)


您需要从 LengthAwarePaginator


@foreach($pcns as $pcn)

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

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

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

@endforeach

顺便说一下,您应该在查询中预加载job和department关系以仅向数据库发送3 个请求而不是101。


$pcns = Pcn::with(['job', 'department'])->paginate(50);


查看完整回答
反对 回复 2021-12-03
  • 2 回答
  • 0 关注
  • 171 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信