php Laravel:将参数传递给关系函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23016216/
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 16:25:56  来源:igfitidea点击:

Laravel: Pass Parameter to Relationship Function?

phplaravel

提问by user2840318

Is it possible to pass, somehow, a parameter to a relationship function?

是否可以以某种方式将参数传递给关系函数?

I have currently the following:

我目前有以下几点:

public function achievements()
{
     return $this->belongsToMany('Achievable', 'user_achievements')->withPivot('value', 'unlocked_at')->orderBy('pivot_unlocked_at', 'desc');
}

The problem is that, in some cases, it does not fetch the unlocked_at column and it returns an error.

问题在于,在某些情况下,它不会获取 unlocked_at 列并返回错误。

I have tried to do something like:

我试图做类似的事情:

public function achievements($orderBy = true)
{
$result = $this->belongsToMany (...)
if($orderBy) return $result->orderBy(...)
return $result;
}

And call it as:

并将其称为:

$member->achievements(false)->(...)

But this does not work. Is there a way to pass parameters into that function or any way to check if the pivot_unlocked_atis being used?

但这不起作用。有没有办法将参数传递给该函数或以任何方式检查是否pivot_unlocked_at正在使用?

回答by Hrach

Well what I've did was just adding new attribute to my model and then add the my condition to that attirbute,simply did this.

那么我所做的只是向我的模型添加新属性,然后将我的条件添加到该属性中,只需这样做。

Class Foo extends Eloquent {
    protected $strSlug;

    public function Relations(){
         return $this->belongsTo('Relation','relation_id')->whereSlug($this->strSlug);
    }
 }

 Class FooController extends BaseController {
     private $objFoo;


     public function __construct(Foo $foo){
         $this->objFoo = $foo
     }

     public function getPage($strSlug){
        $this->objFoo->strSlug = $strSlug;
        $arrData = Foo::with('Relations')->get();
        //some other stuff,page render,etc....
     }
 }

回答by Julio Popócatl

You can do more simple, and secure:

你可以做更简单、更安全的事情:

When you call the relation function with the parentesis Laravel will return just the query, you will need to add the get() or first() to retrieve the results

当您使用 parentesis 调用关系函数时,Laravel 将仅返回查询,您需要添加 get() 或 first() 以检索结果

public function achievements($orderBy = true)
{
if($orderBy) 
    $this->belongsToMany(...)->orderBy(...)->get();
else
    return $this->belongsToMany(...)->get();
}

And then you can call it like:

然后你可以这样称呼它:

$member->achievements(false);

Works for the latest version of Laravel.

适用于最新版本的 Laravel。

回答by ethris

You can simply create a scope and then when necessary add it to a builder instance.

您可以简单地创建一个范围,然后在必要时将其添加到构建器实例中。

Example:

例子:

User.php

用户名

public function achievements()
{
    return $this->hasMany(Achievement::class);
}

Achievement.php

成就.php

public function scopeOrdered(Builder $builder)
{
    return $builder->orderBy(conditions);
}

then when using:

然后在使用时:

//returns unordered collection
$user->achievements()->get();

//returns ordered collection
$user->achievements()->ordered()->get();

You can read more about scopes at Eloquent documentation.

您可以在Eloquent 文档 中阅读有关范围的更多信息。