In my controller I've this method:
public function code($code)
{
$user = Auth::user();
$instituteCodes = DB::table('institute_codes')->where(['code' => $code, 'institute_id' => Auth::id()]);
if($instituteCodes->exists()){
$claim = DB::table('code_claims')->where('institute_code_id', $instituteCodes->value('id'))->get();
$allusers = collect($claim);
$allusers->values()->all();
$course = Course::findOrFail($instituteCodes->value('course_id'));
return view('institute.code', compact(['allusers', 'course']));
}
}
institute_codes table
id
code
course_id
1
ABC
1
2
ZXC
5
course table
id
name
1
Python
2
Laravel
I've a route which passes $code parameter to public function code() I'm getting same course data on using different codes in blade view but when I use return $course or dd($course) in controller it returns the the expected result i.e. different courses for different codes. But in blade I'm getting same courses. Any help will be appreciated.
This is happening!
Why both codes are returning same course in blade and different courses(this is what I want) on using dd or return.
Edit 1
Blade View
<div class="table-wrapper">
<table class="fl-table">
<thead>
<tr>
<th class="long-table-row">Email</th>
<th>Code</th>
<th>Course</th>
<th>Language</th>
<th>Enrolled On</th>
<th>Progress</th>
<th>Certificate</th>
<th>Action</th>
</tr>
</thead>
#foreach ($allusers as $item)
<tr>
<td class="long-table-row">
{{ $item->user_email }}
</td>
<td class="uppercase">{{ $code }}</td>
<td>{{ $course->c_name }}</td>
<td>{{ $course->language->name }}</td>
<td>29/07/2022</td>
<td>100%</td>
<td><i class="bi bi-check-circle-fill" style="font-size: 1.2vw; color: rgb(0, 200, 90);"></i></td>
<td></td>
</tr>
#endforeach
<tbody>
</table>
</div>
I'm getting same c_name and language name for different codes.
I think you are iterating through the allusers but for course you are only picking the first course in the foreach loop of allusers.
Related
I am new to using Laravel and I am currently trying to pass a variable from the index blade file to the controller in order to bring up all apparatus type fields associated with a specific apparatus code id.
I am able to access the apparatus type fields associated with the apparatus code id when I include a specific number which correlates to that apparatus code id (eg. inputting 2 brings up the apparatus type details for the apparatus code id 2, inputting 3 brings up the apparatus type details for the apparatus code id 3 etc).
However when I have tried to pass a variable relating to each individual apparatus code id using a for loop in the controller, the apparatus type loop appears to not be accessed and does not return any information. I will include snippets of my current code for clarity.
Any help or guidance with this issue would be greatly appreciated!
Controller file
public function index()
{
$apparatusCodes = ApparatusCodes::get();
foreach ($apparatusCodes as $apparatusCode){
$apparatusTypes = ApparatusTypes::where('apparatus_code_id', $apparatusCode->id)->get();
}
return view('apparatus_codes.index', compact('apparatusCodes', 'apparatusTypes'));
}
Index.blade file
<table id="datatable" class="stripe hover dt-responsive display nowrap" style="width:100%; padding-top: 1em; padding-bottom: 1em;">
<thead>
<tr>
<th>ID</th>
<th >Apparatus Code</th>
<th>Description</th>
<th>Rent</th>
<th></th>
</tr>
</thead>
<!--start of for loop-->
#foreach ($apparatusCodes as $apparatusCode)
<tbody>
<tr data-toggle="collapse" id="table{{ $apparatusCode->id}}" data-target=".table{{ $apparatusCode->id}}">
<td> {{ $apparatusCode->id}} </td>
<td>{{ $apparatusCode->apparatus_code}} </td>
<td> {{ $apparatusCode->description}}</td>
<td> {{ $apparatusCode->rent}}</td>
<td #click="isOpen = !isOpen" class="main-bg"><img class="mb-1 duration-300 h-6 w-6" :class="{'transform rotate-180' : isOpen}"
src="../img/Icon-arrow-dropdown-white.png" alt="arrow down">
</td>
<td><img class="mb-1 duration-300 h-6 w-6" :class="{'transform rotate-180' : isOpen}"
src="../img/edit-icon.svg" alt="Edit Icon">
</td>
</tr>
<tr x-show.transition.duration.300ms.origin.bottom="isOpen" x-cloak #click.away="isOpen = false" class="collapse table{{ $apparatusCode->id}}">
<td colspan="999">
<div>
<table id="datatable" class="table table-striped">
<thead>
<tr>
<th>Apparatus Code </th>
<th>Apparatus Type</th>
<th>Compensation </th>
<th>Created On </th>
<th>Created By </th>
<th></th>
#foreach ($apparatusTypes as $apparatusType)
</thead>
<tbody>
<tr>
<td>{{ $apparatusCode->apparatus_code}}</td>
<td>{{ $apparatusType->apparatus_type }}</td>
<td>{{ $apparatusType->compensation }}</td>
<td>{{ $apparatusType->created_at }}</td>
<td>{{ $apparatusType->users->name}}</td>
<td> <button type="button" class="btn btn-default btn-edit js-edit"><img class="mb-1 duration-300 ml-4 inset-0 h-6 w-6" src="/../../img/Icon-edit-gray.png" alt="edit"></button></td>
</tr>
#endforeach
</table>
</tr>
</tr>
</td>
</tbody>
#endforeach
</table>
If you have the relationships setup you could eager load the ApparatusTypes for each ApparatusCode:
$apparatusCodes = ApparatusCodes::with('appartusTypes')->get();
Then in the view you could iterate the appartusTypes of each ApparatusCode:
#foreach ($apparatusCodes as $apparatusCode)
...
#foreach ($apparatusCode->apparatusTypes as $type)
...
#endforeach
...
#endforeach
Without using the relationships you could get all the ApparatusTypes for the ApparatusCodes and then group them by apparatus_code_id:
$codes = ApparatusCodes::get();
$types = ApparatusTypes::whereIn('apparatus_code_id', $codes->pluck('id'))
->get()
->groupBy('apparatus_code_id');
Then in the view you could iterate the group that corresponds to the current Code's apparatus_code_id:
#foreach ($codes as $code)
...
#foreach ($types->get($code->id, []) as $type)
...
#endforeach
...
#endforeach
Laravel 8.x Docs - Eloquent - Relationships - Eeager Loading with
Laravel 8.x Docs - Collections - Available Methods - pluck
Laravel 8.x Docs - Collections - Available Methods - groupBy
I have added pagination as follows to my laravel project. But all the pages display the same content.
Think that there are 8 records in the employee table. When I added the below code, on the first page it displays all 8 records, and on the second page also it displays the same 8 records.
It doesn't break the first 5 records to the first page and last 3 records to the second page.
controller
public function all()
{
$data = DB::table('employees')->paginate(5);
$employeeshow = employee::all()->toArray();
return view('employee.index', compact('employeeshow', 'data'));
}
index.blade.php
<div>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
#foreach($employeeshow as $row)
<tr>
<td>{{$row['firstname']}}</td>
<td>{{$row['lastname']}}</td>
</tr>
#endforeach
</table>
{{ $data->links() }}
</div>
I want to display the first 5 records on the first page, next 5 records on the second page likewise. Help me. Thank you
Your code does not make much sense, you are fetching all employees from the database with $employeeshow and looping through them, but then you are getting a different set of employees from the database with $data and paginate them, so it does not matter which page you open, you are always looping through $employeeshow which will always contain the same employees.
I would do the following instead:
Controller:
public function all()
{
$employees = employee::paginate(5); // why is your model lowercase?
return view('employee.index', compact('employees'));
}
View:
<div>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
#foreach($employees as $employee)
<tr>
<td>{{ $employee->firstname }}</td>
<td>{{ $employee->lastname }}</td>
</tr>
#endforeach
</table>
{{ $employees->links() }}
</div>
You can read more about pagination in the docs.
Instead of using $employeeshow please try $data in your foreach loop.
Your paginated value stored in $data not in the $employeeshow.
I'm trying to get the count of different models and list them by company.
I have a collection of different models.
public function listAllRequets()
{
$requests = collect();
$terms = Termination::with('user.company')->where('default_status', 2)->get();
$deacs = LoaDeactivation::with('user.company')->where('manager_status', 2)->get();
$reacs = LoaReactivation::with('user.company')->where('manager_status', 2)->get();
// Use push() to collect them all in one collection
foreach ($terms as $term)
$requests->push($term);
foreach ($deacs as $deac)
$requests->push($deac);
foreach ($reacs as $reac)
$requests->push($reac);
return $requests;
}
In my index function I'm adding them to $requests.
To get the list of companies I'm grouping them using the groupBy() method.
public function index()
{
$requests = $this->listAllRequets();
$requestCompanies = $requests->groupBy(function($requests) {
return $requests->user->company->company_acronym;
});
In my view I have this:
<table class="table table-sm">
<thead class="thead-light">
<tr>
<th>Company</th>
<th class="text-center">Terms</th>
<th class="text-center">Deacs</th>
<th class="text-center">Reacs</th>
</tr>
</thead>
<tbody>
#foreach ($requestCompanies as $company => $requests)
<tr>
<td class="pt-3"><strong>{{ $company }}</strong></td>
#foreach ($requests as $request)
<td class="text-center">{{ $request-> }}</td>
#endforeach
</tr>
#endforeach
</tbody>
</table>
I need a way to get the count of each model by company.
My companies list out as expected but I'm not sure how to get the correct model count.
If you have relationships among your company and each request model (Termination, LoaDeactivation, LoaReactivation), wouldn't be easier to do that with a query at database level?
That seems a nice use case for with count method that eloquent provides:
Assuming the relations in the company model are called terminations, deactivations and reactivations, you could do:
public function index()
{
$companies = Company::withCount('terminations','deactivations','reactivations')->get();
// here pass $companies array to your view
}
Then in your view you can access the count for each type on each company:
<table class="table table-sm">
<thead class="thead-light">
<tr>
<th>Company</th>
<th class="text-center">Terms</th>
<th class="text-center">Deacs</th>
<th class="text-center">Reacs</th>
</tr>
</thead>
<tbody>
#foreach ($companies as $company)
<tr>
<td class="pt-3"><strong>{{ $company->name }}</strong></td>
<td class="text-center">{{ $company->terminations_count }}</td>
<td class="text-center">{{ $company->deactivations_count }}</td>
<td class="text-center">{{ $company->reactivations_count }}</td>
</tr>
#endforeach
</tbody>
</table>
I am developing a project in laravel 5.3. where I am using a users table and a points table in database. users are associated with transactions of points. these transections are recorded in points table. my points table looks like in my previous question who's link is this
-- I want to get user id in laravel 5.3 controller with Auth::user()->id but it creates error their --
. Now I am fetching users' all data in a page whare I want to show the net balance of there points too. you can see the result in image.image is here
so currently I am using the following code in controller.
public function users_list()
{
$ptitle = "Website Users";
$pdesc = "";
//$total_users = User::count();
$users = User::select('*')->orderby('created_at', 'desc')->get();
return view('admin.all-users-list',
['ptitle' => $ptitle,
'pdesc' => $pdesc,
'users' => $users,
'total_users' => $this->total_users,
]);
}
And getting result in view like following
<table class="table table-hover">
<tbody><tr>
<th>ID</th>
<th>User Name</th>
<th>Email</th>
<th>Country</th>
<th>Date</th>
<th>Points</th>
</tr>
<?php foreach ( $users as $u ){ ?>
<tr>
<td>{{ $u->id }}</td>
<td>{{ $u->user_name }}</td>
<td>{{ $u->email }}</td>
<td>{{ $u->country }}</td>
<td>{{ $u->created_at }}</td>
<td></td>
</tr>
<?php }?>
</tbody></table>
But I dont know how can i create a join in query builder to fetch net points for every user in list
I am trying access my model in my blade template. So I can loop thorough data.
I have tried calling global function like
{{ Auth::user }}
and it works fine. I can output user data on my view.
I have created a model called students which holds students data with user_id which is coming from user table. Like 1->N relationship. A user has multiple students associated with it. How can I call custom model in my view.
Pass student data to view
$students = Student::all();
return view('student_view')
->with('student_data', $students);
View like this in table
<table id="table-student" class="table table-bordered table-striped">
<thead>
<tr>
<th width="5%" class="text-center">No.</th>
<th>Name</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
#foreach($student_data as $student)
<tr>
<td width="5%"><?php echo $i; ?></td>
<td>{{ $student->name }}</td>
<td>{{ $student->description }}</td>
</tr>
<?php $i++; ?>
#endforeach
</tbody>
</table>
You can create a variable and pass it to the view:
$user = User::where('id', 1)->with('students')->first();
return view('some.view', compact('user'));
Then you can itearte over students in the view:
#foreach ($user->students as $student)
{{ $student->name }}
#endforeach
In your view your can directly call the relation method as:
#foreach(auth()->user()->students as $student)
{{ $student->name }}
#endforeach
Assuming your relation method name is students.