Skip to content
Advertisement

Does laravel save() method throws exception?

I have a little question here. This is my code:

DB::beginTransaction();
    try{
        $created = new TransportTypeColumn();
        $created->name = $translated_ids[0];
        if(!$created->save())
            throw new Exception("failed saving transport type column");
        DB::commit();
        return response()->json(['success'=>'Property has been created successfully', 'data'=>$created],200);

    }catch(Exception $e){
        DB::rollback();
        return response()->json(['error'=>'Something went wrong, please try later.'], 500);
    }

so do I need this piece of code ? :

if(!$created->save())
            throw new Exception("failed saving transport type column");

or does it throws exception itself if save() function doesn’t succeed?

Advertisement

Answer

save returns a boolean, you need to check if the save was successfull or not, but unless there is a mysql error you won’t get any exceptions.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement