PHP code example of santigarcor / laravel-ownable

1. Go to this page and download the library: Download santigarcor/laravel-ownable 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/ */

    

santigarcor / laravel-ownable example snippets




use Ownable\OwnsModels;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use OwnsModels;
}



use Ownable\OwnsModels;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use OwnsModels;
}

class Video extends Model
{
}

$user = User::first();
$video = Video::first();

// Check If the user owns the video
if ($user->owns($video)){}

// Check If the user doesn't owns the video
if ($user->doesntOwn($video)){}

// Check If the user owns the video but the foreign key is the_user_id
if ($user->owns($video, 'the_user_id')){}



use Ownable\OwnsModels;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use OwnsModels;
}

use Ownable\Contracts\Ownable;

class Video extends Model implements Ownable
{
    public function isOwnedBy($owner): bool
    {
        if ($owner instanceof User) {
            return $this->someRelationship->user_id == $owner->id;
        }

        return false;
    }
}

$user = User::first();
$video = Video::first();

// Then you can simply call the owns method on the user and it will work.

// Check If the user owns the video
if ($user->owns($video)){}

// Check If the user owns the video but don't use the ownable logic, instead the regular one with the foreign key.
if ($user->owns($video, null, false)){}