user3156722
user3156722

Reputation: 1

call model function from another model in laravel

how to call model method in another model, example I have code like this

/model/user.php

public function get_token_by_id($id){
    //some code
}

i want call in my another model

/model/restaurant

App::bind('user','user');

class RestaurantController extends BaseController {

    public function __construct(user $modelUser){
        $this->modelUser = $modelUser;  
    }
    public function getUser(){
       $someVar = $this->modelUser->get_token_by_id($id);  
    }
}

But i get an error

Call to a member function get_token_by_id() on a non-object

how to fix it?

Upvotes: 0

Views: 4267

Answers (1)

Alexandre Butynski
Alexandre Butynski

Reputation: 6746

Well... that's because $this->modelUser is a non object !

To be more precise, $this->modelUser returns null or something like that (try a var_dump($this->modelUser)). It could be because your model doesn't have the attribute declaration (protected $modelUser) or because you don't pass the right variable into the constructor.

Upvotes: 1

Related Questions