PHP code example of nullthoughts / laravel-latest-relation

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

    

nullthoughts / laravel-latest-relation example snippets


$users = User::whereLatestRelation('logins', 'device_type', '=', 'desktop');

public function scopeUsingDevice($query, $device)
{
    return $query->whereLatestRelation('logins', 'device_type', $device);
}

public function scopeHavingCountry($query)
{
    return $query->whereLatestRelation('logins', 'country', '!=', 'null');
}

$users = User::whereHas('logins', function ($query) {
    $query->whereLatest('device_type', 'desktop');
});

public function scopeUsingDevice($query, $device)
{
    return $query->whereHas('logins', function ($query) use ($device) {
        $query->whereLatest('device_type', $device);
    });
}

$users = User::whereHas('logins', function ($query) {
    $query->latestRelation()->whereBetween(
        'created_at', [
            Carbon::now()->startOfDay(),
            Carbon::now()->endOfDay()
        ]);
});

public function scopeHavingDeviceType($query)
{
    return $query->whereHas('logins', function ($query) {
        $query->latestRelation()->whereNotNull('device_type');
    });
}

$users = User::whereLatestRelation('logins', 'device_type', 'desktop');

$users = User::whereHas('logins', function ($query) {
    $query->whereEarliest('device_type', 'desktop');
});

$users = User::whereHas('logins', function ($query) {
    $query->earliestRelation()->whereNotNull('device_type');
});