Answer the question
In order to leave comments, you need to log in
How to synchronize the hasMany relationship model in Laravel as sync?
Good time everyone. I can not solve the problem with editing a contact and his phone numbers.
There are two contact models
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
protected $table = 'contacts';
public function phoneNums()
{
return $this->hasMany('App\Models\PhoneNum');
}
}
class PhoneNum extends Model
{
protected $table = 'phoneNums';
protected $fillable = ['phone_num'];
public function contact()
{
return $this->belongsTo('App\Models\Contact');
}
}
public function update(Request $request, $id)
{
$contact = Contact::find($id);
$contact->name = $request->name;
$contact->save();
// what should I do with the array $request->phonenumber ????
return redirect('/');
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question