According to Dayle Rees
All foreign key columns follow a similar naming convention. The singular form of the related model appended with _id.
Consider this migration:
class CreateTables extends Migration {
// ...
public function up()
{
Schema::create('artists', function($table) {
$table->increments('id');
$table->string('name',64);
$table->timestamps();
});
Schema::create('albums', function($table) {
$table->increments('id');
$table->string('name',64);
$table->integer('artist_id')->unsigned();
$table->foreign('artist_id')->references('id')->on('artists');
$table->timestamps();
});
}
Here are my eloquent models:
<?php
// Artist model
class Artist extends Eloquent {
public function albums() {
return $this->hasMany('Album');
}
}
<?php
// Album model
class Album extends Eloquent {
public function artists() {
return $this->belongsTo('Artist');
}
}
I used them like this:
Route::get('/', function() {
$artist = new Artist;
$artist->name = "Morbid Angel";
$artist->save();
$album = new Album;
$album->name = "Altars of Madness";
$album->artists()->associate($artist);
$album->save();
return View::make('hello');
});
This does not seem to work, according to the logs:
[2015-06-09 06:01:12] production.ERROR: exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'artists_id' in 'field list''
But I did not create a artists_id
. What does this mean? Shouldn't laravel find artist_id
because it should be singular followed by _id
?
This was your problem. You have named your relation
artists
, but it should beartist
. That's why it was looking for a column namedartists_id
.You should define your relations as follow as it looks to me it is a One to Many.
In your
Artists
ModelIn your
Albums
ModelThen try your code.