Unexpected behaviour when using groupby in eloquent query for latest recordsGet most recent row with group by and LaravelLaravel 4 Eloquent Query Using WHERE with OR AND OR?How to Create Multiple Where Clause Query Using Laravel Eloquent?Converting a MariaDB-Statement into a Laravel4 Eloquent-QueryOrderBy in Laravel 5How To GroupBy the latest record on the groupLaravel error in query that has “as having”Laravel : Select and GroupBy SQL Query Error. Multiple Selects with groupBy? Is it Possible?Laravel Eloquent group by most recent recordIs there any function to get the last entry in groupby in query builder or eloquent?Raw Query TO Laravel Eloquent
Can I lend a small amount of my own money to a bank at the federal funds rate?
Count the number of triangles
Defending Castle from Zombies
Fantasy Macro Economics: What would Merfolk trade for?
Journal published a paper, ignoring my objections as a referee
Term used to describe a person who predicts future outcomes
In Endgame, wouldn't Stark have remembered Hulk busting out of the stairwell?
What is Soda Fountain Etiquette?
Why does Sauron not permit his followers to use his name?
Why can't I identify major and minor chords?
Why is 3/4 a simple meter while 6/8 is a compound meter?
Is this position a forced win for Black after move 14?
Find feasible point in polynomial time in linear programming
Is there an in-universe explanation given to the senior Imperial Navy Officers as to why Darth Vader serves Emperor Palpatine?
Why is the Grievance Studies affair considered to be research requiring IRB approval?
web scraping images
What should be done with the carbon when using magic to get oxygen from carbon dioxide?
Magnesium's Role in Photosynthesis
Stolen MacBook should I worry about my data?
How do I portray irrational anger in first person?
How did medieval manors handle population growth? Were there room for more fields to be ploughed?
Why did Lucius make a deal out of Buckbeak hurting Draco but not about Draco being turned into a ferret?
Which polygons can be turned inside out by a smooth deformation?
How to prevent a hosting company from accessing a VM's encryption keys?
Unexpected behaviour when using groupby in eloquent query for latest records
Get most recent row with group by and LaravelLaravel 4 Eloquent Query Using WHERE with OR AND OR?How to Create Multiple Where Clause Query Using Laravel Eloquent?Converting a MariaDB-Statement into a Laravel4 Eloquent-QueryOrderBy in Laravel 5How To GroupBy the latest record on the groupLaravel error in query that has “as having”Laravel : Select and GroupBy SQL Query Error. Multiple Selects with groupBy? Is it Possible?Laravel Eloquent group by most recent recordIs there any function to get the last entry in groupby in query builder or eloquent?Raw Query TO Laravel Eloquent
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm trying to get the latest records in a group by query using eloquent in laravel.
My current query is:
$clubs = ClubsLooking::select('a.*')
->from(DB::raw('(SELECT *, (3959 * acos( cos( radians('.Auth::user()->latitude.') ) *
cos( radians( latitude ) )
* cos( radians( longitude ) - radians('.Auth::user()->longitude.')
) + sin( radians('.Auth::user()->latitude.') ) *
sin( radians( latitude ) ) )
) AS distance FROM clubs_looking ORDER BY created_at DESC) a'))
->groupBy('a.club_id')
->limit(4)
->get();
The query looks complex but is just using the haversine method of calculating distance between two coordinates and returning the result as a column named distance.
I was under the impression that by using this nested query I would be able to group by using the latest created_at date after reading this answer - Get most recent row with group by and Laravel
However when I run this query I always get the oldest record first, I've tried playing around in multiple ways with it but to no avail.
Does anyone with a larger brain than me know what I'm doing wrong?
laravel eloquent
add a comment |
I'm trying to get the latest records in a group by query using eloquent in laravel.
My current query is:
$clubs = ClubsLooking::select('a.*')
->from(DB::raw('(SELECT *, (3959 * acos( cos( radians('.Auth::user()->latitude.') ) *
cos( radians( latitude ) )
* cos( radians( longitude ) - radians('.Auth::user()->longitude.')
) + sin( radians('.Auth::user()->latitude.') ) *
sin( radians( latitude ) ) )
) AS distance FROM clubs_looking ORDER BY created_at DESC) a'))
->groupBy('a.club_id')
->limit(4)
->get();
The query looks complex but is just using the haversine method of calculating distance between two coordinates and returning the result as a column named distance.
I was under the impression that by using this nested query I would be able to group by using the latest created_at date after reading this answer - Get most recent row with group by and Laravel
However when I run this query I always get the oldest record first, I've tried playing around in multiple ways with it but to no avail.
Does anyone with a larger brain than me know what I'm doing wrong?
laravel eloquent
Can you screenshot your table structure? :)
– Benjamin Beganović
Mar 27 at 21:46
add a comment |
I'm trying to get the latest records in a group by query using eloquent in laravel.
My current query is:
$clubs = ClubsLooking::select('a.*')
->from(DB::raw('(SELECT *, (3959 * acos( cos( radians('.Auth::user()->latitude.') ) *
cos( radians( latitude ) )
* cos( radians( longitude ) - radians('.Auth::user()->longitude.')
) + sin( radians('.Auth::user()->latitude.') ) *
sin( radians( latitude ) ) )
) AS distance FROM clubs_looking ORDER BY created_at DESC) a'))
->groupBy('a.club_id')
->limit(4)
->get();
The query looks complex but is just using the haversine method of calculating distance between two coordinates and returning the result as a column named distance.
I was under the impression that by using this nested query I would be able to group by using the latest created_at date after reading this answer - Get most recent row with group by and Laravel
However when I run this query I always get the oldest record first, I've tried playing around in multiple ways with it but to no avail.
Does anyone with a larger brain than me know what I'm doing wrong?
laravel eloquent
I'm trying to get the latest records in a group by query using eloquent in laravel.
My current query is:
$clubs = ClubsLooking::select('a.*')
->from(DB::raw('(SELECT *, (3959 * acos( cos( radians('.Auth::user()->latitude.') ) *
cos( radians( latitude ) )
* cos( radians( longitude ) - radians('.Auth::user()->longitude.')
) + sin( radians('.Auth::user()->latitude.') ) *
sin( radians( latitude ) ) )
) AS distance FROM clubs_looking ORDER BY created_at DESC) a'))
->groupBy('a.club_id')
->limit(4)
->get();
The query looks complex but is just using the haversine method of calculating distance between two coordinates and returning the result as a column named distance.
I was under the impression that by using this nested query I would be able to group by using the latest created_at date after reading this answer - Get most recent row with group by and Laravel
However when I run this query I always get the oldest record first, I've tried playing around in multiple ways with it but to no avail.
Does anyone with a larger brain than me know what I'm doing wrong?
laravel eloquent
laravel eloquent
asked Mar 27 at 21:03
RockhopperRockhopper
981 silver badge10 bronze badges
981 silver badge10 bronze badges
Can you screenshot your table structure? :)
– Benjamin Beganović
Mar 27 at 21:46
add a comment |
Can you screenshot your table structure? :)
– Benjamin Beganović
Mar 27 at 21:46
Can you screenshot your table structure? :)
– Benjamin Beganović
Mar 27 at 21:46
Can you screenshot your table structure? :)
– Benjamin Beganović
Mar 27 at 21:46
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55386416%2funexpected-behaviour-when-using-groupby-in-eloquent-query-for-latest-records%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55386416%2funexpected-behaviour-when-using-groupby-in-eloquent-query-for-latest-records%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you screenshot your table structure? :)
– Benjamin Beganović
Mar 27 at 21:46