jediping
4 years ago
259
11
Laravel

Using a function from another controller

Posted 4 years ago by jediping

I've got several places in my app where one controller needs to access a method in another controller. I tried something like this:

ToysController.php

public function new_user_toy(User $user){
[trim]
        //Make sure the toy knows how to make itself and one other pattern
        ToyPattern::store($toy->id, $toy_pattern->id);
        ToyPattern::store($toy->id, $known_pattern->id);
    }

It doesn't work, and from what I've read on the issues, it shouldn't, because I shouldn't be having controllers talk to each other. (Plus I think that actually calls the model, not the controller.) Unfortunately, I haven't been able to find any resources on how to implement something like this correctly in the MVC framework. I read a bit that suggested going through the model, but didn't really provide a good example on how that would work.

Some things I'm trying to do: When a new user is created, assign them a toy. (Many-to-many relationship between toys and users.) When a toy is created, assign it two known patterns. (One-to-many relationship between toys and known patterns.)

I'm pretty new to Laravel and MVC, so even some basic guidance here would be great.

Please sign in or create an account to participate in this conversation.