user13746
user13746

Reputation: 870

Laravel Model::create or Model->save()

I'm wondering what approach should I use for storing data in the database;

First approach

$product = Product::create($request->all());

In my Product model I have my $filable array for mass assigment

Second approach

    $product = new Product();
    $product->title = $request->title;
    $product->category = $request->category;
    $product->save();

Is any of these two "Better solution"? What should I generally use?

Thank you for you advice

Upvotes: 22

Views: 46097

Answers (1)

Wader
Wader

Reputation: 9883

Personal preference.

Model::create() relies on mass assignment which allows you to quickly dump data into a model, it works nicely hand-in-hand with validation rather than having to set each of the model properties manually. I've more recently started using this method over the latter and can say its a lot nicer and quicker.

Don't forget you also have a range of other mass assignment functions create(), update() and fill() (Possibly more).

Upvotes: 21

Related Questions