我有Laravel口才一流Collage
Laravel雄辩节省关系模型失败
class Collage extends Model
{
/**
* Get the User that owns this Collage.
*/
public function user()
{
return $this->belongsTo('App\User');
}
}
拼贴迁移模式
Schema::create('collages', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('name')->nullable();
$table->timestamps();
});
和User
class User extends Authenticatable
{
/**
* Get all Collages that belongs to this User.
*/
public function collages()
{
return $this->hasMany('App\Collage');
}
}
但是当我做这个鼓捣
$user = App\User::find(1)->first();
$collage = new App\Collage;
$collage->name = 'Collage';
$collage->user = $user;
//and save
$collage->save();
抛出我的错误QueryException
General error: 1 table collages has no column named user
似乎要添加新用户拼贴? – KmasterYC
不,我只是想广告用户ID(谁拥有这个拼贴)拼贴。 –
我想,从我看到你需要改变关系(我假设你的列):'$ this-> hasMany('App \ User','user_id','id');'和'return $ this - > hasMany('App \ Collage','id','user_id');' –