Laravel 5.1发送错误数据进行查看

时间:2015-11-19 15:11:49

标签: php sql laravel eloquent

我正在我的主页上显示所有项目,并希望显示每个项目的amny喜欢。喜欢的内容存储在Likes表中,其中包含唯一ID和项目ID

所以在我的ProjectsController中,我编写了一个计数查询来查找当我在控制器中回显输出时每个项目有多少喜欢一切正常但在我的视图中我总是得到0

我的控制器功能

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]);
}

我的观点:

@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

2 个答案:

答案 0 :(得分:1)

您必须将$likes存入数组。

因为使用您的实际代码,var $likes始终(重新)设置原因foreach(){}

前:

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

<强>更新

并显示它

<?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

顺便说一下,这不是一个好习惯,但修复工作应该有效

答案 1 :(得分:1)

另一种方法是设置模型关系。 (http://laravel.com/docs/5.0/eloquent#one-to-many) 在您的模型项目中:

public static void main(String[] args) throws InterruptedException {

    Observable.from(getUsers()).flatMap(
        // we have the user
        user -> Observable.just(getCity(user)).map(
            // we have the city
            city -> user + " from " + city)) // do stuff with user + city
        .subscribe(result -> System.out.println(result));
}

现在你的控制器将是:

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

在您需要时查看到位:

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

UPD:我修正了返回值。所以现在在控制器中返回视图:

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