Auth::user() return null in ServiceProvider - php

i try to get the user id in ServiceProvider but Auth::user() return null after successfully login where is the problem ?
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use View;
use App\Offer;
use Auth;
class MasterPageProfileServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
dd(Auth::user()); // => null
$offersTrashed = Offer::onlyTrashed()->where('user_id',Auth::user()->id)->get();
View::share('offersTrashed', $offersTrashed);
}
}

Related

Laravel UserPolicy always returns 403 no matter what

I have the following policy called UserPolicy.
I want only admin users to access/edit the users data, even though I have set the return value to true(for testing) I still get 403 response no matter what.
namespace App\Policies;
use App\Models\Auth\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class UserPolicy
{
use HandlesAuthorization;
public function viewAny(User $user)
{
// return $user->admin();
return true;
}
}
I have registered the policy as follows
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
use App\Models\Auth\User;
use App\Policies\UserPolicy;
class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* #var array
*/
protected $policies = [
User::class => UserPolicy::class,
];
/**
* Register any authentication / authorization services.
*
* #return void
*/
public function boot()
{
$this->registerPolicies();
//
}
}
The following code is how I use it in the controller
if (Gate::denies('viewAny')) {
return response('Not Authorized!', 403);
}
You should use authorize() method in your controller and pass User class as the second parameter. It will point the request to the targeted policy.
authorize() method is by default provided by Illuminate\Foundation\Auth\Access\AuthorizesRequests trait in your base controller.
Your controller could be like below:
try {
$this->authorize('viewAny', User::class);
} catch (AuthorizationException $e) {
return response('Not Authorized!', 403);
}

Share data with all views in Laravel?

I'm trying to share a collection of notifications to all views under the auth middleware. I thought I might be able to just call Auth::check() but it seems to always return false?
<?php
namespace App\Providers;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
use Auth;
class ViewServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
* #return void
*/
public function boot()
{
view()->composer('*', function ($view)
{
$view->with('userNotifications', Auth::user()->notifications);
});
}
}

Class is not found with ViewComposer in Lravel

I am trying to use Laravel view composer. I have registered my class in config/app.php but I keep getting the following error:
"Class App\Http\ViewComposers\PostComposer does not exist
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use View;
class ComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
View::composer('plain','App\Http\ViewComposers\PostComposer');
}
/**
* Register services.
*
* #return void
*/
public function register()
{
//
}
}
my post composer class
<?php
namespace App\Http\ViewComposer;
use Illuminate\View\View;
use App\Post;
class PostComposer
{
public function comspose(View $view)
{
$posts = Post::all();
$view->with('postha', $posts );
}
}
and here is the screenshot of my browser:
![folder structure in my app][]
Your namespace is wrong.
You're importing from (plural):
App\Http\ViewComposers\PostComposer
but the namespace of your ViewComposer isn't plural:
App\Http\ViewComposer
try it : namespace App\Http\ViewComposer To namespace App\Http\ViewComposers

Creating model event not firing in observer

I have the following files:
ModelObserverProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Models\User;
use App\Models\Profile;
use App\Observers\UserObserver;
use App\Observers\ProfileObserver;
class ModelObserverProvider extends ServiceProvider
{
public function boot()
{
User::observe(UserObserver::class);
Profile::observe(ProfileObserver::class);
}
public function register()
{}
}
UserObserver.php
<?php
namespace App\Observers;
use App\Models\User;
class UserObserver
{
public function creating(User $user)
{
dd('Creating...');
// Hash the users password when creating.
$user->password = bcrypt($user->password);
// Create a new profile for the user.
$user->profiles()->create(['name' => 'guest-' . time()]);
}
}
I have loaded the ModelObserverProvider in my config/app.php, however, notice the dd('Creating...'); ? When creating a new user via User::create($request->all()) this does not appear to be firing at all? Any ideas?
Try this
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Models\User;
use App\Models\Profile;
use App\Observers\UserObserver;
use App\Observers\ProfileObserver;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
User::observe(UserObserver::class);
Profile::observe(ProfileObserver::class);
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{}
}

Class APP\Http\ViewComposers\CompanyComposer does not exist

I'm new in laravel and I need some help with laravel 5. why do I get this error? The error is in Composer class and View conposer.
Here is error
<?php
namespace App\Providers;
use DB;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
View::composer(
'index','APP\Http\ViewComposers\CompanyComposer'
);
}
}
<?php
namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
class CompanyComposer
{
/**
*
* #param View $view
* #return void
*/
public function compose(View $view)
{
//here is data
$view->with('success', 'success');
}
}

Categories