mercredi 27 mars 2019

Getting an error saving data to parent and its sub child table in laravel 5.4

class User extends Authenticatable
{

....
....
.... 

    public function extensions(){
        return $this->hasMany('App\Extension','user_id');
    }

and

class Extension extends Model
{
....
....
....    
    public function user()
    {
        return $this->belongTo('App\User');
    }

and finally I want to save data to user and then its child table


 $user =  User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'did_no' => $data['did_no'],
            'mobile_no' => $data['mobile_no'],
            'active' => $data['active'],
            'password' => bcrypt($data['password']),
        ]);


       $ext = Extension::create(['extension_no'=>'1993','description'=>'Test Description']);
        $user->extensions()->save($ext);


but I get below error.

SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into extensions (extension_no, description, updated_at, created_at) values (1993, Test Description, 2019-03-27 14:46:48, 2019-03-27 14:46:48))



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire