4 years ago

[Newbie] Eloquent relationship "belongsTo" doesn't store

Posted 4 years ago by ngocphamm

Hi all. I'm slow, and still following the series

This is where I got stuck. I'm having trouble changing the "relationship" from "user" to "author". Basically I want $article->author(). I changed the function name to "author", changed the Article table's column name to "author_id", refers to "id" in table User.

However, now when I create a new article, I get this error

SQLSTATE[HY000]: General error: 1 table articles has no column named user_id (SQL: 
insert into "articles" ("author_id", "title", "body", "published_at", "user_id", "updated_at", "created_at") 
values (1, New Article, My first article here!, 2015-07-11 00:00:00, 1, 2015-07-11 13:00:33, 2015-07-11 13:00:33))

I think it's because I used Auth::user()->articles()->create($request->all()); (Route model binding) under the store() method but I have no idea how to solve it. Please help.

Please sign in or create an account to participate in this conversation.