Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel with Eloquent doesn't save model properties on database

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();

    ?>
like image 703
jcobo1 Avatar asked Sep 10 '14 19:09

jcobo1


2 Answers

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 = ....

like image 104
lowerends Avatar answered Oct 01 '22 17:10

lowerends


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();
like image 38
c-griffin Avatar answered Oct 01 '22 17:10

c-griffin