Reputation: 13
As you see below i have two table.
1.Posts Table (record the in and out of the table)
id lecture_name roomkey created_at updated_at user_id
1 Ee Vonne Ng L02 2019-01-22 2019-02-03 1
2 Ee Vonne Ng L03 2019-01-22 2019-01-23 1
2.key table (keynos)
id roomkey status created_at updated_at
1 L01 0 2019-01-22 2019-02-04
2 L02 1 2019-01-22 2019-01-22
3 L03 1 2019-01-22 2019-01-22
4 L04 0 2019-01-22 2019-02-04
I am having a problem as when i want to delete a record in posts,yes,i am successful, but i want it to update also the keyno status to 0, so that it act as this key is active. But i don't know what have i missed as i can delete but i can't update the keyno status. No error is show, but my status is not update. How anyone can help me or give me some advise TT
Below is my code.
1=Key in use 0=not in use
1.Post controller
*/
public function destroy($id)
{
$post = Post::find($id);
$post->delete();
if($post->delete()){
$post = Post::find($id);
$key = Keyno::all();
$postroomkey = $post->roomkey;
Keyno::where('roomkey','=',$postroomkey)
->update(['status' => 0]);
}
}
Upvotes: 1
Views: 545
Reputation: 2464
You're running $post->delete();
twice.
The first one will delete the post but second one doesn't actually do anything which means it returns false, because it can't find the post.
This means that your code in the if
statement doesn't actually run. Change it to this:
public function destroy($id)
{
$post = Post::find($id);
$postroomkey = $post->roomkey;
if($post->delete()){
$key = Keyno::all();
Keyno::where('roomkey','=',$postroomkey)
->update(['status' => 0]);
}
}
"Trying to get property 'roomkey' of non-object"
This is probably due to $post
not being an object anymore after you delete it, fetch roomkey
before deleting it. Updated answer.
Upvotes: 1