3

Im trying to get exception if update method failed i just check this try catch block it dont return any exception because this project id do not exist in my database i have only about hundred records.

 try {
    $project =  DB::table('project')
                  ->where('prj_id',  '987654' )
                  ->update([
                    'prj_status' => 'open',
                    'prj_updated_date' => Carbon::now()
                  ]);

 }catch(\Exception $e){

     dd($e);
 }

3 Answers 3

5

An update on none-existing row do not fail in SQL. If you run a query like UPDATE foo SET bar = 'foobar' WHERE 1 = 2; your database would be happy to do the job and report back 0 rows updated.

You will have to check the value of $project to see if the update did update any rows

Sign up to request clarification or add additional context in comments.

2 Comments

is there any way i can check why this table failded to update all the values are there when in dump. the problem is return type is just true or false. $project = DB::table('project') ->where('prj_id', '987654' ) ->update([ 'prj_status' => 'open', 'prj_updated_date' => Carbon::now() ]); if ( !$project) { return " project update have error ";
If you get false and no exception it is because the row don't exists, if something is wrong with the query or with the database you will get an exception. If you want to be sure that this always is the case you could try to find the row in the database if you get a false back from the update and check if you did find any rows
1

If you're using Laravel 5, use the model to dictate what will be stored in the DB eventually.

So, using your project model, you'll have something like:

$project = Project::findOrFail('987654')
    ->update([
        'prj_status' => 'open',
        'prj_updated_date' => Carbon::now()
    ]);

You'll get a not found exception if the id of what you're looking for doesn't exist.

3 Comments

Hi but im using fluent queries throughout in this project
Is there any reason you went with that as opposed to using Eloquent? Running fluent queries won't throw an exception when you try to update a non-existent row. It will just return null. So you need to check the value of $project and throw an exception based on that.
There are many join operation on different table so instead of making relationships in each modal i just use fluent queries it feel easy to me. The scenario is, there are more tables and fields to update some time table failed to update and update method just return true false and i want to check any exception why a particular table failed to update
0
$projects_updated = DB::table('project')
    ->where('prj_id',  '987654' )
    ->update([
        'prj_status' => 'open',
        'prj_updated_date' => Carbon::now()
    ]);

if($projects_updated) {
    // n rows updated, do something
}
else {
    // nothing updated
}

1 Comment

thanks calin i already tried this but i want to check error if it failed. this only return true false.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.