How do I get the current model from custom method in laravel

Jason Spick

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)

Then we can get it's relationships like:

$model->relationships()->id

Then we have actions like:

$model->relationships()->detach(4);

My question is, can we have a custom method like:

$model->relationships()->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?

Sorry if this may be a bit confusing.

jedrzej.kurylo

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I get related model from collection in Laravel 5.2?

From Dev

How do I call a custom method on a model with a template in Django?

From Dev

How do I add a custom method to a backbone model?

From Dev

How do I call a custom method on a model with a template in Django?

From Dev

Laravel: How do I create a custom pivot model?

From Dev

How do I add a custom field to a model in Laravel?

From Dev

How do i get shipping method of current Store : Magento

From Dev

How do i get shipping method of current Store : Magento

From Dev

How do I get the current HttpContext from a HttpActionExecutedContext

From Dev

How do I get current ionic mode from code

From Dev

How do I get the offset of an element from the current screen position?

From Dev

How do I get current ionic mode from code

From Dev

How do I get the current bandwidth speed of an interface from the terminal?

From Dev

How do I get the current HttpContext from a HttpActionExecutedContext

From Dev

How do I pass data from a Model to a Controller in Laravel

From Dev

laravel 5.1 : how can i get nested relation from model?

From Dev

How do I get Apiary model details from the rendered documentation?

From Dev

How do I get the value from the model to the controller

From Dev

How do I get an object from Model to AngularJS variable in JSP?

From Dev

How do I get the values of the model returned from Controller in ajax?

From Dev

How do I get the selected text from custom spinner?

From Dev

How do I get the values from Custom UITableViewCell to ViewController?

From Dev

How do I get Custom Attibutes from a assembly in Winrt

From Dev

How do I use a remote method in one model to return info from another model?

From Dev

How do I get class annotations from a Method instance?

From Dev

How do I access data from a fetch in a get method?

From Dev

Call a custom model method from controller laravel4

From Dev

How do I debug the output of a method in a model?

From Java

How do I get the current date in JavaScript?

Related Related

  1. 1

    How do I get related model from collection in Laravel 5.2?

  2. 2

    How do I call a custom method on a model with a template in Django?

  3. 3

    How do I add a custom method to a backbone model?

  4. 4

    How do I call a custom method on a model with a template in Django?

  5. 5

    Laravel: How do I create a custom pivot model?

  6. 6

    How do I add a custom field to a model in Laravel?

  7. 7

    How do i get shipping method of current Store : Magento

  8. 8

    How do i get shipping method of current Store : Magento

  9. 9

    How do I get the current HttpContext from a HttpActionExecutedContext

  10. 10

    How do I get current ionic mode from code

  11. 11

    How do I get the offset of an element from the current screen position?

  12. 12

    How do I get current ionic mode from code

  13. 13

    How do I get the current bandwidth speed of an interface from the terminal?

  14. 14

    How do I get the current HttpContext from a HttpActionExecutedContext

  15. 15

    How do I pass data from a Model to a Controller in Laravel

  16. 16

    laravel 5.1 : how can i get nested relation from model?

  17. 17

    How do I get Apiary model details from the rendered documentation?

  18. 18

    How do I get the value from the model to the controller

  19. 19

    How do I get an object from Model to AngularJS variable in JSP?

  20. 20

    How do I get the values of the model returned from Controller in ajax?

  21. 21

    How do I get the selected text from custom spinner?

  22. 22

    How do I get the values from Custom UITableViewCell to ViewController?

  23. 23

    How do I get Custom Attibutes from a assembly in Winrt

  24. 24

    How do I use a remote method in one model to return info from another model?

  25. 25

    How do I get class annotations from a Method instance?

  26. 26

    How do I access data from a fetch in a get method?

  27. 27

    Call a custom model method from controller laravel4

  28. 28

    How do I debug the output of a method in a model?

  29. 29

    How do I get the current date in JavaScript?

HotTag

Archive