Sunday, March 5, 2017

Why it unable to get Policies working in Laravel 5.3?

User table is like this :

enter image description here

Favorite table is like this :

enter image description here

My FavoritePolicy is like this :

<?php
namespace App\Policies;
use App\User;
use App\Models\Favorite;
use Illuminate\Auth\Access\HandlesAuthorization;
class FavoritePolicy
{
    use HandlesAuthorization;
    public function view(User $user, Favorite $favorite)
    {
        return $user->id === $favorite->user_id;
    }
}

When I add dd($user->id), the result is 1

1 is user id that is being logged

When I add dd($favorite->user_id), the result is null

Whereas based on the data in the favorites table, it should display 1

Why it displays null?

If I add dd($favorite), the result is like this :

Favorite {#498 ▼
  #fillable: array:3 [▼
    0 => "user_id"
    1 => "favoritable_id"
    2 => "favoritable_type"
  ]
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: []
  #original: []
  #relations: []
  #hidden: []
  #visible: []
  #appends: []
  #guarded: array:1 [▼
    0 => "*"
  ]
  #dates: []
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  +exists: false
  +wasRecentlyCreated: false
}

From the above results, the records that have the user_id = 1, did not appear. Whereas it should appear

Is there anyone who can help me?



via moses toh

Advertisement