framework: Auth::user always returning null
- Laravel Version: 6.2.0
- PHP Version: 7.2.19
- Database Driver:Mysql
Description:
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!
`<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request; use App\Article; use Illuminate\Support\Facades\Auth;
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;
}
}`
`<?php
namespace App;
use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Auth\User 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');
}
}`
`<?php
use Illuminate\Http\Request; use App\Article; use App\Controllers\UserController;
/* |--------------------------------------------------------------------------
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.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 17 (4 by maintainers)
As a person who had the same problem, I can share the solution that worked for me. I switched from Passport to Sanctum.
same
"?" "?" "?" false "Illuminate\Auth\SessionGuard"
i also got same “?” “?” “?” false “Illuminate\Auth\SessionGuard”
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.
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```
)