Get first and last element from Eloquent Laravel












1















I would like to know if there is any way to get the first and last value of a data grouped by date, using the Laravel eloquent.
I need the data to put on the TradingView candle chart, and it needs a specific formatting, as follows:



{
t:'unix timestamps',
v:'volumes',
h:'higher prices',
l:'lower prices',
o:'open prices',
c:'close prices'
}


My code is:



$trans = Transacoes::where('created_at', '>=', $end->subMinutes($minute))
->groupBy('t')
->orderBy('t', 'DESC')
->get([
DB::raw('DATE_FORMAT(created_at, "%Y-%m-%d %H:%i") as t'),
DB::raw('sum(amount) as "v"'),
DB::raw('max(`price`) as h'),
DB::raw('min(`price`) as l'),
DB::raw('first(`price`) as o'),
DB::raw('last(`price`) as c'),
])
->toArray();


I know the first() and last() functions do not exist for MySQL, but I wrote that way so it would be easier to understand what I'm trying to do.



My array is currently returning:



{
"t": "2018-11-13 12:20",
"v" : "0.04536078",
"h" : "24249.98",
"l": "24249.97"
},
{...},


I needed to include the opening price and the closing price for each of the arrays, for example: in the minute corresponding to 12:20, there were 4 transactions, I need to know which was the first one and which was the last one at this minute and includes it in each element of the array with index "o" and "c"










