Laravel with Eloquent doesn't save model properties on database

10,818

Solution 1

As described at http://laravel.com/docs/eloquent#mass-assignment, you need the $fillable property set for all the properties you want to be mass-assignable. You should therefore uncomment the line starting with protected $fillable = ....

Solution 2

You don't need to specify these in the constructor. You can use mass-assignment.

Model:

class User extends Eloquent {

protected $fillable = ['name', 'suname', 'mobile', 'phone', 'mail', 'address'];

}

Controller:

$user = new User(['name' => 'John', 'surname' => 'Doe', 'mobile' => '685412578', 'phone' => '9354784125','mail' => '[email protected]', 'address' => 'Portland']);

$user->save();

Solution 3

You don't need to specify the column names that you're storing for the particular model in the model itself. One of the purposes of the ORM is to remove this responsibility from you. What you have also obviously won't work because Eloquent's getters / setters are specified in a parent class, yet you have the properties themselves defined as private which is inaccessible from any parent / child scope.

My suggestion: completely remove these lines from your model:

private $name;
private $surname;
private $mobile;
private $phone;
private $mail;
private $adress;

Remove your constructor in there as well. If you want to create an instance and store it in the database, you can do this:

$user = User::create(array(
    'name' => 'John',
    'surname' => 'Doe',
    // ... etc
));

And later you can easily access the properties of this instance:

echo $user->name;
Share:
10,818
jcobo1
Author by

jcobo1

Updated on June 30, 2022

Comments

  • jcobo1
    jcobo1 almost 2 years

    I'm buiding an web app using php laravel framework. When I save the model on the database it makes an insert but doesn't save model properties. I can't see how to fix because the laravel log doesn't show any mistake. Any idea?

    There's the model:

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'test';
    //  protected $fillable = array('name', 'surname', 'mobile', 'phone', 'mail', 'adress');
    
    // Model properties
    private $name;
    private $surname;
    private $mobile;
    private $phone;
    private $mail;
    private $adress;
    
    // Model constructor
    function __construct($name, $surname, $mobile, $phone, $mail, $adress) {
        $this->name = $name;
        $this->surname = $surname;
        $this->mobile = $mobile;
        $this->phone = $phone;
        $this->mail = $mail;
        $this->adress = $adress;
    }
    

    And there's php code which create User object and save it to the database

    <?php
    
    // Create an object using model's constructor
    $user = new User('John', 'Doe', 685412578, 9354784125, '[email protected]', 'Portland');
    
    // Show some properties
    echo '<p>'.$user->getName().'</p>';
    echo '<p>'.$user->getSurname().'</p>';
    
    // Saving model on database
    $user->save();
    
    ?>
    

    When I execute the php code show on screen the model properties and makes an insert but doesn't save the properties. Would be great if someone can help me :)


    There's is the solution (if someone has the same problem or similar)

    Model:

        protected $table = 'test';
    
        // Fields on databse to save model properties
        protected $fillable = array('name', 'surname', 'mobile', 'phone', 'mail', 'adress');
    
        // Model properties
        private $name;
        private $surname;
        private $mobile;
        private $phone;
        private $mail;
        private $adress;
    

    And php code:

     <?php
    
            // Create an object
                        $user = User::create(array(
                            'name'=>'John', 
                            'surname'=>'Doe', 
                            'mobile'=>685412578, 
                            'phone'=>9354784125, 
                            'mail'=>'[email protected]', 
                            'adress'=>'Portland'
                            ));
    
            // Show some properties
            echo '<p>'.$user->name.'</p>';
            echo '<p>'.$user->surname.'</p>';
    
            // Saving model on database
            $user->save();
    
        ?>
    
  • Rohit Gupta
    Rohit Gupta over 8 years
    Please explain your answer.