Laravel: Pass Parameter to Relationship Function?

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.

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_at is being used?


那么我所做的只是为我的模型添加新的属性,然后将我的条件添加到attirbute中,简单地做到了这一点。

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....
     }
 }
链接地址: http://www.djcxy.com/p/19856.html

上一篇: SBT:具有可在build.sbt中访问的值的外部配置文件

下一篇: Laravel:将参数传递给关系函数?