Arek TG
Arek TG

Reputation: 188

Laravel, additional request data

Im sending from my form 2 values - title and content. MySQL table has fields: id, title, content, userID, created_at, updated_at. Table was created with laravel migration. I dont have to send id, created_at and updated_at. Only title, content and userID which I get from Auth::class.

public function store(Request $request) {
    //some validation
    $request->request->add(['userID' => Auth::id()]);
    Article::create($request->all());

    // testing purpose
    return $request->all();
}

I know I can do it another way directly in create function using array_merge or simply adding two arrays, but I thought this is better way (?) to do this, but Im getting such error:

SQLSTATE[HY000]: General error: 1364 Field 'userID' doesn't have a default value (SQL: insert into `articles` (`title`, `content`, `updated_at`, `created_at`) values ...

Looks like function create doesnt't see userID value. What is funny, if I comment that create line, by return I can see in my brower everything:

{"_token":"...","title":"Test","content":"Test","userID":1}

Why is it returned correctly and in same time function create doesn't see userID value?

Upvotes: 0

Views: 484

Answers (1)

Martin Mička
Martin Mička

Reputation: 31

Try passing the request body instead: Article::create($request->request->all);

It looks like there might be an error with parsing that added value.

Also, when you call $request->request->all(), does it have your added userID?

Upvotes: 2

Related Questions