If this is for an array/collection, and not an Eloquent query, you can use the collection keyBy method to get unique values.
https://laravel.com/docs/5.8/collections#method-keyby
$data = [
[
"Title" => "A",
"Publication date" => "2013",
],
[
"Title" => "B",
"Publication date" => "2010",
],
[
"Title" => "B",
"Publication date" => "2999",
],
[
"Title" => "B",
"Publication date" => "5555",
],
];
$collection = collect($data)->keyBy('Title');
Then your output will look like this:
Collection {#1389
#items: array:2 [
"A" => array:2 [
"Title" => "A"
"Publication date" => "2013"
]
"B" => array:2 [
"Title" => "B"
"Publication date" => "5555"
]
]
}
The keyBy method will give you the last item in the collection for each Title, so you can sort the collection before calling keyBy if you want to get specific items:
// get each Title with the highest Publication date
$collection = collect($data)->sortBy('Publication date')->keyBy('Title');
// get each Title with the lowest Publication date
$collection = collect($data)->sortByDesc('Publication date')->keyBy('Title');
If you want to get back an array without the keys "A" and "B" then you can do this:
$collection = collect($data)->keyBy('Title')->values()->toArray();
Output:
array:2 [
0 => array:2 [
"Title" => "A"
"Publication date" => "2013"
]
1 => array:2 [
"Title" => "B"
"Publication date" => "5555"
]
]