Hello guys,
I have problem with overwriting the table, because I combine it with another using a foreign key.
I am trying to do draggable table, and when I drag row I get error 500 in my console:
"message": "SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint (`photography`.`photos`, CONSTRAINT `photos_business_id_foreign` FOREIGN KEY (`business_id`) REFERENCES `photography`.`businesses` (`id`)) (SQL: truncate `businesses`)",
I have two tables - photos, and business.
In Photo model it looks:
class Photo extends Model
{
protected $fillable = [
'filename',
'business_id',
];
public function business()
{
return $this->belongsTo(Business::class, 'business_id');
}
}
In Business model:
class Business extends Model
{
protected $fillable = [
'title',
'description',
'order',
'visiblity',
'lang'
];
public function photos()
{
return $this->hasMany(Photo::class);
}
}
Its all ok, but I need help with BusinessController function:
public function updateAll(Request $request)
{
Business::truncate();
foreach($request->businesses as $business){
Business::create([
'id' => $business['id'],
'title' => $business['title'],
'description' => $business['description'],
'order' => $business['order'],
'visiblity' => $business['visiblity'],
'lang' => $business['lang'],
]);
}
return response('Update Successful.',200);
}
Here is foreign key migration fragment:
...
public function up()
{
Schema::table('photos', function(Blueprint $table){
$table->integer('business_id')->unsigned()->change();
$table->foreign('business_id','photos_business_id_foreign')->references('id')->on('businesses');
});
}
...
How can I handle photos? I need update business_id, but don't know how... So much thanks!