检查laravel模型是否已保存或查询已执行 [英] Check if laravel model got saved or query got executed

查看:64
本文介绍了检查laravel模型是否已保存或查询已执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到很多人使用这种方法来检查laravel模型是否已保存.所以现在我想知道这是否是一种安全的方法.

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

检查是否保存了模型

例如:

$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);

以上是检查我的模型是否保存的安全方法吗?

检查查询是否返回结果

例如:

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

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

如果找不到产品,以上内容是否会返回错误?

检查查询是否已执行

例如:

$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);

上面是否检查过是否创建了用户?

推荐答案

检查模型是否已保存

save()将返回布尔值,已保存或保存.因此,您可以执行以下操作:

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');
}

或直接保存在if中:

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

请注意,像您的示例一样,连续两次调用save()没有意义.顺便说一句,许多使模型无法保存的错误或问题总会引发异常.

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...

first()将返回null,因此您的检查有效.但是,您也可以使用firstOrFail(),当什么都没发现时,它会自动抛出ModelNotFoundException:

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();

(find()findOrFail()也是一样)

不幸的是,使用create并不是那么容易.来源:

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;
}

如您所见,它将使用$attributes创建模型的新实例,然后调用save().现在,如果save()在哪里返回true,您将不会知道,因为无论如何您都会得到一个模型实例.例如,您可以做的是检查模型ID(因为只有在保存记录并返回新创建的ID之后才可用)

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');
}

这篇关于检查laravel模型是否已保存或查询已执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