alex
alex

Reputation: 7895

laravel : access a property without declaring it

in some laravel based packages there is a reference to properties that are not declared but are the name of that model's table column .

tickets table:

  id 
  name
  content

and Ticket model:

 class Ticket extends Model {

//there is no "protected $content;" defined inside model
$this->content = foo;
 ......

}

does model properties are created dynamically based on model table columns?

Upvotes: 1

Views: 357

Answers (2)

Maurice Kuria
Maurice Kuria

Reputation: 59

Model attributes are implicitly declared depending on the mapped database table columns.

youcan explicitly declare the matching table

protected $table = 'tablename';

Upvotes: 0

Damian Mąsior
Damian Mąsior

Reputation: 164

You need to set $guarded or $fillable https://laravel.com/docs/5.1/eloquent#mass-assignment in your model. For example:

class Ticket extends model {
  protected $guarded =[];
}

This let's you access all table columns and update them.

class Ticket extends model {
  protected $fillable = ['name', 'content'];
}

This let's you access columns name and content and udpate only them.

Upvotes: 2

Related Questions