Displaying query results when using table aliases - laravel

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')

Related

Laravel show in the table nested categories with parent_id

I have categories table (id, category_name, parent_id) with following data.
id-category_name-parent_id
1-Electronics-NULL
2-Mobile Phones-1
3-Smart Watches-1
4-Laptops-1
5-Batteries-4
In the Controller retrieves all categories that parent_id is NULL
public function index()
{
$categories = Category::whereNull('parent_id')->get();
return view('catalogs.categories.v2.index',compact('categories'));
}
In the index.blade.php I tried something using treeview heirarchy
<tbody>
#foreach($categories as $category)
<tr>
<td></td>
<td>{{ $category->category_name }}</td>
<td></td>
</tr>
<tr>
<td></td>
<td>
{{ $category->category_name }}
#if(count($category->childs))
#include('catalogs.categories.v2.manageChild',['childs' => $category->childs])
#endif
</td>
<td>{{$category->childs_count() }}</td>
<th></th>
</tr>
#endforeach
</tbody>
#foreach($childs as $child)
>> {{ $child->category_name }}</span>
#if(count($child->childs))
#include('catalogs.categories.v2.manageChild',['childs' => $child->childs])
#endif
#endforeach
Now, my output in my table in the view is like as
Category Name
Electronics
Electronics >> Mobile Phones >> Smart Watches >> Battery >> Laptops
I want to create something like this
Category Name
Electronics
Electronics >> Mobile Phones
Electronics >> Smart Watches
Electronics >> Laptops >> Battery
Can anyone how to fix this?
In your case you can imagine your categories as nodes and the relations between theme as edges
so to search in depth for each category's sons you need to use graph search algorithms such like DFS,
in this link you could know more better about DFS:
DFS IN GENERAL
show case for similar problem solved IN DFS

Laravel, trying to make a count with values from database

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...

Laravel Building Referral System- Cant display Users in Blade

I Building an Affiliate System for my Users. Registration with ID works fine and save all correct in my DB, but i cant see any Referrals that use my URL ID for Registration in my Example Blade.
Any one cant find the Error?
Controller:
$referal_id = $user->id;
$referer_id = $request['referal_id'];
$referalUserData = [
'referer_id' => $referer_id,
'referal_id' => $referal_id,
];
$insert = ReferralUser::insert($referalUserData);
Blade:
<tr>
<th>#</th>
<th>User Id#</th>
<th>User Name</th>
<th>Registration Time</th>
</tr>
</thead>
<tbody>
#if(!empty($referalUser))
#foreach($referalUser as $referData)
<tr>
<th scope="row">{{$referData->id}}</th>
<td>{{$referData->referal_id}}</td>
<td>#for($i=0;$i<count($referData['names']);$i++) {{$referData['names'][$i]['name']}} #endfor
</td>
<td> {{$referData->created_at}}<br/> <span class="label label-primary">
{{Carbon\Carbon::createFromTimestamp(strtotime($referData->created_at))->diffForHumans()}} </span> </td>
</tr>
#endforeach
#else
<div class="alert alert-noreferals">
<strong></strong><span>No referrals!</span>
</div><br/><br/>
#endif
Ignore the created_at Timestamp.
Thanks
Did you convert the $request object to an array?
$request['referal_id'];
That looks suspicious. Typically you would use
$request->input('referal_id')
or via dynamic properties
$request->referal_id
To access a given value.
Are you getting an error? Anything in the log files?

left join and foreach displays no of rows of secondary table in laravel

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.

keeping smarty index on page reload

I got a question.
i have tha following foreach loop:
{foreach from=$films item=film key=id}
<tr>
<td>
{$film.filmtitel}
</td>
<td>
{$film.zaaltitel}
</td>
<td>
plaats {$film.stoeltjes}
</td>
<td>
{$film.dag}
</td>
<td>
{$film.tijdstip}
</td>
<td>
5€
</td>
<td>
<a href="?page=winkelwagentje&action=verwijder&id={$smarty.forach.id.index}">
<img src="images/verwijderButton.png" alt="verwijderButton" title="verwijderButton"/></a>
</td>
</tr>
{/foreach}
these represent various items. now when its displayedin the browser the link gives me the url, ending with:
id=0 then the next is id=1 and so on.
When i remove 1 of these items through a button, the id's automaticly get rearranged so the index starts back with 0, then 1 and so on. What i would like to have is that if for instance i remove item with id=1 and the page gets refreshed, the id should remain deleted.
So the urls would look like:
id=0
id=2
id=3
anyone know if this is possible?
I understand your problem. You could rely on the keys of the array, but in this case you should populate the array in the same order and with the same id. It seems for me that this is a result set from a database table. It would be much easier to rely on an id of the database table which has unique values.
btw. you have a typo in your example code (...id={$smarty.forach...)

Resources