jcobo1
jcobo1

Reputation: 1170

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

    ?>

Upvotes: 6

Views: 12324

Answers (5)

user5761094
user5761094

Reputation: 1

class User extends Model {

    protected $table = 'users';

    public $timestamps = false;

}

Upvotes: 0

Jeff Lambert
Jeff Lambert

Reputation: 24661

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;

Upvotes: 1

Adrien Delessert
Adrien Delessert

Reputation: 1360

The Eloquent class that your models extend can't reach the properties because you're declaring them as private. You can either not directly specify the properties on the model, or you can declare them as either protected or public, then Eloquent will have access to them and be able to save them to your database.

Upvotes: 0

c-griffin
c-griffin

Reputation: 3026

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

Upvotes: 1

lowerends
lowerends

Reputation: 5267

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

Upvotes: 2

Related Questions