Practical Laravel: Develop clean MVC web applications by Vallejo Paola & Correa Daniel

Practical Laravel: Develop clean MVC web applications by Vallejo Paola & Correa Daniel

Author:Vallejo, Paola & Correa, Daniel
Language: eng
Format: epub
Published: 2022-02-12T00:00:00+00:00


$newProduct = new Product();

$newProduct->setName($request->input('name'));

$newProduct->setDescription($request->input('description'));

$newProduct->setPrice($request->input('price'));

$newProduct->setImage("game.png");

$newProduct->save();

Next, we create a newProduct instance. Then, we set the newProduct attributes based on values collected from the form. We use the request->input method to retrieve the form inputs. We also set a default image (game.png). In the end, we invoke the save method, which inserts the object data into the database. The model's created_at and updated_at timestamps will automatically be set when the save method is called, so there is no need to set them manually.

Note: setting one by one each model attribute presents an issue. When a model has many attributes, it implies manually calling several setters. We will see an alternative at the end of this chapter.

Analyze Code



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.