Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a query was successful laravel

Tags:

php

laravel

I define a query to delete a table in database. After that i want to show a message that the query was run successfully. How can I check it with an if statement?

$query = DB::table('user_users')->delete();     

return view('datenbank');
like image 756
Fabian2000 Avatar asked Oct 20 '25 16:10

Fabian2000


1 Answers

When you use delete with the query builder it will return the number of affected rows.

Your if statement would just need to look something like:

$query = DB::table('user_users')->delete(); 

if ($query) {
    //query successful
}

If you want to be more explicit you could do if ($query > 0) {}

If anything goes wrong with the query (an error) it will throw an Exception which will mean that no rows have been affected.

like image 71
Rwd Avatar answered Oct 23 '25 06:10

Rwd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!