Using foreach in Laravel 5.1 - php

I'm new in Laravel. I'm storing 3 types of user in my users table:
admin : user_type_id = 1
agent : user_type_id = 2
farmer : user_type_id = 3
user_type_id column is in the users table.
In one of my Blade files, I want to only show the names of the agents. Here is my foreach loop, which is importing all 3 types of the user (it's showing the names of admins, agents and farmers).
#foreach($farmerPoint->user as $agent)
<tr>
<td>{{ $agent->name }}</td>
<td>{{ $agent->phone }}</td>
</tr>
#endforeach

This is probably more of a logic issue than a Laravel issue. NOTE that I would suggest you limit the $agents instead using your query (where) rather than this way, BUT:
#foreach($farmerPoint->user as $agent)
#if ($agent->user_type_id === 2)
<tr>
<td>{{ $agent->name }}</td>
<td>{{ $agent->phone }}</td>
</tr>
#endif
#endforeach

You can use a simple blade #if() statement like so:
#foreach($farmerPoint->user as $agent)
#if ($agent->user_type_id === 2)
<tr>
<td>{{ $agent->name }}</td>
<td>{{ $agent->phone }}</td>
</tr>
#endif
#endforeach
Or you can use a collection where() since all eager / lazy loaded relations are returned in collections:
http://laravel.com/docs/5.1/collections#method-where
#foreach($farmerPoint->user->where('user_type_id', 2) as $agent)
<tr>
<td>{{ $agent->name }}</td>
<td>{{ $agent->phone }}</td>
</tr>
#endforeach

Related

print days of a special month ? laravel

I need to print only the records for the month of May
I get them with the variable
<td>{{ ($registro->created_at->format('d-m-Y')) }}</td>
I need to see only May results, how do I do it?
You can use if condition
#if($user->created_at->format('M')=="May")
<td>{{ ($registro->created_at->format('d-m-Y')) }}</td>
#endif
or
#if($user->created_at->format('m')=="05")
<td>{{ ($registro->created_at->format('d-m-Y')) }}</td>
#endif
Simply check May month in created_at date
#if($user->created_at->format('m')=="05")
<td>{{ ($registro->created_at->format('d-m-Y')) }}</td>
#endif

Laravel modify cells with expired date

Sorry I didn´t know how to ask this question, but I want if a file reach his expiration date in each cell of expired date column just put "expired" otherwise just put his expiration date.
In my controller I create an variable called $actualDate= Carbon::now(); and I send it to my view.
In my view I have this on table:
#foreach ($Files as $File)
<tr>
<td><a href="/File/{{$File->id}}">{{
$File->Code}}</td>
<td>{{ $File->created_at }}</td>
<td>{{ $File->person}}</td>
<td>{{ $File->category_file}}</td>
<td>{{ $File->status}}</td>
<td>{{ $File->employed}}</td>
#if($File->expirationDate < $actualDate)
<td>{{$File->expirationDate}}</td>
#else
<td>Expired</td>
#endif
If I put < on comparision all the column will be "expired" and if I put > it will be no expired Files even if there are ¿what can I do to solve this? I hope you can help me.
Try it:
#foreach ($Files as $File)
<tr>
<td><a href="/File/{{$File->id}}">{{ $File->Code}}</td>
<td>{{ $File->created_at }}</td>
<td>{{ $File->person}}</td>
<td>{{ $File->category_file}}</td>
<td>{{ $File->status}}</td>
<td>{{ $File->employed}}</td>
#if(\Carbon\Carbon::parse($File->expirationDate)->lessThan(\Carbon\Carbon::now()))
<td>{{$File->expirationDate}}</td>
#else
<td>Expired</td>
#endif
#endforeach
Hope it helps.

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id

Can anyone help me complete this? I'm a beginner and this is my first time using Laravel and PHP.
<?php
#foreach ($peminjaman as $value)
<tbody>
<tr>
<td>{{ ++$i }}</td>
<td>{{ $value->no_peminjaman }}</td>
<td>{{ $value->no_bmn }}</td>
<td>{{ $value->no_seri }}</td>
<td>{{ $value->kelengkapan }}</td>
</tr>
</tbody>
#endforeach
You need to add links(), so that the links for pagination can be generated in your view.Just have a look at the documentation here and you're good to go:https://laravel.com/docs/5.7/pagination
Hope this helps.
When you do :
$peminjaman = Model::paginate(10);
The $peminjaman is an instance of Illuminate\Pagination\LengthAwarePaginator.
In your case it seems like somewhere you have $peminjaman->id which as a result throwing the error you mentioned.
There is no property called id present on the LengthAwarePaginator paginator instance.

Change attribute inside foreach in laravel

So I'm using laravel, and I have a table with id, category_name, parent_id, desc, and url.
Then in the view, I foreach the table with this code:
#foreach($categories as $category)
<tr class="gradeX">
<td>{{ $category->id }}</td>
<td>{{ $category->name }}</td>
<td>{{ $category->parent_id }}</td>
<td>{{ $category->description }}</td>
<td>{{ $category->url }}</td>
<td class="center">
Edit
Delete
</td>
</tr>
#endforeach
now, instead of showing parent_id, I want to show the parent category name.
I've tried a lot of things but nothing work.
I know this might be a very easy question, but I'm just started learning web development by myself. So please bear with me.
Thank you.
Define the parent relationship in the Category model. https://laravel.com/docs/5.7/eloquent-relationships
Eager load that relationship in the query where you are fetching the categories. https://laravel.com/docs/5.7/eloquent-relationships#eager-loading
Access the loaded relationship in the view, eg. $category->parent->name

Compare two different columns from two different tables and show other column's data from either table in laravel 5.2

Compare two different columns from two different tables with if statement and show other column's data from either table in laravel 5.2
controller like:
public function labDetails(){
$users = DB::table('users')->lists('lab_name');
$labdetails = Labdetails2::paginate(20);
return View('labdetails')
->with('users', $users)
->with('labdetails', $labdetails);
}
here i want to match users table's 'lab_name' column with labdetails2 table's 'lab_name' column and if they matched show only the related data from labdetails table.
N.B. I am working with an old database where there's no relationship among tables.
views like:
<form action="" method="">
<select name="state" onchange="getValue(this)">
<option value="">Select Lab</option>
#foreach ($users as $key => $user)
<option value="{{ $user }}">{{ $user }}</option>
#endforeach
</select>
<input type="submit" value="Submit">
#foreach ($labdetails as $labdetail)
<tbody>
<tr>
<td>{{ $labdetail->sl }}</td>
<td>{{ $labdetail->lab_name }}</td>
<td>{{ $labdetail->pc_name }}</td>
<td>{{ $labdetail->pc_ip }}</td>
<td>{{ $labdetail->mac1 }}</td>
<td>{{ $labdetail->assetno }}</td>
<td>{{ $labdetail->pc_type }}</td>
<td>{{ $labdetail->processor }}</td>
<td>{{ $labdetail->motherboard }}</td>
<td>{{ $labdetail->ram }}</td>
<td>{{ $labdetail->hdd }}</td>
<td>{{ $labdetail->location }}</td>
<td>{{ $labdetail->department }}</td>
<td>{{ $labdetail->entrydate }}</td>
<td>{{ $labdetail->comment }}</td>
</tr>
</tbody>
#endforeach
Please help me what should I do with the controller & views...
You can retrieve only the matches record this way
$users = DB::table('users')->lists('lab_name'); // this retrieves all lab_name as array.
Then, to retrieve only the matches labdetails
$labdetails = Labdetails2::whereIn('lab_name',$users)->paginate(20);
DB::table('users')
->join('labdetails2', 'users.lab_name', '=', 'labdetails2.lab_name')
->select('labdetails2.lab_name', 'labdetails2.pc_name')
->get()
in select(), white down the fields you required.
Here, labdetails2 and users are the table name.
this code finally worked for me:
public function showLabDetails(Request $request){
$q = $request -> request -> all();
$labdetails = Labdetails2::whereIn('lab_name', $q)->get();
return View('showlabdetails')
->with('labdetails', $labdetails);
}
thanks to everyone for helping me.

Categories