PHP code example of salomoni / authorized-attributes
1. Go to this page and download the library: Download salomoni/authorized-attributes library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
salomoni / authorized-attributes example snippets
namespace App;
use Illuminate\Database\Eloquent\Model;
use Vantage\AuthorizedAttributes;
class Post extends Model
{
use AuthorizedAttributes;
/**
* The attributes that should be fillable from requests.
*
* @var array
*/
protected $fillable = ['title', 'content', 'author_id'];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = ['draft'];
}
namespace App\Policies;
use App\Post;
use App\User;
class PostPolicy
{
/**
* Determine if an draft attribute can be seen by the user.
*
* @param \App\User $user
* @param \App\Post $post
* @return bool
*/
public function seeDraft(User $user, Post $post)
{
// Post drafts can only be seen by admins and the post author
return $user->isAdmin() || $user->created($post);
}
/**
* Determine if the author_id attribute can be changed by the user.
*
* @param \App\User $user
* @param \App\Post $post
* @return bool
*/
public function editAuthorId(User $user, Post $post)
{
// Admins can re-assign the author for non-published posts
return $user->isAdmin() && $post->isNotPublished();
}
}
use Illuminate\Support\Str;
class Post extends Model
{
/**
* Get the method name for the attribute visibility ability in the model policy.
*
* @param string $attribute
* @return string
*/
public function getAttributeViewAbilityMethod($attribute)
{
return 'see'.Str::studly($attribute);
}
/**
* Get the model policy ability method name to update an model attribute.
*
* @param string $attribute
* @return string
*/
public function getAttributeUpdateAbilityMethod($attribute)
{
return 'edit'.Str::studly($attribute);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.