Consulta Laravel Eloquent

Asked

Viewed 1,365 times

1

How to query below with Eloquent, in view of the fact that the Group By of error in the Eloquent.

SELECT
  otimibus_gps.positions.id,
  otimibus_gps.positions.deviceid,
  otimibus_gps.positions.devicetime,
  otimibus_gps.positions.latitude,
  otimibus_gps.positions.longitude
FROM
  otimibus_gps.positions
WHERE deviceid in ('1','2')
GROUP BY deviceid
ORDER BY devicetime DESC
  • 1

    What error is being presented when you try to make the query by Eloquent?

  • 1

    When I put select and the above expression it returns empty when it should return at least 2 lines and when I use the ->groupBy error Syntax error or access Violation: 1055 Expression #1

  • 1

    @Carlosalexandrerramos, put what you have managed to do and put the error in your answer please and explain which grouping you need!

2 answers

1


To generate this query you have 2 ways to do it.

In case I will assume that the name of your table is positions even, but what I will try to explain to you is valid for any table you want to use from the database.

Using the DB library or Elonquent from Laravel, both are excellent but each works a little different, but the purpose is the same.

If you want to use Elonquent you need to create a model.

To create models, I orient using the Artisan wizard of the proper Laravel, you open a command prompt and go to the folder of your project and type:

php artisan make:model Positions

The model that will be created stays in your folder app.

With the model created you enter it and define the table that will be used in the database through the code line:

protected $table = "positions"

If you don’t own the fields created_at, updated_at and deleted_at in your type table timestamps (I am considering that you are using Mysql as a database) you have to disable the timestamps from it (I am implying that you will elonquent the Adjustable for inserts, edits and so on). And to do this even within your model you need to enter the following line of code:

public $timestamps = false;

That step is already ready, now let’s use your enloquent.

First you need to configure your model inside your controller in case you don’t want to keep bumping heads with references.

Then you will pick up and put the following one line below some term use

use App\Positions;

Ready, your control is already referenced with your model.

Now let’s create your query within the method, there are 2 ways to do, apparently there is no difference, but technically has differences in relation to memory and etc, but it is not the focus of the answer so I leave this as curiosity for your searches.

For you to query would look like this 1:

$positions = new Positions();
$positions = $positions->select('id', 'deviceid', 'devicetime', 'latitude', 'longitude')
    ->whereIn('deviceid', [1, 2])
    ->groupBy('id', 'deviceid', 'devicetime', 'latitude', 'longitude')
    ->orderBy('devicetime', DESC)
    ->get();

And the second way is how Virgilio Novic said above, but you can’t forget above after your controller’s namespace declaration use DB

Basically it is like this, it is not very difficult or anything, you did not say which version of the Standard you are using, so I assume it is the latest, I advise gives a good read in the documentation that it is very easy and simple to understand.

https://laravel.com/docs/5.5

  • 1

    Thanks already, I tried so yesterday more instead of returning me the last record of Id1 and Id2, he is bringing me all in descending order as the version and 5.4

  • 1

    Got it that way Thanks Thanks Thanks Even $data = Position::select('deviceid', 'devicetime', 'latitude', 'longitude') ->wherein('deviceid', [1, 2]) ->groupBy('deviceid') ->orderby('devicetime', 'DESC') ->get();

  • Glad you could make it, glad you could help.

0

You can try to use it as follows:

Model::select('id', 'deviceid', 'devicetime', 'latitude', 'longitude')
    ->whereIn('deviceid', [1, 2])
    ->groupBy('id', 'deviceid', 'devicetime', 'latitude', 'longitude')
    ->orderBy('devicetime', DESC)
    ->get();
  • 1

    I think we are on the right track but he brought all the disaggregated positions, example needs it to bring only one line or each is inserted in the wherein

  • 1

    Sorry I couldn’t understand, can you give more details? So it’s easier to help you.

  • 1

    I need a result like this:

  • 1

    { "id": 389, "deviceid": 2, "latitude": -22.7306, "longitude": -42.943278333333 } { "id": 388, "deviceid": 1, "latitude": -22.7306, "longitude": -42.943278333333 }

  • 1

    and he’s bringing me all the records

  • 1

    Can a device have more than one record in the same table? Because the correct thing with this query will be to bring only Devices 1 and 2 that are in wherein.

  • 1

    SQLSTATE[42000]: Syntax error or access Violation: 1055 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'optimistbus_gps.positions.devicetime

  • 1

    I don’t know if this is the right way to proceed, but change the config/database.php in the array of your connection to 'strict' => false and test again leaving in groupBy only the deviceid

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.