vendredi 29 janvier 2016

Laravel 5 database table relationships

I am starting with learning the database table relationships in Laravel 5. As a cakePHPer something just work a little bit different. My first test is this...

Table containing different sql_types:

class ElementFieldType extends Model {

    protected $fillable = ['name', 'sql_type'];

}

Table containing fields linked to the sql_types in the ElementFieldType Model (element_field_type_id is a foreign key):

class ElementField extends Model {

    protected $fillable = ['api', 'element_field_type_id', 'label', 'type'];

    public function type() {
        return $this->hasOne('App\Models\ElementFieldType');
    }

}

In my controller after finding the ElementField, I want to access the sql_type using:

$field = ElementField::find($id);
echo $field->type->sql_type;

But I get below error, meaning to what I can tell, that there should be an element_field_id key in ElementFieldType, but that cannot be right.

What am I doing wrong?

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'element_field_types.element_field_id' 
in 'where clause' (SQL: select `sql_type` from `element_field_types` 
where `element_field_types`.`element_field_id` = 1 and `element_field_types`.`element_field_id` 
is not null limit 1)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire