This is how I wish to be
Actual
Status gets printed 3 times
I return this objects from my controller:
return view('ViewTicket') ->with('tickets', $tickets)
->with('user', $user)
->with('priority', $priority)
->with('status', $status)
->with('type', $type);
However I want to print the respective fields like in my view:
#foreach ($tickets as $t)
<tr>
<td> {{$t->id}} </td>
#foreach ($user as $u)
#if($t->user_id==$u->Id)
<td>{{ $u->UserName }}</td>
#endif
#endforeach
Even this doesnt solve my problem.Is there any way to avoid the loop inside the loop to get these data?The goal is to get respective fields for each ticket
If I dd($user) it returns 3 values okay
when I loop in my view it displays 9 values,which means it loops 3 times the lements
Thanks in Advance
ASSUMPTIONS
$tickets is a collection of ticket objects.
$user is a collection of user objects.
CODE
#foreach ($tickets as $t)
<tr>
#foreach ($user as $u)
#if($t->user_id == $u->Id)
<td> {{$t->id}} </td>
<td>{{ $u->UserName }}</td>
#endif
#endforeach
</tr>
#endforeach
Related
I'm trying to make a show view with Laravel 8 but i can't show the detail, this is the code from the controller:
public function show($id)
{
$accesorios=DB::table('accesorio as acc')
->join('detalle_aparato as da','acc.idAccesorio','=','da.idAccesorio')
->select('acc.Nombre')
->where('da.idAparato','=',$id);
return view("almacen.aparato.show",["accesorio"=>Accesorio::findOrFail($id)]);
}
And this is the code from the view:
#foreach ($accesorio as $acc)
<tr>
<td>{{ $acc->Nombre}}</td>
</tr>
#endforeach
Error message
When I use:
#foreach ($accesorio as $acc)
<tr>
<td>{{ $acc}}</td>
</tr>
#endforeach
It prints: 1 for each record
Hope you can help me
In you're controller you're using DB::Table to set the $accesorios variable, but never using it.
You then are setting accesorio in your view to Accesorio::findOrFail($id) which will only return one instance of the object.
Either pass $accessorios into your view
return view("almacen.aparato.show",["accesorios"=>$accessorios]);
then loop through it
#foreach ($accesorios as $acc)
<tr>
<td>{{ $acc->Nombre}}</td>
</tr>
#endforeach
or since you're just sending one instance of the object to the view, remove the loop and you can render it like this.
<tr>
<td>{{ $accesorio->Nombre }}</td>
</tr>
i cant access my admin_table i know i have and problem on my route on the admin_table please help me on my route Property [id] does not exist on this collection instance.
my admin_table.blade
#foreach ($users as $positions)
#foreach ($positions->admins as $position )
<tbody>
<tr>
<td>{{ $position->id}}</td>
<td>{{ $position->first_name}}</td>
<td>{{ $position->last_name}}</td>
<td>{{ $position->contact}}</td>
<td>{{ $position->departments->department}}</td>
<td>{{ $position->schoolpositions->school_position}}</td>
<td>{{ $position->email}}</td>
<th> Edit
</th>
</tr>
</tbody>
#endforeach
</tr>
</tbody>
#endforeach
#endforeach
</table>
this is my route
Route::get('/admin_table', 'DashboardController#index');
Route::put('/admin_table/{id}', 'DashboardController#store');
Route::get('/admin_table', 'DashboardController#show');
Route::get('/teacher_editform/{id}', 'DashboardController#edit');
Route::put('/teacher_editform/{id}', 'DashboardController#update')->name('teacheradminpage.teacher_tableform.teacher_editform');
this is my controller
public function edit($id)
{
$departments = Department::find($id);
$users = Schoolposition::find($id);
$students = Student::find($id);
$admins = Admin::find($id);
return view('teacheradminpage.teacher_tableform.teacher_editform', compact('departments','users','students','admins', 'id', 'id', 'id', 'id'));
}
Your issue is that you put an extra $position before the array. Your route should be
Edit
You've got several issues in this code that probably need attention. The main issue with the position error, seems to be that you've got a circular path to get that $position in your anchor tab within the loop.
This section is likely not creating what you want:
#foreach ($users as $positions)
#foreach ($positions->admins as $position )
This is roughly saying - take my collection of users and make a new collection of positions that are directly attached to each user object. From here, take that collection of positions and try and find a collection of admins on that are attached to that collection and then find a $position. (I think this is close to what it is trying to do...)
When you get to the inside of the loop and your anchor / route, you are asking the anchor to find an id on something that is likely either non-existent, or a collection again.
You have already passed in admins to this view. It would make sense to use that as your top level loop item, no?
To fix, use admins as the only loop item:
#foreach ($admins as $position )
Edit Your Blade With
#foreach ($users as $positions)
#if(!empty($positions->admins)
#foreach ($positions->admins as $position )
<tbody>
<tr>
<td>{{ $position->id}}</td>
<td>{{ $position->first_name}}</td>
<td>{{ $position->last_name}}</td>
<td>{{ $position->contact}}</td>
<td>{{ $position->departments->department}}</td>
<td>{{ $position->schoolpositions->school_position}}</td>
<td>{{ $position->email}}</td>
<th> Edit
</th>
</tr>
</tbody>
#endforeach
</tr>
</tbody>
#endforeach
#endif
#endforeach
</table>
I am trying to show the table like below image
And i write the logic for this table creation is below
#foreach($users as $key => $user)
<tr>
<td>{{$key+1}}</td>
<td>{{$user->name}}</td>
<td rowspan="{{count($users->where('area',$user->area))}}">
{{$user->userarea->name}}</td>
<td rowspan="{{count($user->candidate->election->candidates)}}">{{$user->candidate->election->name}}</td>
</tr>
#endforeach
</tbody>
But this code produce me the following code like this
Here The Election and Candidate has one to many relationships and candidate and user has one one to one relationshipHelp me achieve my expected results.
You could keep track of which elections/area's have been rendered already. This could be done by creating an array containing a reference to these objects. Then in the template just add an if statement checking whether an election/area has been rendered:
<?php $renderedElections = []; ?>
<?php $renderedAreas = []; ?>
#foreach($users as $key => $user)
<tr>
<td>{{$key+1}}</td>
<td>{{$user->name}}</td>
#if (!in_array($user->userarea->name, $renderedAreas))
<?php $renderedElections[] = $user->userarea->name ?>
<td rowspan="{{count($user->userarea->name)}}">
{{$user->userarea->name}}
</td>
#endif
#if (!in_array($user->candidate->election->name, $renderedElections))
<?php $renderedElections[] = $user->candidate->election->name ?>
<td rowspan="{{count($user->candidate->election->candidates)}}">
{{$user->candidate->election->name}}
</td>
#endif
</tr>
This is not the best solution, but its simple and easy. For this to work, the users must be sorted perfectly by election and area.
This code is untested but should theorethically work. I tryied to gather informations about your model relationships from the code you posted, but it might need some tweaking with the model names and relations.
The following code basically fetches all the users each with their area, candidate and candidate.election relationships, then group users by 'election name' (first criteria), then 'area name' (second criteria).
The resulting array will be something like this:
// $elections will be:
// [
// 'Presidential' => [
// 'Dhaka-5' => [ candidates for that area in that election type ],
// 'Dhaka-1' => [ candidates for that area in that election type ],
// ...
// ],
// ...
// ]
In your controller do:
$elections = User::with('area', 'candidate.election')->get()->groupBy(function ($user) {
return $user->candidate->election->name;
}, function ($user) {
return $user->area->name;
});
// then pass $elections to the view...
Then in your view:
<table>
#php ($i = 1)
#foreach ($elections as $election => $areas)
#foreach ($areas as $area => $candidates)
#php ($areaLoop = $loop)
#foreach ($candidates as $user)
<tr>
<td>{{ $i++ }}</td>
<td>{{ $user->name }}</td>
#if ($loop->first)
<td rowspan="{{ $candidates->count() }}">{{ $area }}</td>
#endif
#if ($areaLoop->first)
<td rowspan="{{ $areas->sum(function ($area) { return $area->count(); }); }}">{{ $election }}</td>
#endif
</tr>
#endforeach
#endforeach
#endforeach
</table>
Note that $candidates->count() will be the number of candidates for that particular election area of the current election type. $areas->sum(...) will sum the number of candidates for each area to get the total count of candidates in that election type.
If you need further explaination or something doesn't work, just let me know. All the documentation about the collection functions I used is available here.
I have a simple foreach block in my view, like below.
#foreach ($teams as $key => $team)
{{ str_ordinal($key + 1) }}
#endforeach
Right now I'm displaying the key, although it isn't exactly accurate. Here's an image:
How can I display the actual position of the current iteration? I order by my teams collection but I'm not sure how I get the position of the current interation in that loop?
You can use $loop->index to get the index. Check its docs. For instance:
#foreach ($teams as $team)
{{ $loop->index }}
#endforeach
Will display 0,1,2,3,4... until the last element position.
You can apply array_values to your data before passing to template:
array_values($teams);
Or, according to this https://laravel.com/docs/5.6/blade#the-loop-variable, you can use special $loop variable. I suppose you need $loop->iteration property (it starts with 1) or $loop->index (starts with 0):
#foreach ($teams as $key => $team)
{{ $loop->iteration }}
#endforeach
#foreach($teams as $team)
<tr>
<td>{{ $loop->index+1}}</td>
<td>{{ $team->name}}</td>
<td>{{ $team->caption}}</td>
<td>{{ $team->address}}</td>
</tr>
#endforeach
Use a for loop instead of a foreach. You will get them in order.
$count = count($teams) ;
for($i=1;$i<=$count;$i++) {
// your code here using $i as the position
}
In Controller
foreach ($partners as $key => $partner) {
$data['id'] = $key + 1;
}
I have solved the same issue with the combination of built in loop variable and php directive
*this is wrong* You cannot use it directly in double curly braces
{{ $loop->index }}
Instead use it like this,
#foreach ($users as $user)
#php($count= $loop->index + 1)
<tr>
<th>{{ $count }}</th>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->created_at }}</td>
</tr>
#endforeach
Though $loop variable is available inside foreach, you cannot use it inside the curly braces in blade template.
use this only it works fine {{$key+1}}
I have the following controller and models:
Appointments Controller:
if ($view === 'default') {
$appointments = Appointment::with('label')->with('status')->paginate(25);
}
if ($view === 'label') {
$appointments = Label::with('appointments')->paginate(25);
}
Appointment Model:
public function label()
{
return $this->belongsTo('App\Label');
}
Label Model:
public function appointments()
{
return $this->hasMany('App\Appointment');
}
In my "default" view, the following is displayed:
And in my "label" view, the following is displayed:
What I want to accomplish is, when a label has no appointments, I do not want the label to be shown at all. So, in my "label view" (check out the below image) I only want the "business" and "personal" label (and it's appointments as shown in image) to be displayed, and all the other labels shouldn't be displayed.
In my views I am using simply foreach loops. Any pointers in the right direction?
My view (simple):
#if ($view === 'label')
#foreach ($appointments as $appointment)
<table>
<tr>
<th><p>{{ $appointment->label }}</p></th>
</tr>
#foreach ($appointment->appointments as $label)
<tr>
<td>{{ $label->appointment }}</td>
</tr>
#endforeach
</table>
#foreach ($appointments as $appointment)
#endif
Try this:
You can achieve this by checking for non empty appointments. Here we would check for the negative case of empty () method.
View (blade)
#if ($view === 'label')
#foreach ($appointments as $appointment)
<table>
#foreach ($appointment->appointments as $key=> $label)
#if(!empty($label))
#($key == 0)
<tr>
<th><p>{{ $appointment->label }}</p> </th>
</tr>
#endif
<tr>
<td>{{ $label->appointment }}</td>
</tr>
#endif
#endforeach
</table>
#endforeach
#endif
Hope this is helpful.
You need to check the length of the array before you write the label. So here you'd make sure that $appointment->appointments count is greater than 0.
#if ($view === 'label')
#foreach ($appointments as $appointment)
#if (count($appointment->appointments) > 0)
<table>
<tr>
<th><p>{{ $appointment->label }}</p></th>
</tr>
#foreach ($appointment->appointments as $label)
<tr>
<td>{{ $label->appointment }}</td>
</tr>
#endforeach
</table>
#endif
#foreach ($appointments as $appointment)
#endif