Eloquent One to One Relationship CRUD

Different CRUD statements to add to routes:

Route::get('/insert', function () {
    $user = User::findOrFail(1);
    $address = new Address(['name'=>'1234 Houston Ave NY NY 11218']);
    $user->address()->save($address);
});

Route::get('/update', function () {
    $address = Address::whereUserId(1)->first();
    $address->name = '1234 Update Ave Alaska';
    $address->save();
});

Route::get('/read', function () {
    $user = User::findOrFail(1);
    echo $user->address->name;
});

Route::get('/delete', function () {
    $user = User::findOrFail(1);
    $user->address()->delete();
});

Leave a Reply

Your email address will not be published. Required fields are marked *