1 year ago

(One to One) to (Zero to Many) / (Zero to One) to (One to Many)

Posted 1 year ago by Chron

Is this possible without making a pivot table? For example..

One child has/have zero to many Phones

I think it can be easily achieved with something like this..

$child = Child::create([
    'name' => $request['name'],
    'age' => $request['age'],
    //add child infos into the phones table.
    'child_id' => $child->id;
    'model' => $request['model'],

else{  // if the child doesn't have any phone
    //just create the child account

Add the child infos to the phones table ONLY if they have any phones.

From what I see, this is just okay because the foreign key "child_id" in phones table will never get a null value unless the child has a phone.

I've heard of belongsToMany, that deals with many to many relationships but can it the also deal with zero to many relationship?

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