I'm not sure I am asking the questions correctly, but this is what I am trying to do.
我不确定我问的问题是否正确,但这就是我要做的。
So we can get the current from
所以我们可以得到电流。
$model = Model::find($id)
$ =模型:模型:找到($ id)
Then we can get it's relationships like:
然后我们可以得到这样的关系:
$model->relationships()->id
$ - >关系模型()- > id
Then we have actions like:
然后我们采取如下行动:
$model->relationships()->detach(4);
$ - >关系模型()- >分离(4);
My question is, can we have a custom method like:
我的问题是,我们能有一个自定义的方法吗?
$model->relationships()->customMethod($params);
?
$ - >关系模型()- > customMethod($ params);?
and in the model it may look like:
在模型中,它可能是这样的:
public function customMethod($params){
//Do something with relationship id
}
But further more, how in the customMethod
would I get the $models
info like id?
但更重要的是,如何在customMethod中获得像id这样的$models信息?
Sorry if this may be a bit confusing.
抱歉,这可能有点让人困惑。
1 个解决方案
#1
2
First of all, if you want to access a related object, you do this by accessing an attribute with the same name as the relation. In your case, in order to access object(s) from relationships, you need to do this by:
首先,如果您想访问一个相关的对象,您可以通过访问与关系同名的属性来实现这一点。在您的情况下,为了从关系中访问对象,您需要通过以下方式进行:
$model->relationships //returns related object or collection of objects
instead of
而不是
$model->relationships() //returns relation definition
Secondly, if you want to access attributes on the related object, you can do it the same way:
其次,如果你想访问相关对象的属性,你可以用同样的方法:
$relatedObjectName = $model->relationship->name; // this works if you have a single object on the other end of relations
Lastly, if you want to call a method on a related model you need to implement this method in related model class.
最后,如果您想调用一个相关模型上的方法,您需要在相关模型类中实现这个方法。
class A extends Eloquent {
public function b() {
return $this->belongsTo('Some\Namespace\B');
}
public function cs() {
return $this->hasMany('Some\Namespace\C');
}
}
class B extends Eloquent {
public function printId() {
echo $this->id;
}
}
class C extends Eloquent {
public function printId() {
echo $this->id;
}
}
$a = A::find(5);
$a->b->printId(); //call method on related object
foreach ($a->cs as $c) { //iterate the collection
$c->printId(); //call method on related object
}
You can read more about how to define and use relationships here: http://laravel.com/docs/5.1/eloquent-relationships
您可以在这里阅读更多关于如何定义和使用关系的信息:http://laravel.com/docs/5.1/eloqu- relationships
#1
2
First of all, if you want to access a related object, you do this by accessing an attribute with the same name as the relation. In your case, in order to access object(s) from relationships, you need to do this by:
首先,如果您想访问一个相关的对象,您可以通过访问与关系同名的属性来实现这一点。在您的情况下,为了从关系中访问对象,您需要通过以下方式进行:
$model->relationships //returns related object or collection of objects
instead of
而不是
$model->relationships() //returns relation definition
Secondly, if you want to access attributes on the related object, you can do it the same way:
其次,如果你想访问相关对象的属性,你可以用同样的方法:
$relatedObjectName = $model->relationship->name; // this works if you have a single object on the other end of relations
Lastly, if you want to call a method on a related model you need to implement this method in related model class.
最后,如果您想调用一个相关模型上的方法,您需要在相关模型类中实现这个方法。
class A extends Eloquent {
public function b() {
return $this->belongsTo('Some\Namespace\B');
}
public function cs() {
return $this->hasMany('Some\Namespace\C');
}
}
class B extends Eloquent {
public function printId() {
echo $this->id;
}
}
class C extends Eloquent {
public function printId() {
echo $this->id;
}
}
$a = A::find(5);
$a->b->printId(); //call method on related object
foreach ($a->cs as $c) { //iterate the collection
$c->printId(); //call method on related object
}
You can read more about how to define and use relationships here: http://laravel.com/docs/5.1/eloquent-relationships
您可以在这里阅读更多关于如何定义和使用关系的信息:http://laravel.com/docs/5.1/eloqu- relationships