Laravel with Eloquent doesn't store model properties in the database

I am creating a web application using php laravel framework. When I save the model to the database, it inserts but does not save the properties of the model. I can't figure out how to fix it because the laravel log doesn't show any error. Any idea?

There's a 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 is php code that creates a User object and stores it in the database

<?php

// Create an object using model constructor
$user = new User('John', 'Doe', 685412578, 9354784125, 'j@doe.com', 'Portland');

// Show some properties
echo '<p>'.$user->getName().'</p>';
echo '<p>'.$user->getSurname().'</p>';

// Saving model on database
$user->save();

?>

      

When I run the php code, you are showing the properties of the model and inserting into it, but not saving the properties. It would be great if someone can help me :)


There is a solution (in case anyone has the same problem or something 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'=>'j@doe.com', 
                        'adress'=>'Portland'
                        ));

        // Show some properties
        echo '<p>'.$user->name.'</p>';
        echo '<p>'.$user->surname.'</p>';

        // Saving model on database
        $user->save();

    ?>

      

+3


source to share


5 answers


As described at http://laravel.com/docs/eloquent#mass-assignment , you need to set a property $fillable

for all properties you want to assign to a mass. Therefore, you must uncomment the line starting with protected $fillable = ...

.



+2


source


You don't need to specify them 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' => 'j@doe.com', 'address' => 'Portland']);

$user->save();

      

+1


source


You don't need to specify the column names that you store for a particular model in the model itself. One of the goals of the ORM is to remove this responsibility from you. What you also obviously won't work, because the Eloquent getters / setters are specified in the parent class, but you have the properties themselves defined as private

that are not available 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 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 that instance:

echo $user->name;

      

0


source


The class Eloquent

that extends your models cannot reach the properties because you are declaring them as private

. You cannot directly specify properties of the model, or you can declare them as protected

or public

, then Eloquent will have access to them and will be able to store them in your database.

0


source


class User extends Model {

    protected $table = 'users';

    public $timestamps = false;

}

      

0


source







All Articles