Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

php - Check if laravel model got saved or query got executed

I've seen alot of people using this way to check if a laravel model got saved. So now I wonder if it is a safe way.

And also can I check if the queries bellow got executed like this

Check if model got saved

Eg:

$myModel = new User();

$myModel->firstname = Input::get('firstname');
$myModel->lastname = Input::get('lastname');

$myModel->save();

//Check if user got saved
if ( ! $myModel->save())
{
  App::abort(500, 'Error');
}

//User got saved show OK message
return Response::json(array('success' => true, 'user_added' => 1), 200);

Is the above a safe way to check whenever my model got saved or not?

Check if query returned a result

Eg:

$UserProduct = Product::where('seller_id', '=', $userId)->first();

if (! $UserProduct)
{
    App::abort(401); //Error
}

Does above return an error if no product where found?

Check if query got executed

Eg:

$newUser = User::create([
        'username' => Input::get('username'),
        'email' => Input::get('email')
]);

//Check if user was created
if ( ! $newUser)
{
    App::abort(500, 'Some Error');
}


//User was created show OK message
return Response::json(array('success' => true, 'user_created' => 1), 200);

Does above check if a user was created?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Check if model got saved

save() will return a boolean, saved or not saved. So you can either do:

$saved = $myModel->save();

if(!$saved){
    App::abort(500, 'Error');
}

Or directly save in the if:

if(!$myModel->save()){
    App::abort(500, 'Error');
}

Note that it doesn't make sense to call save() two times in a row like in your example. And by the way, many errors or problems that would keep the model from being saved will throw an exception anyways...

Check if query returned a result

first() will return null when no record is found so your check works find. However as alternative you could also use firstOrFail() which will automatically throw a ModelNotFoundException when nothing is found:

$UserProduct = Product::where('seller_id', '=', $userId)->firstOrFail();

(The same is true for find() and findOrFail())

Check if query got executed

Unfortunately with create it's not that easy. Here's the source:

public static function create(array $attributes)
{
    $model = new static($attributes);

    $model->save();

    return $model;
}

As you can see it will create a new instance of the model with the $attributes and then call save(). Now if save() where to return true you wouldn't know because you'd get a model instance anyways. What you could do for example is check for the models id (since that's only available after the record is saved and the newly created id is returned)

if(!$newUser->id){
    App::abort(500, 'Some Error');
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...