Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent: How to store model values before saving?

I have a custom function within one of my models. It looks like this:

    public function newWithTeam($data, $team_id = false){

    $levels = Permissions::get_levels();

    $this->email    = $data['email'];
    $this->password = bcrypt($data['password']);
    $this->username = $data['username'];

    $this->save();

    $profile =  new Profile(['name' => $data['name'],'bio'  => $data['bio']]);
    $this->profile()->save($profile);
    }

Here, you can see I store the email, password and username as object properties, before hitting save()

Instead, I'd like to do this in one line, something like:

$this->store(['email' => $data['email], 'password' => $data['password], 'username' => $data['username']]);

$this->save();

I am aware that the create() method exists, but when I use this, the following line $this->profile()->save($profile); does not work properly. I think the create() function does not work the same as save() for some reason! Is there any equivalent to the store() function as above?

like image 336
Mazatec Avatar asked Sep 14 '25 04:09

Mazatec


1 Answers

You can use the fill() method to achieve what you are looking for.

But before using it, you should know a few things.

Laravel models are protected against mass-assignment by security reasons, to use the fill() method you will need to define what properties of your model can be filled using the fillable or the guarded properties.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserModel extends Model
{
    protected $fillable = ['email', 'password', 'username'];

    public function newWithTeam($data, $team_id = false){

        $levels = Permissions::get_levels();

        $this->fill([
            'email'    => $data['email'],
            'password' => bcrypt($data['password']),
            'username' => $data['username']
        ]);

        $this->save();

        $profile = new Profile([
            'name' => $data['name'],
            'bio' => $data['bio']
        ]);

        $this->profile()->save($profile);
    }
}

The fillable property functions like a white-list for mass-assignment. If you want the other way, you can use the guarded property that will function like a black-list. Which means that every column listed within the guarded property will not be available for mass-assignment and everything else will, it's your choice.

About your last statement, if you look at the implementation of the create() method you will find that it accepts a regular php array, while the save() method will accept an Eloquent Model instance. That's why create() will not work receiving your $profile variable.

I hope this helps.

like image 84
Álvaro Guimarães Avatar answered Sep 15 '25 18:09

Álvaro Guimarães