1

I have Laravel Eloquent class Collage

class Collage extends Model { /** * Get the User that owns this Collage. */ public function user() { return $this->belongsTo('App\User'); } } 

Collage migration schema

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

and User

class User extends Authenticatable { /** * Get all Collages that belongs to this User. */ public function collages() { return $this->hasMany('App\Collage'); } } 

but when I do this on tinker

$user = App\User::find(1)->first(); $collage = new App\Collage; $collage->name = 'Collage'; $collage->user = $user; //and save $collage->save(); 

throws me error QueryException

General error: 1 table collages has no column named user 
3
  • Seem you want to add new user to collage? Commented Aug 17, 2016 at 6:00
  • no i just wanted to ad user id (who owns this collage) to collage. Commented Aug 17, 2016 at 6:01
  • I think, from what I see you need to change the relationships(I am assuming your columns): $this->hasMany('App\User', 'user_id', 'id'); and return $this->hasMany('App\Collage','id','user_id'); Commented Aug 17, 2016 at 6:15

2 Answers 2

1

Change

$collage->user = $user 

To

$collage->user_id = $user->id 
Sign up to request clarification or add additional context in comments.

Comments

1

Update your schema to make the relationship,

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

That way Laravel Eloquent will automatically make references to the relationship.

1 Comment

Yes i have added this line but still the same, Im using sqlite, may be sqlite cause this?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.