php 查询关系 Eloquent

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20036269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:48:48  来源:igfitidea点击:

Query relationship Eloquent

phplaravellaravel-4eloquent

提问by Vuk Stankovi?

I have Newsmodel, and Newshas many comments, so I did this in Newsmodel:

我有News模型,并且News有很多评论,所以我在News模型中做了这个:

public function comments(){
    $this->hasMany('Comment', 'news_id');
}

But I also have field trashedin commentstable, and I only want to select comments that are not trashed. So trashed <> 1. So I wonder is there a way to do something like this:

但是我trashedcomments表中也有字段,我只想选择未被删除的评论。所以trashed <> 1。所以我想知道有没有办法做这样的事情:

$news = News::find(123);
$news->comments->where('trashed', '<>', 1); //some sort of pseudo-code

Is there a way to use above method or should I just write something like this:

有没有办法使用上述方法,或者我应该写这样的东西:

$comments = Comment::where('trashed', '<>', 1)
    ->where('news_id', '=', $news->id)
    ->get();

回答by rmobis

Any of these should work for you, pick the one you like the most:

这些都应该适合你,选择你最喜欢的:

  1. Eager-loading.

    $comments = News::find(123)->with(['comments' => function ($query) {
        $query->where('trashed', '<>', 1);
    }])->get();
    

    You can inject the parameter to query function by use($param)method, that allows you to use dynemic query value at runtime.

  2. Lazy-loading

    $news = News::find(123);
    $comments = $news->comments()->where('trashed', '<>', 1)->get();
    
  1. 急切加载。

    $comments = News::find(123)->with(['comments' => function ($query) {
        $query->where('trashed', '<>', 1);
    }])->get();
    

    您可以通过use($param)方法将参数注入查询函数,这允许您在运行时使用动态查询值。

  2. 懒加载

    $news = News::find(123);
    $comments = $news->comments()->where('trashed', '<>', 1)->get();
    


I couldn't help but notice, though, that what you're probably trying to do is handle soft deleting, and that Laravel has built-in functionality to help you with that: http://laravel.com/docs/eloquent#soft-deleting

不过,我不禁注意到,您可能想做的是处理软删除,而 Laravel 具有内置功能来帮助您:http://laravel.com/docs/eloquent#软删除

回答by glnemeth

rmobis's answer was what I needed, but it throws an error in current Laravel 5. You have to use it as an associatve array now:

rmobis的答案正是我所需要的,但它在当前的 Laravel 5 中引发了错误。您现在必须将其用作关联数组:

$comments = News::find(123)->with(
    ['comments' => function ($query) {$query->where('trashed', '<>', 1);}]
);

Took me some time to figure it out, hope this will help others.

我花了一些时间来弄清楚,希望这会对其他人有所帮助。

Read more in Laravel's Docs (5.6): https://laravel.com/docs/5.6/eloquent-relationships#querying-relations

在 Laravel 的文档 (5.6) 中阅读更多内容:https://laravel.com/docs/5.6/eloquent-relationships#querying-relations

回答by antoniputra

You can do simply in your eloquent model file. do like this :

您可以简单地在您的 eloquent 模型文件中进行。这样做:

public function comments_with_deleted()
{
    return $this->belongsTo('Comments', 'id')->where('deleted', 1);
}

public function comments()
{
    return $this->belongsTo('Comments', 'id');
}

call like this :

像这样打电话:

// for show comments with deleted
$comments = News::find(123)->with('comments_with_deleted');

// for show comments without deleted
$comments = News::find(123)->with('comments');