mysql - Can't store a new row - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails -
my clients table , users table child tables of businesses table. both clients , users table contain business_id column refers id column of businesses table:
public function up() { schema::create('clients', function(blueprint $table) { $table->increments('id'); $table->integer('business_id')->unsigned(); $table->foreign('business_id')->references('id')->on('businesses'); $table->string('first_name'); $table->string('last_name'); etc… i able store new user, works fine, keep getting error when trying store new row clients table :
sqlstate[23000]: integrity constraint violation: 1452 cannot add or update child row: foreign key constraint fails (
laravel.clients, constraintclients_business_id_foreignforeign key (business_id)
client.php model
/** * attributes mass assignable. * * @var array */ protected $fillable = ['business_id', 'first_name', 'last_name']; /** * business user works. * * @return \illuminate\database\eloquent\relations\belongsto */ public function businesses() { return $this->belongsto('app\business'); } business.php model
/** * clients use business. * * @return \illuminate\database\eloquent\relations\hasmany */ public function clients() { return $this->hasmany('app\client','business_id'); } clientscontroller.php
public function store(clientsrequest $request) { var_dump(auth::user()->business_id); $clientinfo = request::all(); client::create($clientinfo); $client = new client; $client->business_id = auth::user()->business_id; $client->first_name = $clientinfo['first_name']; $client->last_name = $clientinfo['last_name']; //$client->save(); $business->clients()->save($client); $last_inserted_id = $data->id; return redirect('clients/'.$last_inserted_id.'/edit'); the thing i’m not sure way i’m retrieving business_id (from users table), via auth. when var_dump value, :
string(1) "7" i know id value 7 in businesses table, that’s i’m looking for, not sure if should converted integer.
and, not sure if have save using :
$client->save(); or
$business->clients()->save($client); thanks
both following correct ways, first 1 more laravel later,
public function store(clientsrequest $request) { $client = new client([ 'first_name' => $request->get('first_name'), 'last_name' => $request->get('last_name'), ]); $client = business::findorfail(auth::user()->business_id) ->clients() ->save($client); return redirect('clients/'.$client->id.'/edit'); } p.s. above business_id automatically added client eloquent
other way did, inserting business_id manually.
public function store(clientsrequest $request) { $clientinfo = request::all(); $client = client::create([ 'business_id' => auth::user()->business_id; 'first_name' => $clientinfo['first_name']; 'last_name' => $clientinfo['last_name']; ]); return redirect('clients/'.$client->id.'/edit'); } you receiving error because of following statement in code,
client::create($clientinfo); above statement attempt create database entry without business_id foreign key , should supplied wasn't , hence error.
and don't have worry type-conversion between int<->string, php @ it.
Comments
Post a Comment