Truncate table with foreign key

Posted 3 months ago by ziben69

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 = [
public function business()
        return $this->belongsTo(Business::class, 'business_id');

In Business model:

class Business extends Model
    protected $fillable = [

    public function photos()
        return $this->hasMany(Photo::class); 

Its all ok, but I need help with BusinessController function:

    public function updateAll(Request $request)

        foreach($request->businesses as $business){
                '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){

How can I handle photos? I need update business_id, but don't know how... So much thanks!

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.