I was working on a blog projects API, and then found this kind of weird behavior that looks like a bug. While requesting user id in the function that returns user articles by id, it work all right, but on the other controller functions it always give null id. Here is my controller code. After trying different methods found around the web nothing seems to work!
`
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppArticle;
use IlluminateSupportFacadesAuth;
class articles_controller extends Controller
{
public function index()
{
return Article::all();
}
public function show()
{
return Article::where('user_id', auth()->user()->id)->get();
}
public function store(Request $request)
{
$user_id =Auth::user()->id;
$article = new Article([
'user_id' => $user_id,
'title' => $request->title,
'article' => $request->article,
]);
$article->save();
ddd($article);
return $article;
}
public function delete(Request $request)
{
$user_id = $request->user;
$article = Article::where('user_id',$user_id)->first();
$article->delete();
return 204;
}
}`
`
namespace App;
use IlluminateDatabaseEloquentModel;
use IlluminateFoundationAuthUser as Authenticatable;
class Article extends Authenticatable
{
protected $fillable = [ 'title', 'article'];
protected $hidden = [ 'id','created_at', 'updated_at'];
public function user()
{
return $this->belongsTo('App\User');
}
}`
`
use IlluminateHttpRequest;
use AppArticle;
use AppControllersUserController;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
Route::post('login', 'UserController@login');
Route::post('register', 'UserController@register');
Route::group(['middleware' => 'auth:api'], function(){
Route::get('/article', 'articles_controller@index');
Route::get('/article/specific','articles_controller@show');
Route::post('article', 'articles_controller@store');
Route::patch('article/{id}', 'articles_controller@update');
Route::delete('article/delete', 'articles_controller@delete');
});`
No matter what I try, always the show function works as it should while other functions return the id as null.
It's normal, you are using the auth:api middleware which does not retain user in session
So what shall be used in this case and why it works only for the show function?
The github issues are for reporting bugs, there are other channels to request help with your project.
Laracasts Forums
Laravel.io Forums
StackOverflow
Discord
Larachat
IRC
That aside, you could try to check with dd what is the resolved user id because if the show method work then the store should be too.
In the delete mehod you should call the user method instead of reading the user property.
$user_id = $request->user()->id;
Furthermore I think you really should read and follow a style guide like the PSR-12 or PSR-2.
Next time please use the code blocks to post your code (put it between ```php and ```)
Well tried what you said and it changes nothing. If it's not a bug then why it gets the id only in the first function and returns null on the other ones? Furthermore no solution seems to work in any forum, maybe cause of the fact that I'm currently using the latest Laravel version?
Please post the result of the following command in the show and in the other affected methods, if the $request parameter is not available, then omit that parameter or add to the method.
dd(
auth()->id() ?? '?',
Auth::id() ?? '?',
$request->user()->id ?? '?',
auth()->check(),
get_class(auth()->guard())
);
It seems like it gets the id, but why does it save it as null in the database then?
Furthermore here is the preview
```3
3
3
true
"IlluminateAuthRequestGuard"```
Please add the user_id field to your fillable properties in the Article model and try again.
That will be it, it only throws MassAssignmentException if there are no fillable fields
Thank you. Had this issue for 2 days. Thanks again!
Just in case you鈥檙e not aware, there are a lot of options available to get help with Laravel.
Laracasts, Laravel.io, Laravel discord channel, and larachat slack. You could probably get help much faster there for issues like this.
Hi,
I have the same problem with auth()->user(). Here are the results which I have:
"?"
"?"
false
"IlluminateAuthSessionGuard"
Hi,
I have the same problem with auth()->user(). Here are the results which I have:
"?"
"?"
false
"IlluminateAuthSessionGuard"
Hi @riatabduramani have you found a solution. I'm getting exact same outcomes.
same
"?"
"?"
"?"
false
"Illuminate\Auth\SessionGuard"
i also got same
"?" "?" "?" false "IlluminateAuthSessionGuard"
As a person who had the same problem, I can share the solution that worked for me. I switched from Passport to Sanctum.