1
votes

I am facing problem regarding permission

Argument 1 passed to App\Providers\AuthServiceProvider::App\Providers{closure}() must be an instance of App\Providers\User, instance of App\User given, called in C:\xampp\htdocs\Tweety\vendor\laravel\framework\src\Illuminate\Auth\Access\Gate.php on line 473 (View: C:\xampp\htdocs\Tweety\resources\views\tweet.blade.php)

I am just working to show delete button only on those tweets made by the authenticated users

my controller

  public function destroy(Tweet $tweet)
      {

          $tweet->delete();
          Session::flash('success');
          return redirect()->route('tweets.index')->with(['message' => 'Tweet Deleted']);

      }

my user model

 public function tweets()
    {
        return $this->hasMany(Tweet::class)->latest();
    }

my blade

 @can('delete',$tweet)
                <form action="{{ route('tweets.destroy',$tweet->id) }}" method="POST">
                    @csrf
                    @method('DELETE')
                        <button type="submit" class="btn btn-danger">Delete</button>
                </form>
            @endcan

AuthServiceProvider

 public function boot()
    {
        $this->registerPolicies();

        Gate::define('delete', function (User $user , Tweet $tweet){
           return  $tweet->user->is($user);
        });
    }

Any help will be appreciated

2
In AuthServiceProvider, did you import User class correctly? looks like it's namespace issue. use App\UserCrazyDev
please post full code for AuthServiceProviderCrazyDev
yes it was the issue of namespace, thanks for your helpNabeel Siddiqui

2 Answers

1
votes

It looks like a namespace issue and didn't import User model's namespace correctly.

<?php
    
    namespace App\Providers;
    
    use Illuminate\Support\Facades\Gate;
    use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
    use App\User; // looks like you're missing this line.
    use App\Tweet;

    class AuthServiceProvider extends ServiceProvider
    {
        /**
         * The policy mappings for the application.
         *
         * @var array
         */
        protected $policies = [
            'App\Model' => 'App\Policies\ModelPolicy',
        ];
    
        /**
         * Register any authentication / authorization services.
         *
         * @return void
         */
         public function boot()
         {
             $this->registerPolicies();

             Gate::define('delete', function (User $user , Tweet $tweet){
                return  $tweet->user->is($user);
             });
        }
    }
0
votes

You would need to change:

@can('delete', $tweet)

to:

@can('delete')