简体   繁体   中英

How deleted at working with laravel with different foreign key

Here is my code:

I have created a table that is image_upload . In this table there are 2 colou that is post_id , post_name , deleted_at , created_at , updated_at . Post id belong to post table.

When user deletes a post, I am trying to use soft delete in laravel and update the deleted_at in image_upload table. I have added below code in my model

POST MODEL:

public function imageLinks()
    {
        return $this->hasMany(ImageUpload::class, 'post_id'); 
    } 

When I delete a post, I get this error. My column name is post_id but it will take id . I don't understand what exact issue.

"message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update image_upload set deleted_at = 2020-07-03 21:28:14, updated_at = 2020-07-03 21:28:14 where id is null)",

I suggest that you add this couple lines on your Delete_post() function: For example

$post=post::where('id',=,$id)->get(); 
$post->delete();
  
//And you can 

$images=imageUpload::where('post_id',=,$post['id'])->delete();

//Or 

$post->imageLinks()->delete();

But I don't understand why you have added post_name in the images table that doesn't make sense because you can get it using relationships.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM