Laravel关系问题或者我混淆了

时间:2014-01-30 12:06:54

标签: php laravel-4

我有一个名为提案的表,其中包含以下内容

id,job_id,user_id,created

我还有一个工作表,其中包含以下内容:

id,user_id,title,description

所以我创建了一个名为proposal

的新模型

其中包含以下内容:

 class Proposals extends Eloquent {

 protected $table = 'proposals';
 public $timestamps = false;

 public function user()
 {
      return $this->belongsTo('User'); // This works fine by calling $proposals->user->email
 }

 public function jobs()
 {
    return $this->belongsTo('Jobs'); // This is not working, when i call $proposals->job->title
 }


}

然后在我的控制器中我有:

 public function workstream()
 {
     $user_id = Auth::user()->id;
     $proposals = Proposals::where('user_id','=', $user_id)->paginate(5);
     return View::make('jobs/workstream', compact('proposals'))->with('meta_title', 'Workstream');
 }

最后在我看来,我有:

    @foreach($proposals as $item)
           <p>{{ $item->user->first_name }}&nbsp;{{ ucfirst(substr($item->user->last_name, 0, 1))  }} 
                sent a propopsal for ~  {{ $item->jobs->title }} <a href="">See proposal</a>
           </p>

      @endforeach

此{{$ item-&gt; jobs-&gt; title}}显示试图获取非对象的属性,所以也许我让自己混淆了

1 个答案:

答案 0 :(得分:2)

我认为您需要将jobs课程中的Proposals更改为job

public function job()
{
   return $this->belongsTo('Jobs');
}

之后,您可以在视图中访问$item->job->title

@foreach($proposals as $item)
       <p>{{ $item->user->first_name }}&nbsp;{{ ucfirst(substr($item->user->last_name, 0, 1))  }} 
            sent a propopsal for ~  {{ $item->job->title }} <a href="">See proposal</a>
       </p>

@endforeach
相关问题