How to Use Distinct in the Laravel Query Model
In this lesson, we will see how to use distinct in the Laravel query model, so let's assume that we have a table where we store image extensions and we want to get all the extensions and eliminate all duplicate records from the result.
Use distinct in the Laravel query model
So, to get all extensions from the table and eliminate all duplicate records from the result use the following code.
/**
* Fetch all the extensions
*/
public function fetchExtensions()
{
$extensions = Image::select('ext')->distinct()->get();
return redirect()->route('home')->with([
'extensions' => $extensions
]);
}