Laravel 5.3 Custom Class - laravel

Create a custom class in laravel when I am call in controller construct then
Auth::user() not return any data
When call from in a function then it's work
Class Code
<?php namespace App\Libraries;
use App\User;
use Auth;
use Illuminate\Support\Facades\DB;
use App\Friends;
class AppLibrarie
{
private static $friends_ids = array();
public function __construct()
{
self::$friends_ids=Auth::user();
}
public function getfriends(){
return self::$friends_ids;
}
}
And Controller
<?php
namespace App\Http\Controllers;
use App\Libraries\AppLibrarie;
use Illuminate\Http\Request;
use App\Http\Requests;
class LiveController extends Controller
{
protected $lib;
public function __construct(AppLibrarie $appLibrarie)
{
$this->lib = $appLibrarie;
}
public function search(Request $request){
return response()->json($this->lib->searchdata($request->get('query')));
}
}

Accessing authenticated user sessions has been deprecated in Laravel 5.3. Here is the paragraph in the upgrade guide
In previous versions of Laravel, you could access session variables or the authenticated user in your controller's constructor. This was never intended to be an explicit feature of the framework. In Laravel 5.3, you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet.
As an alternative, you may define a Closure based middleware directly in your controller's constructor. Before using this feature, make sure that your application is running Laravel 5.3.4 or above:
You will need to rethink your Authentication structure a bit if you are to upgrade

Related

How to access inputted token value - Laravel API

I am inputting a token as above and trying to print the token value with the below code. But getting error Undefined variable: token. I'm not sure whether i can access the token as below. Pls help me with ur suggestions.
<?php
namespace App\Http\Controllers;
use App\Models\Files;
use Illuminate\Support\Facades\Auth;
class FileController extends Controller
{
public function upload()
{
$tock=Auth::user()->$token;
dd($tock);
}
}
You are trying to access the $token variable on the user, even though it does not exist.
Instead you should by trying to access the request and the values that are send with the request. This can be achieved using the request() helper or by injecting the Request class into the function.
With injection:
<?php
namespace App\Http\Controllers;
use App\Models\Files;
use Illuminate\Http\Request; // Add request to namespace
use Illuminate\Support\Facades\Auth;
class FileController extends Controller
{
public function upload(Request $request)
{
dd($request->bearerToken());
}
}
Or, without injection:
<?php
namespace App\Http\Controllers;
use App\Models\Files;
use Illuminate\Support\Facades\Auth;
class FileController extends Controller
{
public function upload()
{
dd($request->bearerToken());
}
}
With the bearerToken() method you can access the Bearer token provided in the request.

Call Auth:user() Laravel in extend Controller

I create my controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ResponsaveisController extends InternoController
{
}
I try to access Auth::user() multiple forms, all times return NULL
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Auth;
class InternoController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
var_dump(Auth::user());
}
}
Would anyone have any suggestions on how to solve this issue?
This is taken directly from the Laravel docs
In previous versions of Laravel, you could access session variables or the authenticated user in your controller's constructor. This was never intended to be an explicit feature of the framework. In Laravel 5.3, you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet
You can read about it here as it provides an alternative solution.

Custom validation using make:request returns controller not found

Using php artisan make:request StoreUserData I've create my rules to the request:
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreUserData extends FormRequest {
public function rules(){
return [
'name'=>'required|integer',
'surname'=>'required|max:255|string',
];
}
}
And I'm trying to use it in controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserDataController extends Controller {
public function store(StoreUserData $request){
return 'valid';
}
}
Here the error I get: Class App\Http\Controllers\StoreUserData does not exist.
PS. Is not a problem of routing.
I'm following this documentation since I'm using Laravel 5.6 https://laravel.com/docs/5.6/validation#creating-form-requests
Actually when you use StoreUserData in controller method then you have to import that class otherwise it will assume the class is in App\Http\Controllers namespace and thats why it throw Class App\Http\Controllers\StoreUserData does not exist.
just add the below import at top of your controller class
use App\Http\Requests\StoreUserData

Binding Primitives in Laravel Scope class

I have a global scope that is used for a few models. Here is the scope class:
<?php
namespace App\Scopes;
use App\Models\UserMeta;
use Auth;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class WorkplaceScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$user_active_workplace = UserMeta::where(['user_id' => Auth::id(), 'meta_key' => 'active_workplace'])->first();
$builder->where('workplace_id', $user_active_workplace->meta_value);
}
}
I am assigning the scope by overriding the model's boot method:
protected static function boot()
{
parent::boot();
static::addGlobalScope(new WorkplaceScope);
}
The problem is that I make a query inside the scope and that query is executed a lot of times for no reason. I was thinking of using primitive binding inside a service provider, but I can't figure out how to do it properly. I tried adding this inside the AppServiceProvider:
$this->app->when('App\Scopes\WorkplaceScope')
->needs('$user_active_workplace_meta_value')
->give(1);
but I don't know how to hint the service provider that the WorkplaceScope expects a $user_active_workplace_meta_value variable. Any idea how can I do this?

Laravel: Model not working if I named it "Auth"

Why when I name my model Auth it not working at all? But when I change name to different model work correctly?
Not working:
<?php
class Auth extends Eloquent {
public static function check()
{
return "working";
}
}
Working:
<?php
class MyAuth extends Eloquent {
public static function check()
{
return "working";
}
}
Laravel already has a built-in Auth class.
You could remove the line:
'Auth' => 'Illuminate\Support\Facades\Auth',
from app/config/app.php if you're not using Laravel's built-in Auth class.
Auth is a predefined class for user authorization in Laravel. To name a second class Auth too, you will need to put the new one in a different Namespace

Resources