Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel雄辩的保存关系模型失败_Php_Laravel_Eloquent_Relational Database - Fatal编程技术网

Php Laravel雄辩的保存关系模型失败

Php Laravel雄辩的保存关系模型失败,php,laravel,eloquent,relational-database,Php,Laravel,Eloquent,Relational Database,我有一门拉威尔口才班拼贴 class Collage extends Model { /** * Get the User that owns this Collage. */ public function user() { return $this->belongsTo('App\User'); } } 拼贴迁移模式 Schema::create('collages', function (Blueprint $t

我有一门拉威尔口才班
拼贴

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();
        });
用户

class User extends Authenticatable
{
    /**
     * Get all Collages that belongs to this User.
     */
    public function collages()
    {
      return $this->hasMany('App\Collage');
    }
}
但是当我在tinker上做这个的时候

$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

更新您的模式以建立关系

Schema::create('collages', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('user_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users');
    $table->string('name')->nullable();
    $table->timestamps();
});

这样,Laravel Eloquent将自动引用关系。

更改

$collage->user = $user

$collage->user_id = $user->id

似乎你想在拼贴中添加新用户?不,我只是想在拼贴中添加用户id(拥有此拼贴的人)。我认为,从我看到的情况来看,你需要更改关系(我假设你的列):
$this->hasMany('App\user','user\u id','id')
返回$this->hasMany('App\Collage','id','user\u id')是的,我添加了这一行,但仍然是一样的,我使用sqlite,可能是sqlite导致的吗?