user3742399
user3742399

Reputation: 57

Laravel resource create model with id

I have two models 'a' and 'b' and a->hasMany('b') and b->belongsTo('a')

So when I create one 'b' this should belongs to exactly one 'a'.

The problem is with the usual Route::resource('b', 'bController') I can just create this 'b' and don't know to which 'a' this belongs.

I tried editing the create() method in bController to create($id) but

Redirect::action('bController@create', $a->id)

Still redirects to /b/create?2 an gives an error

Missing argument 1 for bController::create()

Maybe a bit easier to unserstand when I use phoneand user.

Every phone belongs to one user.

How can I create a phone? How do I give the create() the parameter of the user and still use the Route::resource?

Upvotes: 3

Views: 2259

Answers (1)

The Alpha
The Alpha

Reputation: 146201

I think you are on a wrong direction because User and Phone are two models and hence a User has many phones so here Phone model is a child model (related) of User class and a Phone can't exist without a User so you only need to create a User through the controller and Phone will be created when the User gets created, for example:

Route::resource('users', 'UserController');

Now assume that you have tow models as User and Phone and the User model has phones method which builds the relationship (phones = User->hasMany('Phone')) and the Phone model has a user method which builds the relationship (user = Phone->belomgsTo('User')).

// User model
class User extends Eloquent {

    //...
    public function phones()
    {
        return $this->hasMany('Phone');
    }
}

// Phone model
class Phone extends Eloquent {

    //...
    public function user()
    {
        return $this->belongsTo('user');
    }
}

Now to create a User the store method will be used like this:

// UserController.php
class UserController extends BaseController {

    // Other methods

    // Creates a User
    // URI: /users Method: POST
    public function store()
    {
        // Create a User using User model
        $user = User::create(...);
        if($user) {
            // Initialize/Create a Phone
            $phone = new Phone(array(...));
            if($phone) {
                // Save the Phone and relate with User
                $user->phones()->save($phone);
            }
        }
    }

}

This is the basic idea and the final thing is that, a Phone doesn't require a Controller to be created, because it's the part of a User so when you create a new User then create a (or more) Phone from the UserController after you create a User or update a Phone when you update a User.

So if you want to load a User with it's related Phone models then you may do it like this:

$user = User::with('phones')->find(1);

So, when you load a user, you can load all the phones related with that user so during editing of an user; you only need to load a User with related Phone models and pass that model to the view.

To add a new Phone to an existing User you need to edit the User model so you can load a User model with phones and pass that User model to the view for editing. When new Phone being added to the user, you only need to attach that Phone with existing User, that's it. Hope it makes sense.

Upvotes: 2

Related Questions