dimanche 18 octobre 2015

how to insert two table related, in laravel 5.1

I need insert my information to information table and then insert information_id to advertise table , in one step

I get this error :

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (baroot_laravel.advertise, CONSTRAINT advertise_information_id_foreign FOREIGN KEY (information_id) REFERENCES information (id)) (SQL: insert into advertise (jobs_sub_category_id, area_id, description, updated_at, created_at) values (, , , 2015-10-18 07:04:13, 2015-10-18 07:04:13))

my codes :

Advertise_Model :

class Advertise_Model extends Model
{
    protected $table = 'advertise';
    protected $fillable = ['jensiat','information_id','wage_range_id','jobs_sub_category_id','jobs_type_id','age_range_id','experience_id','confirmed','description','city_id','area_id','academic_degree_id'];

    public function Information()
    {
        return $this->hasOne('App\Information_Model','information_id','id');
    }

}

Information_Model :

class Information_Model extends Model
{
    protected $table = 'information';
    protected $fillable = ['person_name','company_name','company_phone','person_mobile','location_id','person_id','email','website'];
}

And my save to db controller :

public function AdminStore(Request $request)
    {
        Advertise_Model::create($request->all())->with(Information_Model::create($request)->all());
        //Information_Model::create($request->all());
        //Advertise_Model::create($request->all());
        return view('Administrator.Advertise.index');
    }

thanks for attention



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire