1. Go to this page and download the library: Download tmyers273/laravel-ownership 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/ */
tmyers273 / laravel-ownership example snippets
// Let's whip up a few Users and a Post to demonstrate
$owner = factory(User::class)->create();
$post = factory(Post::class)->create([
'user_id' => $owner->id
]);
$otherUser = factory(User::class)->create();
$adminUser = factory(USer::class)->create([
'is_admin' => true
]);
use App\Traits\OwnsModels;
class User extends Authenticatable
{
use OwnsModels;
}
use TMyers273\Ownership\OwnedByUser;
class Post extends Model
{
use OwnedByUser;
}
// This will be a successful call, since $owner owns the $post
$this->be($owner);
$this->json('PATCH', '/post/' . $post->id, [
'title' => 'Edited by Owner'
]);
// This will fail, since $otherUser does not own the $post
$this->be($otherUser);
$this->json('PATCH', '/post/' . $post->id, [
'title' => 'Edited by Owner'
]);
// This will be a successful call, since $adminUser can override the ownership check (by default)
$this->be($adminUser);
$this->json('PATCH', '/post/' . $post->id, [
'title' => 'Edited by Owner'
]);
// This will fail, since 12345 is a made up Post id
$this->be($owner);
$this->json('PATCH', '/post/12345', [
'title' => 'Edited by Owner'
]);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.