简体   繁体   中英

Laravel 5.1 wrong data being send to view

I am displaying all projects on my homepage and wanna show how amny likes each project has. The likes are stored in the Likes table with a unique id and the project id

So in my ProjectsController I write a count query to find how many likes each project has when I echo the output in my controller everything works but in my view I always get 0 as result

My controller function

public function home_projects()
{
    $projects = Project::all();

    foreach ($projects as $project){
        $likes = Like::where('project_id', '=', $project->id)->count();
        echo $likes;
    }

    //return $projects;
    return view('welcome', ['projects' => $projects, 'likes' => $likes]);
}

My view:

@foreach ($projects as $project)
            <div class="col-md-4 grid_item">


                <a class="grid_link" href="projects/{{ $project->id }}">
                    <img class="imagebox" src="uploads/projects/{{ $project->file_name }}">
                </a>


                <div class="image_actions">
                    <form action="" method="post" class="form-inline">
                        <button type="submit" class="like_button">Like</button>
                        <label for="">{{ $likes }}</label>
                        <button type="submit" class="comment_button">Comment</button>
                        <label for="">3</label>
                        <button type="submit" class="favourite_button">Favourite</button>
                    </form>
                </div>
            </div>
            @endforeach

You must stock your $likes in an array.

Because with your actual code, the var $likes is always (re)set cause foreach(){}

Ex :

$likesArray = array();
foreach ($projects as $project) {
        $likesArray[] = Like::where('project_id', '=', $project->id)->count();
}

UPDATE

And to display it

<?php $key = 0; ?>
@foreach ($projects as $project)
            <div class="col-md-4 grid_item">


                <a class="grid_link" href="projects/{{ $project->id }}">
                    <img class="imagebox" src="uploads/projects/{{ $project->file_name }}">
                </a>


                <div class="image_actions">
                    <form action="" method="post" class="form-inline">
                        <button type="submit" class="like_button">Like</button>
                        <label for="">{{ $likesArray[$key] }}</label>
                        <button type="submit" class="comment_button">Comment</button>
                        <label for="">3</label>
                        <button type="submit" class="favourite_button">Favourite</button>
                    </form>
                </div>
            </div>
            // increment your key var
            <?php $key++; ?>
            @endforeach

By the way, this is not a good practice , but the fix should work

Another way to do this is to set model relationship. ( http://laravel.com/docs/5.0/eloquent#one-to-many ) In your model Project:

public function likes() {
  return $this->hasMany('Like', 'project_id');
}

And now your controller will be:

$projects = Project::all();
return view('welcome', ['projects' => $projects]);

View in place when you need likes:

{{ $project->likes()->count() }}

UPD: I fixed return value. So now in the controller to return view:

return view('welcome', ['projects' => $projects]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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