Best approach to Insert Data in Mysql through Laravel - laravel

Below are the two ways to insert data in MySql through laravel
Way 1:
$post = Post::create([
'title' => $request->input('title'),
'body' => $request->input('body')
]);
Way 2:
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
I just want to know which approach is better and why? Could anyone please tell which approach is better?

Model::create is a simple wrapper around this, if you look at its implementation:
public static function create(array $attributes = [])
{
$model = new static($attributes);
$model->save();
return $model;
}
save()
save() method is used both for saving new model, and updating existing one. here you are creating new model or find existing one, setting its properties one by one and finally saves in database
save() accepts a full Eloquent model instance
$comment = new App\Comment(['message' => 'A new comment.']);
$post = App\Post::find(1);`
$post->comments()->save($comment);
create()
while in create method you are passing array, setting properties in model and persists in database in one shot.
create() accepts a plain PHP array
$post = App\Post::find(1);
$comment = $post->comments()->create([
'message' => 'A new comment.',
]);

Related

Difference between save, fill, create in laravel eloquent

So i am new to laravel,
I don't know the difference between save, fill and create in laravel eloquent.
can anybody describe it?
save() method is when you already assign a value to a model instance. for example
$user = new User;
$user->name = "anything";
$user->save();
dd($user); // {'id': 1, 'name': 'anything'}
save() can also for update a value
$user = User::first(); // { 'id': 1, 'name': "anything" }
$user->name = "change";
$user->save();
dd($user); // {id: 1, name: "change"}
create() is a static function, need array parameter to create a new record
$user = User::create(['name' => 'new user']);
dd($user); // {id: 2, name: 'new user'}
fill() is same like save() method but without create a new record. need to create a new instance before can use fill()
$user = new User;
$user->fill(['name' => 'last user']);
echo count(User::all()); // We count users on DB and result is 2
$user->save(); // This will save 'last user' to DB
echo count(User::all()); // result is 3
The are kinda the same, but not quite.
//Creating User with fill()
$user = new User();
$user->fill($validatedUserData);
$user->save();
//Creating User with create();
$user = User::create($validatedUserData);
As you can see, create can do all of 3 lines(with fill function) with just one line. That's essentially a quality of life feature.
Both of this shown above does the same thing.
With that being said, you'd probably want to use create() when making a new entry. But for updating an item, it's better to just do something like this:
public function update(User $user, Request $request){
$validatedUserData = $request->validate([
//Validation logic here
]);
$user->fill($validatedUserData);
$user->save();
}
Note: You need to mark fields as fillable to use those fields with create() or fill().

laravel 5.7 how to pass request of controller to model and save

I am trying to pass $request from a function in controller to a function in model.
THis is my controller function:
PostController.php
public function store(Request $request, post $post)
{
$post->title = $request->title;
$post->description = $request->description;
$post->save();
return redirect(route('post.index'));
}
how save data in model Post.php?
I want the controller to only be in the role of sending information. Information is sent to the model. All calculations and storage are performed in the model
Thanks
You can make it even easier. Laravel has it's own helper "request()", which can be called anywhere in your code.
So, generally, you can do this:
PostController.php
public function store()
{
$post_model = new Post;
// for queries it's better to use transactions to handle errors
\DB::beginTransaction();
try {
$post_model->postStore();
\DB::commit(); // if there was no errors, your query will be executed
} catch (\Exception $e) {
\DB::rollback(); // either it won't execute any statements and rollback your database to previous state
abort(500);
}
// you don't need any if statements anymore. If you're here, it means all data has been saved successfully
return redirect(route('post.index'));
}
Post.php
public function postStore()
{
$request = request(); //save helper result to variable, so it can be reused
$this->title = $request->title;
$this->description = $request->description;
$this->save();
}
I'll show you full best practice example for update and create:
web.php
Route::post('store/post/{post?}', 'PostController#post')->name('post.store');
yourform.blade.php - can be used for update and create
<form action='{{ route('post.store', ['post' => $post->id ?? null]))'>
<!-- some inputs here -->
<!-- some inputs here -->
</form>
PostController.php
public function update(Post $post) {
// $post - if you sent null, in this variable will be 'new Post' result
// either laravel will try to find id you provided in your view, like Post::findOrFail(1). Of course, if it can't, it'll abort(404)
// then you can call your method postStore and it'll update or create for your new post.
// anyway, I'd recommend you to do next
\DB::beginTransaction();
try {
$post->fill(request()->all())->save();
\DB::commit();
} catch (\Exception $e) {
\DB::rollback();
abort(500);
}
return redirect(route('post.index'));
}
Based on description, not sure what you want exactly but assuming you want a clean controller and model . Here is one way
Model - Post
class Post {
$fillable = array(
'title', 'description'
);
}
PostController
class PostController extend Controller {
// store function normally don't get Casted Objects as `Post`
function store(\Request $request) {
$parameters = $request->all(); // get all your request data as an array
$post = \Post::create($parameters); // create method expect an array of fields mentioned in $fillable and returns a save dinstance
// OR
$post = new \Post();
$post->fill($parameters);
}
}
I hope it helps
You need to create new model simply by instantiating it:
$post = new Post; //Post is your model
then put content in record
$post->title = $request->title;
$post->description = $request->description;
and finally save it to db later:
$post->save();
To save all data in model using create method.You need to setup Mass Assignments when using create and set columns in fillable property in model.
protected $fillable = [ 'title', 'description' ];
and then call this with input
$post = Post::create([ 'parametername' => 'parametervalue' ]);
and if request has unwanted entries like token then us except on request before passing.
$post = Post::create([ $request->except(['_token']) ]);
Hope this helps.
I find to answer my question :
pass $request to my_method in model Post.php :
PostController.php:
public function store(Request $request)
{
$post_model = new Post;
$saved = $post_model->postStore($request);
//$saved = response of my_method in model
if($saved){
return redirect(route('post.index'));
}
}
and save data in the model :
Post.php
we can return instance or boolean to the controller .
I returned bool (save method response) to controller :
public function postStore($request)
{
$this->title = $request->title;
$this->description = $request->description;
$saved = $this->save();
//save method response bool
return $saved;
}
in this way, all calculations and storage are performed in the model (best way to save data in MVC)
public function store(Request $request)
{
$book = new Song();
$book->title = $request['title'];
$book->artist = $request['artist'];
$book->rating = $request['rating'];
$book->album_id = $request['album_id'];
$result= $book->save();
}

Proper syntax for Laravel Eloquent insert/update

I have seemingly simple problem that I still can't solve after days of browsing around.
Say I have Debitur model.
class Debitur extends Model
{
protected $table = 'debiturs';
protected $fillable = [
'name', 'address',
];
public function debiturWife(){
return $this->hasOne('App\DebiturWife');
}
}
And I have a DebiturWife model.
class DebiturWife extends Model
{
protected $table = 'debitur_wifes';
protected $fillable = [
'nama', 'address', 'photo',
];
public function debitur(){
return $this->belongsTo('App\Debitur');
}
}
I can get Debitur and DebiturWife data in the DebiturController like this:
return Debitur::with('debiturWife')->find($debitur_id);
Now I want to create a new Debitur and DebiturWife, how do I do that?
Something like
$debitur = new Debitur;
$debitur->name = $request->name;
$debitur_wife = new Debitur.DebiturWife; //obviously doesn't work
$debitur = new Debitur;
$debitur->name = $request->name;
$debitur->save();
$debitur_wife = new DebiturWife([
'name' => 'Foo'
]);
$debitur->debiturWife()->save($debitur_wife);
Documentation
What if later I want to update DebiturWife based on Debitur's id?
$debitur = Debitur::with('debiturWife')->find($id);
$debitur->debiturWife->name = 'new name';
$debitur->debiturWife->address = 'new address';
$debitur->save();
You could do this way:
$debitur = new Debitur;
$debitur->name = $request->name;
$debitur->save();
$debitur->debiturWife()->create([
'name' => $request->name,
'address' => $request->address,
'photo' => $request->photo
]);
Another way of doing this would be —
$debiturWife = new DebiturWife();
$debiturWife->name = $request->name;
$debiturWife->address = $request->address;
$debiturWife->photo = $request->photo;
$debitur->debiturWife()->save($debiturWife);
The debitur_id will be automatically set. The only difference between the create & save method is that the create method accepts a plain PHP array whereas the save method accepts an instance of DebitureWife
See the documentation for better understanding.

Create Relationship inside the create function

I have a model that has a one to many relationship to the versions of the description.
In my Controller
$tag = Tags::create([
'name' => $request->get('name'),
'user_id' => \Auth::id(),
]);
$tag->update([
'content' => $request->get('description')
]);
In my Model:
public function setContentAttribute(string $value)
{
$this->versions()->create([
'user_id' => \Auth::id(),
'value' => $value
]);
}
So I can't put content directly as an attribute in the create method because there is no Model right now.
But is it possible to overwrite the create Method?
When I try to overwrite something like this in my Model it will do an infinity loop
public static function create($attr) {
return parent::create($attr);
}
So my question is if it is possible to have something like this:
$tag = Tags::create([
'name' => $request->get('name'),
'user_id' => \Auth::id(),
'content' => $request->get('content')
]);
and in the Model:
public static function create($attr) {
$value = $attr['content'];
$attr['content'] = null;
$object = parent::create($attr);
$object->content = $value;
$object->save();
return $object;
}
Update
I didn't overwrite the create method but called it customCreate. So there is no infinity loop anymore and I can pass all variables to the customCreate function that handles the relationships for me.
Solution
After reading the changes from 5.3 to 5.4 it turns out that the create method was moved so you don't have to call parent::create() anymore.
The final solution is:
public static function create($attr) {
$content = $attr['content'];
unset($attr['content']);
$element = static::query()->create($attr);
$element->content = $content;
$element->save();
return $element;
}
I don't see why not and you could probably implement a more general approach? Eg. checking if set{property}Attribute() method exists, if it does - use it to assign a value, if it doesn't - use mass assigning.
Something like:
public static function create($attr) {
$indirect = collect($attr)->filter(function($value, $property) {
return method_exists(self::class, 'set' . camel_case($property) . 'Attribute');
});
$entity = parent::create(array_diff_key($attr, $indirect->toArray()));
$indirect->each(function($value, $property) use ($entity) {
$entity->{$property} = $value;
});
$entity->save();
return $entity;
}
I haven't really tested it but it should work. I use something like this in one of my Symfony apps.

Laravel convert an array to a model

i have an array as follows
'topic' =>
array (
'id' => 13,
'title' => 'Macros',
'content' => '<p>Macros. This is the updated content.</p>
',
'created_at' => '2014-02-28 18:36:55',
'updated_at' => '2014-05-14 16:42:14',
'category_id' => '5',
'tags' => 'tags',
'referUrl' => '',
'user_id' => 3,
'videoUrl' => '',
'useDefaultVideoOverlay' => 'true',
'positive' => 0,
'negative' => 1,
'context' => 'macros',
'viewcount' => 60,
'deleted_at' => NULL,
)
I would like to use this array and convert/cast it into the Topic Model . Is there a way this can be done.
thanks
Try creating a new object and passing the array into the constructor
$topic = new Topic($array['topic']);
For creating models from a single item array:
$Topic = new Topic();
$Topic->fill($array);
For creating a collection from an array of items:
$Topic::hydrate($result);
Here is a generic way to do it, not sure if there is a Laravel-specific method -- but this is pretty simple to implement.
You have your Topic class with its properties, and a constructor that will create a new Topic object and assign values to its properties based on an array of $data passed as a parameter.
class Topic
{
public $id;
public $title;
public function __construct(array $data = array())
{
foreach($data as $key => $value) {
$this->$key = $value;
}
}
}
Use it like this:
$Topic = new Topic(array(
'id' => 13,
'title' => 'Marcos',
));
Output:
object(Topic)#1 (2) {
["id"]=>
int(13)
["title"]=>
string(6) "Marcos"
}
It seems that you have data of an existing model there, so:
First, you can use that array to fill only fillable (or not guarded) properties on your model. Mind that if there is no fillable or guarded array on the Topic model you'll get MassAssignmentException.
Then manually assign the rest of the properties if needed.
Finally use newInstance with 2nd param set to true to let Eloquent know it's existing model, not instantiate a new object as it would, again, throw an exception upon saving (due to unique indexes constraints, primary key for a start).
.
$topic = with(new Topic)->newInstance($yourArray, true);
$topic->someProperty = $array['someProperty']; // do that for each attribute that is not fillable (or guarded)
...
$topic->save();
To sum up, it's cumbersome and probably you shouldn't be doing that at all, so the question is: Why you'd like to do that anyway?
Look at these two available methods in L5 newInstance and newFromBuilder
e.g with(new static)->newInstance( $attributes , true ) ;
I would likely create the new instance of the object and then build it that way, then you can actually split some useful reusable things or defaults into the model otherwise what's the point in pushing an array into a model and doing nothing with it - very little besides for normalization.
What I mean is:
$topic = new Topic();
$topic->id = 3489;
$topic->name = 'Test';
And the model would simply be a class with public $id;. You can also set defaults so if you had like resync_topic or whatever property, you can set it as 0 in the model rather than setting 0 in your array.
I came across this question looking for something else. Noticed it was a bit outdated and I have another way that I go about handling the OPs issue. This might be a known way of handling the creation of a model from an array with more recent versions of Laravel.
I add a generic constructor to my class/model
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
}
Then when I want to create a new instance of the model from an array I make a call like this
$topic = new Topic($attrs);
// Then save it if applicable
$topic->save(); // or $topic->saveOrFail();
I hope someone finds this helpful.

Resources