Query posts with a thumbnail

Hi! How do I query posts with a thumbnail?

Relationship attribute:

public $attachOne = [
    'thumbnail' => 'System\Models\File'
]

Simple query example in my custom component:

$query = newsModel::orderBy($order, $orderDirection)->get();
return $query;
// Eager loads the relation
$query = newsModel::with('thumbnail')->orderBy($order, $orderDirection)->get();

// ... and only returns records that have the relation
$query = newsModel::withWhereHas('thumbnail')->orderBy($order, $orderDirection)->get();

// No eager load, only check if relation exists
$query = newsModel::whereHas('thumbnail')->orderBy($order, $orderDirection)->get();

See the Laravel documentation for more details:

1 Like