mercredi 7 novembre 2018

How to eager load the 'user' function from eloquent relationship previously set (User Model) in controller when I have this situation

NOTE** The relationship between Post and Tag model is many to many and also the relationship with User and Post model is one to Many. Everything working good I just have to many Queries from users where users.id....when debbug it. I want to use eager load for the user function in this situation in my controller function. How to use it like with('user')->get(); .... Thanks.

This is my web.php route:

Route::get('/posts/tags/tag/{tag}', 'TagsController@show');

This is my controller function for that:

*public function show(Tag $tag) {

    return view('tags.show', compact('tag'));
}*

This is my show.blade.php:

*@foreach ($tag->posts as $post)

          <div class="col-sm-6" >

              <div class="card" style="margin-bottom:15px;">
                <img class="card-img-top" src="https://neilpatel-qvjnwj7eutn3.netdna-ssl.com/wp-content/uploads/2015/07/seoimage.jpg" height="270px" alt="Card image cap">
                <div class="card-body">
                  <h5 class="card-title">
                      <a href="/posts/">
                      
                    </a>
                  </h5>
                  <p class="card-text">
                   
                  </p>
                  <p class="card-text"><small class="text-muted"> on
                            </small></p>
                  <hr>
                  <a href="/posts/" class="btn btn-warning">Go somewhere</a>
                </div>
              </div>
            </div>
      @endforeach*



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire