PHP code example of crhg / eloquent-exists-relation

1. Go to this page and download the library: Download crhg/eloquent-exists-relation 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/ */

    

crhg / eloquent-exists-relation example snippets


$app->register(\Crhg\EloquentExistsRelation\Providers\EloquentExistsRelationProvider::class);

$posts = App\Post::withExists('comments')->get();

foreach ($posts as $post) {
    echo $post->comments_exists;
}

$posts = App\Post::withCount(['votes', 'comments' => function ($query) {
    $query->where('content', 'like', 'foo%');
}])->get();

echo $posts[0]->votes_exists;
echo $posts[0]->comments_exists;

$posts = App\Post::withCount([
    'comments',
    'comments as pending_comments_exists' => function ($query) {
        $query->where('approved', false);
    }
])->get();

echo $posts[0]->comments_exists;

echo $posts[0]->pending_comments_exists;

    protected $cast = [
        'commensts_exists' => 'bool',
    ];