我怎样才能加入Laravel来获取数据?

时间:2018-05-03 16:32:18

标签: php mysql laravel laravel-5

我正在使用Laravel 5,我有以下模型

Group.php

class Group extends Model
{

    protected $fillable = ([
    'id'
    ]);

    public function users(){
        return $this->belongsToMany(
            'App\User',
            'user_group'
        );
    }

    public function posted(){
        return $this->hasMany(
            'App\PostGroup'
        );
    }
}

PostGroup.php

class PostGroup extends Model
{
    protected $fillable = [
        'group_id', 'user_id', 'post_content'
    ];

    public function user(){
        return $this->belongsTo('App\User');
    }

    public function group(){
        return $this->belongsTo('App\Group');
    }

    public function commented(){
        return $this->hasMany(
            'App\PostComment'
        );
    }
}

PostComment.php

class PostComment extends Model
{
    protected $fillable = [
        'post_id', 'user_id', 'comment_content'
    ];

    public function post(){
        return $this->belongsTo('App\PostGroup');
    }

    public function user(){
        return $this->belongsTo('App\User');
    }

}

在上下文中,我的网络应用程序会显示groups,您可以在其中创建posts以及您可以撰写comments的帖子。所以,我的数据库中有以下关系:

  • 组:(id,name,description);
  • PostGroup :( id,group_id,user_id,post_content);
  • PostComment:(id,post_id,user_id,comment_content);

我想要做的是创建一个PostComment对象列表,然后进行查询以获取组中所有帖子的所有评论, MySQL看起来像:

SELECT post_comment.* FROM post_comment,post_group WHERE post_comment.post_id = post_group.id AND post_groups.group_id = 1;

现在,例如,在Laravel,如果我想得到一个组的所有帖子,我有以下代码行:

$postList = Group::find($id)->posted->sortByDesc('created_at');

posted是组模态中的函数。我试着做一些像

这样的事情
$commentList = PostGroup::where('group_id', $id)->commented;

但它不起作用,我该如何解决?

修改

GroupController.php

/**
 * Display the specified resource.
 *
 * @param  int $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    // Replace with shares of publication-group-model
    $authUser =  Auth::user();
    $sharesList = Group::find($id)->shares->sortByDesc('created_at');
    $groupList = $authUser->groupsAsMember->where('id', '<>', $id);
    $group = $authUser->groups->find($id);
    $postList = Group::find($id)->posted->sortByDesc('created_at');

    $postsGroups = PostGroup::where('group_id', $id)->with('commented')->get(); 

    //dd($postsGroups);

    foreach ($postsGroups as $postGroup) {
        $commentsList = $postGroup->commented;
    }

    //dd($commentsList);

    return view('Pages.Group.detail', ['sharesList' => $sharesList, 'groupList' => $groupList, 'theGroup' => $group, 'postList' => $postList, 'commentsList' => $commentsList]);
}

3 个答案:

答案 0 :(得分:2)

Eloquent的HasManyThrough关系是针对这种情况设计的,以便能够访问远距离的关系:

class Group extends Model
{
    public function comments()
    {
        return $this->hasManyThrough(
            PostComment::class, 
            PostGroup::class, 
            'group_id', 
            'post_id',
            'id', 
            'id'
        );
    }
}

答案 1 :(得分:1)

// This doesn't work because you can't call `commented` on a query builder
$commentList = PostGroup::where('group_id', $id)->commented;

以下是使用这种关系的一种方法:

// Eager load the commented relationship
$postsGroups = PostGroup::where('group_id', $id)->with('commented')->get();

foreach ($postsGroups as $postsGroup) {
    $comments = $postGroup->commented;
}

如果您不想循环甚至需要PostGroup,那么您需要将PostComment表加入PostGroup表,以便您可以访问post_groups.group_id列。

答案 2 :(得分:1)

你需要做这样的事情:

$id = "1"; //suppose
$postsGroups = PostGroup::where('group_id', $id)->with('commented')->get(); //this will give you a collection

dd($postGroups); //check this first

//with every loop, add comments to the comment collection    
foreach ($postsGroups as $postGroup) {
    $comments = $postGroup->commented;
    dd($comments); //check  if you are getting the data
}

关于评论中的另一个问题:(下面的代码会给你一些想法)

use Illuminate\Database\Eloquent\Collection;   // use this to create new collections

     $id = "1";
     $postsGroups = PostGroup::where('group_id', $id)->with('commented')->get();

     $comments = new Collection();
     //now you can loop through the object to get the values

     foreach ($postsGroups as $postGroup) {
         $coms = $postGroup->commented;
         $comments->push([
                'comments' => $coms,
                ]);
     }
     return view('index', array(
            'comments' => $comments,
        ));
相关问题