Friday, March 17, 2017

How to make a laravel like counter?

I have a loop like this in my blade.php file:

        @foreach ($posts as $post)
          <article class="post" data-postid="">
                <p></p>
                <div class="info">
                    Posted by  on 
                </div>
                <div class="interaction">
                  <a href="#" class="like"> </a> |
                  <a href="#" class="like"> </a> |
                  @if(Auth::user() == $post->user)
                  <a href="#" class="edit">Edit Post</a> |
                  <a href="">Delete</a>
                  @endif
                </div>
            </article>
            <br>

        @endforeach

This is my controller function:

  public function getDashboard(Request $request) {
     $posts = Post::orderBy('created_at', 'desc')->get();
     $countlike = Like::where(['like' => '1']);
     $countdislike = Like::where(['like' => '0']);
     return view('dashboard')->with(['posts' => $posts])->with(['countlike' =>   $countlike])->with(['countdislike' => $countdislike]);
  }

Likes in the database: likes in the database

Posts in the database: posts in the database

The result: enter image description here

While the first post in the loop communicates with ORM, the rest doesn't. I cannot integrate eloquent ORM to the loop in the blade.php file, what am I doing wrong here?



via Göktuğ Aşçı

Advertisement