无法在Laravel中获取Auth :: user-> id

时间:2016-06-29 13:50:53

标签: php laravel laravel-5.2

这是我的控制器。

<?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);
    }

}

如果我调用此控制器的任何功能,我将无法获取Auth::user()->id

这是一个错误。

  

MagazineController.php第54行中的ErrorException:   尝试获取非对象的属性

如果我尝试在任何其他控制器中回显Auth::user()->id,它的工作正常。

使用laravel 5.2

任何人都可以帮助我吗?

如果您愿意,请随时提出任何问题。

6 个答案:

答案 0 :(得分:2)

Auth::user可能会返回null,而您正试图让id为空。

因此请确保用户的记录如下:

if (Auth::check()) {
  // The user is logged in...
  echo Auth::user()->id;
}

答案 1 :(得分:0)

ApiController做了什么?

将顶部的命名空间更改为

namespace App\Http\Controllers;

尝试使用普通ApiController更改Controller并检查将会发生什么

而不是:

class MagazineController extends ApiController

像这样改变:

class MagazineController extends Controller

答案 2 :(得分:0)

确保此控制器的路由包含在web中间件组中。

此外,由于MagazineController延伸ApiController,请确保ApiController延伸Controller

答案 3 :(得分:0)

像这样使用Laravel 5.2方式

$user_collection = Auth::user();
echo $user_collection->id;
exit;

答案 4 :(得分:0)

您是否更改了用户表的ID名称?我经历过那个。我把我的全部资金(身份证)。我在Linux上,db,表和列名称区分大小写。请尝试检查users表的id列。

也就是说,如果您确定该用户已登录。否则,如果我没有弄错的话,它将为空。

答案 5 :(得分:0)

将您的控制器置于登录中间件之下,以确保您拥有登录用户,或者只是在访问用户ID之前进行验证:

if (Auth::check()) {
    $id = Auth::user()->id;
}