I have this problem. I have a Group and Role models, with a Many-to-Many relationship setup.
Group model
public function roles()
{
return $this->belongsToMany('App\Role', 'group_roles');
}
Role Model
public function groups()
{
return $this->belongsToMany('App\Group', 'group_roles');
}
GroupsController store method
public function store(Request $requests)
{
$group = new Group;
//$group->group_name = $requests->group_name;
//$group->save();
$group->create($requests->all());
$group->roles()->sync($requests->input('roles'));
Session::flash('success', $requests->group_name.' successfully added');
return redirect('/settings/groups/groups');
}
The problem I have here is that when I call create method on the group model, it returns null, thus causing this $group->roles()->sync($requests->input('roles')); to fail. However, when I use the save method, it works flawlessly. Why doesn't the create work?
EDIT: if I use create it does insert the records into the database, but the problem is that it's not return insertLastId.