share|improve this question

























  • Please add this in your post by editing

    – vivek_23
    Nov 13 '18 at 16:12











  • Also, how are you getting a JSON when you did a toArray() there?

    – vivek_23
    Nov 13 '18 at 16:21











  • No, JSON is as I get in my javascript, toArray() returns me an ordinary array.

    – Erick Crus
    Nov 13 '18 at 20:07













  • Can you show a proper result(don't trim it) and the expected output?

    – vivek_23
    Nov 13 '18 at 20:10











  • brasilbitcoin.com.br/chart/…

    – Erick Crus
    Nov 13 '18 at 20:48
















1















I would like to know if there is any way to get the first and last value of a data grouped by date, using the Laravel eloquent.
I need the data to put on the TradingView candle chart, and it needs a specific formatting, as follows:



{
t:'unix timestamps',
v:'volumes',
h:'higher prices',
l:'lower prices',
o:'open prices',
c:'close prices'
}


My code is:



$trans = Transacoes::where('created_at', '>=', $end->subMinutes($minute))
->groupBy('t')
->orderBy('t', 'DESC')
->get([
DB::raw('DATE_FORMAT(created_at, "%Y-%m-%d %H:%i") as t'),
DB::raw('sum(amount) as "v"'),
DB::raw('max(`price`) as h'),
DB::raw('min(`price`) as l'),
DB::raw('first(`price`) as o'),
DB::raw('last(`price`) as c'),
])
->toArray();


I know the first() and last() functions do not exist for MySQL, but I wrote that way so it would be easier to understand what I'm trying to do.



My array is currently returning:



{
"t": "2018-11-13 12:20",
"v" : "0.04536078",
"h" : "24249.98",
"l": "24249.97"
},
{...},


I needed to include the opening price and the closing price for each of the arrays, for example: in the minute corresponding to 12:20, there were 4 transactions, I need to know which was the first one and which was the last one at this minute and includes it in each element of the array with index "o" and "c"










share|improve this question

























  • Please add this in your post by editing

    – vivek_23
    Nov 13 '18 at 16:12











  • Also, how are you getting a JSON when you did a toArray() there?

    – vivek_23
    Nov 13 '18 at 16:21











  • No, JSON is as I get in my javascript, toArray() returns me an ordinary array.

    – Erick Crus
    Nov 13 '18 at 20:07













  • Can you show a proper result(don't trim it) and the expected output?

    – vivek_23
    Nov 13 '18 at 20:10











  • brasilbitcoin.com.br/chart/…

    – Erick Crus
    Nov 13 '18 at 20:48














1












1








1








I would like to know if there is any way to get the first and last value of a data grouped by date, using the Laravel eloquent.
I need the data to put on the TradingView candle chart, and it needs a specific formatting, as follows:



{
t:'unix timestamps',
v:'volumes',
h:'higher prices',
l:'lower prices',
o:'open prices',
c:'close prices'
}


My code is:



$trans = Transacoes::where('created_at', '>=', $end->subMinutes($minute))
->groupBy('t')
->orderBy('t', 'DESC')
->get([
DB::raw('DATE_FORMAT(created_at, "%Y-%m-%d %H:%i") as t'),
DB::raw('sum(amount) as "v"'),
DB::raw('max(`price`) as h'),
DB::raw('min(`price`) as l'),
DB::raw('first(`price`) as o'),
DB::raw('last(`price`) as c'),
])
->toArray();


I know the first() and last() functions do not exist for MySQL, but I wrote that way so it would be easier to understand what I'm trying to do.



My array is currently returning:



{
"t": "2018-11-13 12:20",
"v" : "0.04536078",
"h" : "24249.98",
"l": "24249.97"
},
{...},


I needed to include the opening price and the closing price for each of the arrays, for example: in the minute corresponding to 12:20, there were 4 transactions, I need to know which was the first one and which was the last one at this minute and includes it in each element of the array with index "o" and "c"










share|improve this question
















I would like to know if there is any way to get the first and last value of a data grouped by date, using the Laravel eloquent.
I need the data to put on the TradingView candle chart, and it needs a specific formatting, as follows:



{
t:'unix timestamps',
v:'volumes',
h:'higher prices',
l:'lower prices',
o:'open prices',
c:'close prices'
}


My code is:



$trans = Transacoes::where('created_at', '>=', $end->subMinutes($minute))
->groupBy('t')
->orderBy('t', 'DESC')
->get([
DB::raw('DATE_FORMAT(created_at, "%Y-%m-%d %H:%i") as t'),
DB::raw('sum(amount) as "v"'),
DB::raw('max(`price`) as h'),
DB::raw('min(`price`) as l'),
DB::raw('first(`price`) as o'),
DB::raw('last(`price`) as c'),
])
->toArray();


I know the first() and last() functions do not exist for MySQL, but I wrote that way so it would be easier to understand what I'm trying to do.



My array is currently returning:



{
"t": "2018-11-13 12:20",
"v" : "0.04536078",
"h" : "24249.98",
"l": "24249.97"
},
{...},


I needed to include the opening price and the closing price for each of the arrays, for example: in the minute corresponding to 12:20, there were 4 transactions, I need to know which was the first one and which was the last one at this minute and includes it in each element of the array with index "o" and "c"







php laravel eloquent






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 20:05







Erick Crus

















asked Nov 13 '18 at 14:16









Erick CrusErick Crus

113




113













  • Please add this in your post by editing

    – vivek_23
    Nov 13 '18 at 16:12











  • Also, how are you getting a JSON when you did a toArray() there?

    – vivek_23
    Nov 13 '18 at 16:21











  • No, JSON is as I get in my javascript, toArray() returns me an ordinary array.

    – Erick Crus
    Nov 13 '18 at 20:07













  • Can you show a proper result(don't trim it) and the expected output?

    – vivek_23
    Nov 13 '18 at 20:10











  • brasilbitcoin.com.br/chart/…

    – Erick Crus
    Nov 13 '18 at 20:48



















  • Please add this in your post by editing

    – vivek_23
    Nov 13 '18 at 16:12











  • Also, how are you getting a JSON when you did a toArray() there?

    – vivek_23
    Nov 13 '18 at 16:21











  • No, JSON is as I get in my javascript, toArray() returns me an ordinary array.

    – Erick Crus
    Nov 13 '18 at 20:07













  • Can you show a proper result(don't trim it) and the expected output?

    – vivek_23
    Nov 13 '18 at 20:10











  • brasilbitcoin.com.br/chart/…

    – Erick Crus
    Nov 13 '18 at 20:48

















Please add this in your post by editing

– vivek_23
Nov 13 '18 at 16:12





Please add this in your post by editing

– vivek_23
Nov 13 '18 at 16:12













Also, how are you getting a JSON when you did a toArray() there?

– vivek_23
Nov 13 '18 at 16:21





Also, how are you getting a JSON when you did a toArray() there?

– vivek_23
Nov 13 '18 at 16:21













No, JSON is as I get in my javascript, toArray() returns me an ordinary array.

– Erick Crus
Nov 13 '18 at 20:07







No, JSON is as I get in my javascript, toArray() returns me an ordinary array.

– Erick Crus
Nov 13 '18 at 20:07















Can you show a proper result(don't trim it) and the expected output?

– vivek_23
Nov 13 '18 at 20:10





Can you show a proper result(don't trim it) and the expected output?

– vivek_23
Nov 13 '18 at 20:10













brasilbitcoin.com.br/chart/…

– Erick Crus
Nov 13 '18 at 20:48





brasilbitcoin.com.br/chart/…

– Erick Crus
Nov 13 '18 at 20:48












1 Answer
1






active

oldest

votes


















0














$first = array_key_exists(0,$trans)?$trans[0]:"";
$last= array_key_exists(count($trans)-1,$trans)?$trans[count($trans)-1]:"";


I assume you have $trans as an array






share|improve this answer
























  • my array is currently returning: {"t": "2018-11-13 12:20", "v" : "0.04536078", "h" : "24249.98", "l": "24249.97"}, {...}, I needed to include the opening price and the closing price for each of the arrays, for example: in the minute corresponding to 12:20, there were 4 transactions, I need to know which was the first one and which was the last one at this minute and includes it in each element of the array.

    – Erick Crus
    Nov 13 '18 at 14:36











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283016%2fget-first-and-last-element-from-eloquent-laravel%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














$first = array_key_exists(0,$trans)?$trans[0]:"";
$last= array_key_exists(count($trans)-1,$trans)?$trans[count($trans)-1]:"";


I assume you have $trans as an array






share|improve this answer
























  • my array is currently returning: {"t": "2018-11-13 12:20", "v" : "0.04536078", "h" : "24249.98", "l": "24249.97"}, {...}, I needed to include the opening price and the closing price for each of the arrays, for example: in the minute corresponding to 12:20, there were 4 transactions, I need to know which was the first one and which was the last one at this minute and includes it in each element of the array.

    – Erick Crus
    Nov 13 '18 at 14:36
















0














$first = array_key_exists(0,$trans)?$trans[0]:"";
$last= array_key_exists(count($trans)-1,$trans)?$trans[count($trans)-1]:"";


I assume you have $trans as an array






share|improve this answer
























  • my array is currently returning: {"t": "2018-11-13 12:20", "v" : "0.04536078", "h" : "24249.98", "l": "24249.97"}, {...}, I needed to include the opening price and the closing price for each of the arrays, for example: in the minute corresponding to 12:20, there were 4 transactions, I need to know which was the first one and which was the last one at this minute and includes it in each element of the array.

    – Erick Crus
    Nov 13 '18 at 14:36














0












0








0







$first = array_key_exists(0,$trans)?$trans[0]:"";
$last= array_key_exists(count($trans)-1,$trans)?$trans[count($trans)-1]:"";


I assume you have $trans as an array






share|improve this answer













$first = array_key_exists(0,$trans)?$trans[0]:"";
$last= array_key_exists(count($trans)-1,$trans)?$trans[count($trans)-1]:"";


I assume you have $trans as an array







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 14:21









Danyal SandeeloDanyal Sandeelo

8,30422242




8,30422242













  • my array is currently returning: {"t": "2018-11-13 12:20", "v" : "0.04536078", "h" : "24249.98", "l": "24249.97"}, {...}, I needed to include the opening price and the closing price for each of the arrays, for example: in the minute corresponding to 12:20, there were 4 transactions, I need to know which was the first one and which was the last one at this minute and includes it in each element of the array.

    – Erick Crus
    Nov 13 '18 at 14:36



















  • my array is currently returning: {"t": "2018-11-13 12:20", "v" : "0.04536078", "h" : "24249.98", "l": "24249.97"}, {...}, I needed to include the opening price and the closing price for each of the arrays, for example: in the minute corresponding to 12:20, there were 4 transactions, I need to know which was the first one and which was the last one at this minute and includes it in each element of the array.

    – Erick Crus
    Nov 13 '18 at 14:36

















my array is currently returning: {"t": "2018-11-13 12:20", "v" : "0.04536078", "h" : "24249.98", "l": "24249.97"}, {...}, I needed to include the opening price and the closing price for each of the arrays, for example: in the minute corresponding to 12:20, there were 4 transactions, I need to know which was the first one and which was the last one at this minute and includes it in each element of the array.

– Erick Crus
Nov 13 '18 at 14:36





my array is currently returning: {"t": "2018-11-13 12:20", "v" : "0.04536078", "h" : "24249.98", "l": "24249.97"}, {...}, I needed to include the opening price and the closing price for each of the arrays, for example: in the minute corresponding to 12:20, there were 4 transactions, I need to know which was the first one and which was the last one at this minute and includes it in each element of the array.

– Erick Crus
Nov 13 '18 at 14:36


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283016%2fget-first-and-last-element-from-eloquent-laravel%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Coverage of Google Street View

Full-time equivalent

Surfing