I need some help with Laravel, I'm trying to do a list, with information from my database, with a count, for example:
List image
How can I make to count how many times the same place appears in my list? Remembering: My place list comes from my DB, the place can variable. I can't make it count. Here is my code:
<table border=`1px` class="table-teste">
<tr>
<td><h3> Place</h3></td>
<td><h3> Count</h3></td>
</tr>
#foreach ($setor_data as $setor)
<tr>
<td>{{$setor->setor}}</td>
</tr>
#endforeach
</table>
And my Controller:
$setor_data = DB::table('atividades')
->where('local', $condominio)
->whereDate('date', '>=', $dtinicio)
->whereDate('date','<=',$dtok)
->orderBy("setor")
->select('setor')
->get();
User groupBy to count each item in list in the query, like following:
$setor_data = DB::table('atividades')
->where('local', $condominio)
->whereDate('date', '>=', $dtinicio)
->whereDate('date','<=',$dtok)
->orderBy("setor")
->select('setor', , DB::raw('count(*) as total'))
->get();
Then in the blade view:
#foreach ($setor_data as $setor)
<tr>
<td>{{$setor->setor}}</td>
<td>{{$setor->total}}</td>
</tr>
#endforeach
Hope, it helps...
Related
I have a select script that displays team names and the match results, the script looks like this ->
select match.ID, match.ht_ID, ht.Name as ht_Name, match.at_ID, at.Name as at_Name, match_results.Results
from match_results
join match on mattch.ID = match_results.Match_ID
join teams ht on match.ht_ID = ht.ID
join teams at on mtach.at_ID = at.ID`
My Laravel controller has this function:
DB::table('results')
->select('match.ID', ht.Name', 'at.Name', 'results.Results')
->join('match','match.ID','=','results.Match_ID')
->join('teams as ht','match.ht_ID','=','ht.ID')
->join('teams as at','match.at_ID','=','at.ID')
->get();
return view('results',['result' => $result]);
My blade:
#foreach($result as $results)
<tr>
<td> {{$results->ID}} </td>
<td> {{$results->ht.Name}} </td>
<td> {{$results->at.Name}} </td>
<td> {{$results->Rezultats}} </td>
</tr>
#endforeach
How can I code it so ht.Name is replaced by the home team name and at.Name is replace by the away team name?
Thank you in advance!
You can use:
->select('match.ID', 'ht.Name as home_team_name', 'at.Name as away_team_name', 'results.Results')
I am writing a function in laravel 5.8 in which I want to get sum of the user amount that lies within my selected range, I am using the two input fields in view which they get the price range "T0 and From" e.g if the user enters 100 and 1000, it should show the list of the users who has amount > 100 and amount < 1000. The problem is there are multiple records of the same users as shown below in the table.
I want to sum the amount of the users and show the user if their amount is within range
table name= deposits
<table>
<thead>
<tr>
<th>id</th>
<th>user_id</th>
<th>name</th>
<th>amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>12</td>
<td>ali</td>
<td>100</td>
</tr>
<tr>
<td>2</td>
<td>12</td>
<td>ali</td>
<td>800</td>
</tr>
<tr>
<td>3</td>
<td>12</td>
<td>ali</td>
<td>50</td>
</tr>
<tr>
<td>4</td>
<td>15</td>
<td>khan</td>
<td>1100</td>
</tr>
<tr>
<td>6</td>
<td>9</td>
<td>james</td>
<td>850</td>
</tr>
<tr>
<td>7</td>
<td>9</td>
<td>james</td>
<td>90</td>
</tr>
</tbody>
</table>
This behavior of your query is correct, to get what you need it is necessary to work on the query.
Assuming this fields in your deposits table: id, user_id, name, amount in according with data showing in the table.
You need to sum the amount grouping by user_id
Example query:
$deposit = Deposits::select(
'id',
'user_id',
'name',
'sum(amount)',
)->whereBetween('amount', [100, 1000])
->groupBy('user_id')->get();
DB::table('deposits')
->selectRaw('user_id, SUM(amount) as total_amount')
->where('amount', '>', '100')
->where('amount', '<', '1000')
->groupBy('user_id')
->get();
Result is:
[
{
"user_id":9,
"total_amount":850
},
{
"user_id":12,
"total_amount":800
}
]
app('db')
->table('users')
->select('name')
->selectRaw("SUM(amount) as total_amounts")
->havingBetween('total_amounts', [ 100, 1000 ])
->groupBy('user_id')
->get();
You can use the raw query for the sum per user and use the having between for the sum of amounts.
I am self learner and stuck at one point. I don't know is that possible or not. Please refer image of data get in array.
I have 3 database table "animalsell", "dairyincomes" and "othersell". I sucessfully able to get required data from this.
Now I want to put the same in one table in blade file (refer table image)
Controller file where i collect the data
$fiveincomedata = Dairyincome::select(
DB::raw('DATE_FORMAT(milksaledate, "%M %Y") as "month_name",SUM(totalamount) as "totalamount", max(milksaledate) as milksaledate')
)
->whereBetween('milksaledate', [Carbon::now()->subMonth(6), Carbon::now()->endOfMonth()])
->groupBy('month_name')
->orderBy('milksaledate', 'desc')
->get()->toArray();
// Income from Animal Sell
$fiveanimalselldata = Animalsell::select(
DB::raw('DATE_FORMAT(selldate, "%M %Y") as "month_name",SUM(totalamount) as "totalamount", max(selldate) as selldate')
)
->whereBetween('selldate', [Carbon::now()->subMonth(6), Carbon::now()->endOfMonth()])
->groupBy('month_name')
->orderBy('selldate', 'desc')
->get()->toArray();
// Income from Other Sell
$fiveotherselldata = Othersell::select(
DB::raw('DATE_FORMAT(saledate, "%M %Y") as "month_name",SUM(totalamount) as "totalamount", max(saledate) as saledate')
)
->whereBetween('saledate', [Carbon::now()->subMonth(6), Carbon::now()->endOfMonth()])
->groupBy('month_name')
->orderBy('saledate', 'desc')
->get()->toArray();
Blade File
<div class="card-body">
<table id="incometable" class="table m-0 table table-bordered table-hover table" style="width:100%">
<thead>
<tr>
<th>Date</th>
<th>Amount (Milk Sale)</th>
<th>Amount (Animal Sale)</th>
<th>Amount (Other Sale)</th>
<th>Total Amount </th>
</tr>
</thead>
<tbody>
#foreach ($fiveincomedata as $fivei )
<tr>
<td>{{ $fivei->month_name }}</td>
<td>{{$fivei->totalamount}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
Hope i explain my problem clearly and thanks for help in advance.
$a1=array("red","green");
$a2=array("blue","yellow");
array_merge($a1,$a2);
or
push the data into a common array.
You can use the merge() function.
For example:
$fiveincomedata = Dairyincome::all();
$fiveanimalselldata = Animalsell::all();
$fiveotherselldata = Othersell::all();
$merged = $fiveincomedata->merge($fiveanimalselldata);
$merged = $merged->merge($fiveotherselldata);
$array = $merged->toArray();
More information: https://laravel.com/docs/9.x/collections#method-merge
This is my function in the controller:
$bssr = table::('adv')
->leftjoin('picture', 'adv.id', '=', 'picture.advid')
->select('adv.*', 'picture.path)
->where('adv.id', '=', '1')
->paginate(20);
return view('bssr.category')->with('bssr', $bssr);
Then I have my blade as follows:
#foreach($bssr as $data)
<table>
<tr>
<td width="20%">
<img src="{{url('avatar/'.$data->path) }}">
</td>
<td>
{{ $data->advtopic }}
</td>
</tr>
</table>
#endforeach
I have my tables as follows:
Table: adv
id advtopic
1 my adv name
Table :picture
id path advid
1 mypath1 1
2 mypath2 1
3 mypath3 1
Shouldn't my result in blade return only 1 row?
The result gives 3 rows.
Can anyone help me to return only 1 row based on table 'adv'? Any help will be highly appreciated.
I think you should try this:
$bssr = table::('adv')
->leftjoin('picture', 'picture.advid', '=', 'adv.id')
->select('adv.id AS Id','adv.advtopic AS advtopic', 'picture.path AS path')
->where('adv.id', '=', '1')
->groupBy('picture.advid')
->paginate(20);
return view('bssr.category',compact('bssr'));
#foreach($bssr as $data)
<table>
<tr>
<td width="20%">
<img src="{{url('avatar/'.$data->path) }}">
</td>
<td>
{{ $data->advtopic }}
</td>
</tr>
</table>
#endforeach
Hope this work for you !!!
On your case, Yes, it will return 3 result because your query matches 3 rows on the left table (picture table). I don't know what you want to get from picture table, Do you want the recent one? the first one? or the last one? or just a random one?
$bssr = table::('adv')
->leftjoin('picture', 'adv.id', '=', 'picture.advid')
->select('adv.*', 'picture.path)
->where('adv.id', '=', '1')
->groupBy('adv.id') // try adding this if you want that result
->paginate(20);
return view('bssr.category')->with('bssr', $bssr);
Please modify your question so i can modify my answer to. Don't forget to include what you want to get on picture table.
The following code works fine, except that there is some "cleaning up" of the data that is taking place in one instance and not the other that I can't decipher.
I have a table (clientdocs) with a field 'from' that contains string data in the format:
Richard Robinson <richard#gmail.com>
In my controller, I have code that builds an array for a dropdown from this table:
// get clients
$sender_sel = array('Any Sender');
$senders = Clientdoc::orderBy('from', 'ASC')
->distinct()
->get( array('from') );
foreach($senders as $sender) {
$sender_sel[] = $sender->from;
}
I also retrieve all the data from the table and pass it to the view (along with the above array and other stuff):
return View::make('clientdocs/index')
->with('date_sel', $date_sel)
->with('sender_sel', $sender_sel)
->with('docs', Clientdoc::orderBy('date_emailed', 'ASC')->get());
Now, in my view:
I use $sender_sel as:
{{ Form::select('from', $sender_sel, '', array(
'class' => 'form-control'
)) }}
and it displays as expected:
"Richard Robinson <richard#gmail.com>"
and I also use data from the 2nd query (docs object):
<table class="table table-striped table-hover">
<tr>
<th>Date</th>
<th>From</th>
<th>Type</th>
</tr>
#foreach ($docs as $doc)
<?php $detail_url = 'clientdocs/' . $doc->id . '/edit' ; ?>
<tr>
<td>{{ $doc->from }}</td>
</tr>
#endforeach
</table>
Here's the kicker: $docs->from displays as
"Richard Robinson", without the "<richard#gmail.com>"
Any ideas why?
View source. It's still there, but < and > are special characters in HTML (as they're used in HTML tags).
Using triple brackets escapes output in Blade, so {{{ $doc->from }}} will fix it by turning it into < and >.