我如何在刀片模板中使用关系(hasOne)|| Laravel

时间:2018-02-09 23:26:46

标签: php laravel has-one

好吧,我正在尝试通过关系hasOne获得帖子中的“赞”和“用户”。

这是我的Post.php模型     class Posts扩展Model {     protected $ table ='posts';

public function User()
{
    return $this->hasOne(User::class, 'id', 'user_id');
}

public function Like()
{
    return $this->hasOne(Like::class, 'post_id', 'id');
}}

我的刀片模板

@foreach ($showdeals as $deal)


                                <div class="tab-pane active" id="home" role="tabpanel">
                                    <div class="card-body">
                                        <div class="profiletimeline">

                                            {{$deal->like->status}}
                                            <br>
                                            {{$deal->user->email}}

                        <div class="sl-item">
                            <div class="sl-left"> <img src=" {{asset( '/assets/images/users/2.jpg')}}" alt="user" class="img-circle"> </div>
                            <div class="sl-right">
                                <div> <a href="#" class="link">{{$deal->user->username}}</a> || {{$deal->subject}} <Br> <span class="sl-date">{{$deal->created_at}}</span>
                                    <div class="m-t-20 row">
                                        <div class="col-md-3 col-xs-12"><img src="{{$deal->image}}" alt="user" class="img-responsive radius"></div>
                                        <div class="col-md-9 col-xs-12">
                                            <p> {{$deal->body}} </p> <a href="{{$deal->link}}" class="btn btn-success"> עבור למוצר </a></div>
                                    </div>
                                    <div class="like-comm m-t-20"> <a href="javascript:void(0)" class="link m-r-10">2 תגובות</a> <a href="javascript:void(0)" class="link m-r-10"><i class="fa fa-heart text-danger"></i> 5 לייקים</a> </div>
                                </div>
                            </div>
                        </div>




                    </div>
                    <hr></div>
            </div>

                @endforeach

还有我的控制器

类PostsController扩展了Controller {

public function showdeals()
{

    $showdeals = Posts::with( 'User', 'Like')->get();
        return view('posts.show', compact('showdeals'));
}




public function helpnewview(){
    return view('posts.anew');
}


public function helpnew(Request $request){
    //User pick link
    $userlink = $request['userlink'];
    return \Redirect::route('newdeal', compact('userlink'));
}



public function new(Request $request)
{
    //Emdeb user link
    $link = Embed::create($request['userlink']);
    $linke = $request['userlink'];
    return view('posts.new', compact('link', 'userlink', 'linke'));
}




public function create(Request $request)
{

    $posts = New Posts;
    $posts->user_id = Auth::User()->id;
    $posts->subject = $request['subject'];
    $posts->body = $request['body'];
    $posts->link = $request['link'];
    $posts->price = $request['price'];
    $posts->image = $request['image'];
    $posts->tag = $request['tag'];
    $posts->save();

    return back();
}

}

现在,如果我做{{$deal->user->email}}这样的事情,它会起作用, 如果我去这样的事情{{$deal->like->status}}它不起作用, 我错过了什么吗?

3 个答案:

答案 0 :(得分:0)

如果您希望加载multiple relationships,则需要使用一系列关系:Model::with(['rl1', 'rl2'])->get();

public function showdeals()
{
    ...
    $showdeals = Posts::with(['User', 'Like'])->get();
    ...
}

修改

在我看到的评论中jsonstatus模型中没有名为Like的属性,这可能是问题的根源

答案 1 :(得分:0)

控制器编辑此代码

  public function showdeals()
 {
        $showdeals = Posts::all();
        return view('posts.show', compact('showdeals'));
 }

和刀片文件代码

 @foreach ($showdeals as $deal)
                            <div class="tab-pane active" id="home" role="tabpanel">
                                <div class="card-body">
                                    <div class="profiletimeline">

                                        {{ $deal->Like->status }}
                                        <br>
                                        {{ $deal->User->email }}

   @endforeach

答案 2 :(得分:0)

我认为一切都很好,除了

  

{{$ deal-&gt; like-&gt; status}}
{{$ deal-&gt; user-&gt; email}}

请尝试

{{$deal->Like()->status}} 
<br> 
{{$deal->User()->email}}