Support the ongoing development of Laravel.io →
Requests Database Eloquent
Last updated 2 years ago.
0

Do You Mean Like That :

//Controller 
$users = DB::table('users')
                ->whereBetween('date', ['10/01', '10/02'])->get();
       

        return view('index',compact('users')) ;

//View
@foreach ($users as $user)
    {{ $element->userid }}
    {{ $element->username }}
    {{ $element->int1 }}
    {{ $element->int2 }}
@endforeach
0

@ABDELRHMAN

whereBetween works successfully for my data, but compact isn't doing what it should be (I think).

Without compact I get multiple rows like my first table.
With compact my view is getting passed an empty array rather than one with users.

I don't know if it makes any difference, but I'm passing additional data to my view in a multidimensional array like so:
[
['users' => (array) $users],
['startmonth' => (string) $sm],
['endmonth' => (string) $em],
]

Last updated 9 years ago.
0

I think I just got it actually. Instead of using Eloquent to query my DB, I used the query builder in the following format:

$users = DB::table('users')  
->select(DB::Raw('sum(int1) as int1'))  
->addselect(DB::Raw('sum(int2) as int2'))  
->addselect('id')  
->addselect('userid')  
->addselect('username')  
->addselect('date')  
->whereBetween('date', [$startdate, $enddate])  
->groupBy('userid')  
->get();  

then passing the $users array to the view like so:

$data['users'] = $users;  
$data['startdate'] = $startdate;  
$data['enddate'] = $enddate;  

return view('view.index')->with('data', $data);

I'll have to do some more testing to be sure it's giving me exactly what I want, but for now it looks good. Hopefully this helps others with the same problem!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

xylotism xylotism Joined 5 Oct 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.