简体   繁体   English

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

[英]How I use relationship in blade template (hasOne) || Laravel

Okay i'm trying get "likes" and "users" in Posts by relationship hasOne. 好的,我正在尝试通过关系hasOne在“帖子”中获得“点赞”和“用户”。

here is my Post.php Model class Posts extends Model { protected $table = 'posts'; 这是我的Post.php模型类Post.php扩展了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');
}}

My Blade template 我的刀片模板

@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

And there is my Controller 还有我的控制器

class PostsController extends Controller { 类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();
}

} }

Now if I do something like {{$deal->user->email}} its will work, if I go to something like this {{$deal->like->status}} its does not work, am I missing something ? 现在,如果我执行类似{{$deal->user->email}}的操作,则可以正常工作,如果我执行类似{{$deal->like->status}}的操作,则无法正常工作,我是否丢失了某些信息? ?

If you want multiple relationships to be eagerly loaded you need to use an array of relationships: Model::with(['rl1', 'rl2'])->get(); 如果要Model::with(['rl1', 'rl2'])->get();加载多个关系 ,则需要使用一系列关系: Model::with(['rl1', 'rl2'])->get();

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

EDIT: 编辑:

From that json in the comments that I see, there is no attribute named status in your Like model so thats probably the root of the problem 从我看到的注释中的json中,您的Like模型中没有名为status属性,因此这可能是问题的根源

Controller edit this code 控制器编辑此代码

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

And blade file code 和刀片文件代码

 @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

I think everything is good except 我认为一切都很好,除了

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

Please try as 请尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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