A
A
Alexey Sklyarov2018-09-09 12:41:51
Laravel
Alexey Sklyarov, 2018-09-09 12:41:51

What is the correct way to use distinct in such a query?

Laravel version: 5.5.*
I use this method to get 50 rows from the database.

/**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
      $prices = Prices::where('name',$id)->take(50)->get();
      return GraphResource::collection($prices);
    }

But according to the last_updated field, I need to get records with a non-repeating key value (the column stores the time in timestamp format).
I'm trying to do this:
$prices = Prices::where('name',$id)->take(50)->distinct('last_updated')->get();

But the data is still displayed with duplicate last_updated values.
How to select from a database of 50 values ​​with unique data in last_updated?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2018-09-09
@0example

id: 1, last_updated: 1524855575, price: 5000
id: 2, last_updated: 1524855575, price:5000.

You can group through groupBy('name', 'last_updated') then there will be unique results.
You should also think about adding a unique last_updated/name key so that such records are not entered into the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question