B
B
BonBon Slick2017-02-03 15:19:53
Laravel
BonBon Slick, 2017-02-03 15:19:53

Which is better to use $model->create() or $model->save()?

I know that create te wrapper save .

public static function create(array $attributes = [])
{
    $model = new static($attributes);
    $model->save();
    return $model;
}

What is better to use when instantiating? Why?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Pochepko, 2017-02-03
@DJZT

Depends on your specific task. Think for yourself.

A
Anton, 2017-02-03
@Yadalay

I find it's better and easier to use create() as this method allows for quick data entry and works well with data validation.
For example:
Agree, this is much faster than, for example:

$article = new Article();
$article->title = $request->input('title');
$article->category_id = $request->input('category_id');
$article->content = $request->input('content');
$article->save();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question