Reputation: 4987
Here is my Controller.
<?php
/**
* Created by PhpStorm.
* User: ed
* Date: 05/02/16
* Time: 09:33
*/
namespace App\Http\Controllers\API\V1;
use App\Certificate;
use App\Country;
use App\Film;
use App\FilmExtra;
use App\FilmFavourite;
use App\FilmGenre;
use App\FilmLike;
use App\FilmView;
use App\Genre;
use App\Language;
use App\Magazine;
use App\News;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
class MagazineController extends ApiController
{
public function viewAll(){
echo Auth::user()->id;
exit;
$user_id = Input::get('user_id');
$magazines = Magazine::paginate(5);
return parent::api_response($magazines->toArray(), true, ['return' => 'all magazines'], 200);
}
public function getMagazine($id){
$magazine = Magazine::find($id);
if($magazine){
return parent::api_response($magazine->toArray(), true, ['return' => 'magazine details'], 200);
}else{
return parent::api_response([], false, ['error' => 'Couldn\'t find that magazine'], 404);
}
}
protected function getURL($id){
$magazine = Magazine::find($id);
if($magazine){
return parent::api_response(['url' => $magazine->file_url], true, ['return' => 'magazine url'], 200);
}else{
return parent::api_response([], false, ['error' => 'Couldn\'t find that magazine'], 404);
}
}
public function search($term){
$magazines = Magazine::search($term)->paginate(5);
return parent::api_response($magazines, true, ['return' => 'search for '.$term], 200);
}
public function purchased(){
$magazines = Magazine::leftJoin('ordered_items', 'ordered_items.item_id', '=', 'magazines.id')
->leftJoin('orders', 'orders.id', '=', 'ordered_items.order_id')
->leftJoin('items', 'items.id', '=', 'ordered_items.item_id')
->where('orders.user_id', $user_id)
->where('items.class', 'book');
if(Input::get('filter')) {
$jsonFilter = Input::get('filter');
$filters = json_decode($jsonFilter);
foreach ($filters as $filter => $value){
switch ($filter){
case "genre":
if($value){
$magazines = $magazines->whereHas('genre', function ($query) use($value) {
$query->whereIn('genre_id', $value);
});
}
break;
case "cert":
if($value){
$magazines = $magazines->whereIn('cert', $value);
}
break;
case "country":
if($value){
$magazines = $magazines->whereIn('country', $value);
}
break;
case "lang":
if($value){
$magazines = $magazines->whereHas('languages', function ($query) use($value) {
$query->whereIn('language_id', $value);
});
}
break;
}
}
}
$magazines = $magazines->paginate(5);
return parent::api_response($magazines->toArray(), true, ['return' => 'all magazines'], 200);
}
}
If i call any of the function for this controller i am not able to get my Auth::user()->id
.
it's throwing an error.
ErrorException in MagazineController.php line 54: Trying to get property of non-object
If i try to echo Auth::user()->id
in any other controller, its working fine.
using laravel 5.2
Can anybody help me ?
don't hesitate to ask any question if you want.
Upvotes: 2
Views: 2836
Reputation: 4013
What ApiController do?
Change the namespace at the top to
namespace App\Http\Controllers;
Try to change ApiController
with plain Controller
and check what will be happen
Instead of:
class MagazineController extends ApiController
Change it like so:
class MagazineController extends Controller
Upvotes: 0
Reputation: 309
Put your controller under a login Middleware to ensure you have a logged in user, or simply put a verification before you access user id:
if (Auth::check()) {
$id = Auth::user()->id;
}
Upvotes: 0
Reputation: 218
Did you changed the id name of your users table? I experienced that. I made mine all capital (ID). I was on Linux and db, table, and column names are case sensitive. Try checking the id column of your users table.
That is, if you're sure that the user is logged in. Else, it would be null if I'm not mistaken.
Upvotes: 0
Reputation: 1129
Use it like this the Laravel 5.2 way
$user_collection = Auth::user();
echo $user_collection->id;
exit;
Upvotes: 0
Reputation: 33048
Ensure your routes for this controller are wrapped in the web
middleware group.
Also, since MagazineController
extends ApiController
, ensure that ApiController
extends Controller
.
Upvotes: 0
Reputation: 4690
Probably the Auth::user
returns null
and you are trying to get id
of null.
So make sure that user is logged like:
if (Auth::check()) {
// The user is logged in...
echo Auth::user()->id;
}
Upvotes: 3