Q:

Manually Passing a Foreign key Value

I can not pass a foreign key value (which is user_id) to my newly created article.
Here is my code...
<?php

if (is_null($request->user_id)) {
    $request->user_id = $user->user_id;
}

$request->validate(['title' => 'Required|string|min:3', 'body' => 'Required|string|min:5', 'user_id' => 'Required|exists:users,user_id']);

if ($article = Article::create($request->all())) {
    event(new ArticleCreated($user));
    return response()->json(['success' => true, 'reason' => 'Article Created successfully', $article]);
} else {
    return 'Article could not be created';
}

A:

Change this:
 if($article = Article::create($request->all())) {
    $article->user_id = $request->user_id; 
    $article->save();

    event(new ArticleCreated($user));
    return response()->json(['success' => true, 'reason' => 'Article Created successfully', $article]);

}

