Laravel 5.5 and Sentinel Missing required parameters - php

Everytime I try to delete a user, I get "Missing required parameters for [Route: delUser] [URI: deleteUser/{id}]". But when I refresh the page, the selected user will be deleted. I don't know why I am getting this error.
Here is my web.php:
Route::get('/deleteUser/{id}',[
'uses' => 'ViewUsersController#deleteUser',
'as' => 'delUser'
]);
ViewUsersController.php:
public function deleteUser($id){
$user = Sentinel::findById($id);
$session1 = Session::flash('del_message', 'User has been successfully deleted.');
$session2 = Session::flash('alert-class', 'alert-success');
if ($user != null) {
$user->delete();
return redirect()->route('delUser')->withSessionone($session1)->withSessiontwo($session2);
}
return redirect()->route('delUser')->withMessage("Wrong ID");
}
viewUsers.blade.php:
<h1> Users </h1>
<table border = '1'>
<tr>
<th> First Name </th>
<th> Last Name </th>
<th> Email </th>
<th> Date Registered </th>
<th> Delete </th>
</tr>
#foreach($users as $key => $data)
<tr>
<td> {{ $data->first_name }} </td>
<td> {{ $data->last_name }} </td>
<td> {{ $data->email }} </td>
<td> {{ $data->created_at }} </td>
<td>Delete</td>
</tr>
#endforeach
</table>
I am using laravel 5.5 and sentinel

When you are redirecting, it is expecting a route delUser with an id like delUser/1 but you are redirecting without id part.
return redirect()->route('delUser')->withMessage("Wrong ID");
Make sure you have a url like -
Route::get('delUser','SomeConroller#delUserMethod');
Or add an id with the URL.

Related

Not getting all entries of an attribute in Laravel

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.

Passing variable from index to controller using Laravel

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

Property [id] does not exist on this collection instance laravel

I keep getting this error after fixing a new code that I had just put in and I have no idea why it is giving me this error "Property [id] does not exist on this collection instance" which point to the id of my route in home.blade.php. Can someone help me take a look thanks a lot
The part that I added in was the hire_status part and after fixing it, it then gave me this error.
home.blade.php
<table class="table table-bordered">
<tr>
<th><strong><big>Name: </big></strong></th>
<th><strong><big>Hire Status: </big></strong></th>
<th><strong><big>Action: </big></strong></th>
</tr>
<td>
<tr>
#foreach($data as $value)
<tr>
<th>{{$value->Name}}</th>
<th>
#foreach($data1 as $value1)
{{$value1->hire_status}}
</th>
<th><form action="{{ url('/home/'.$value->id.'/delete') }}" method="get">
{{ csrf_field() }}
<button type="submit">Delete</button>
</form></th>
</tr>
#endforeach
#endforeach
</tr>
</tr>
</table>
HomeController:
public function getData(){
$data['data'] = DB::table('personal_infos')->where('deleted_at',NULL)->get()->sortByDesc('created_at');
$data1 = DB::table('hires')->where('hire_status','Yes')->get();
if(count($data)>0){
return view('home',compact('data','data1'));
}else{
return view('home');
}
}
Just remove key from array,
you are trying to get data in your view directly so get it directly not to data of data...
$data['data'] = DB::table('personal_infos')->where('deleted_at',NULL)->get()‌​->sortByDesc('create‌​d_at');
change it to,
$data = DB::table('personal_infos')->where('deleted_at',NULL)->get()‌​->sortByDesc('create‌​d_at');

How to show 2 tables data in 1 view using laravel?

There is two tables (one is Users and second is Branches). I want to show the Branch with its Branch Admin Name.And the branch Admin info is save in users table. There is an error When i am trying to show the data of Two table in one view. Tell me How i manage this issue.
View:
<div class="branches col-xs-12 col-sm-12 col-md-9 col-lg-9">
<input type="text" class="pull-right form-control search" placeholder="Search">
<div class="spaces"></div>
<table class="table table-bordered">
<thead>
<tr>
<th>
BranchName
</th>
<th>
BranchAdmin
</th>
<th>
Email
</th>
<th>
Contact
</th>
<th>
Location
</th>
<th>
Action
</th>
</tr>
</thead>
<tbody>
#foreach($branch as $brnch)
<tr class="branchies-row">
<td>
{{$brnch->branch_name}}
</td>
#foreach($user as $users)
<td>
{{$users->name}}
</td>
#endforeach
<td>
{{$brnch->email}}
</td>
<td>
{{$brnch->contact}}
</td>
<td>
{{$brnch->address}}
</td>
<td>
<a data-id="{{$brnch->id}}" class="delete-branch">Delete</a> /
Edit
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
Controller:
public function getBranchinfo(){
$user = User::where('type', '=', 'BranchAdmin');
$branch = Branch::all();
return view('Branch.branchinfo')->with('branch',$branch)->with('user', $user);
}
Model:
public function branch(){
return $this->hasOne('App\Branch', 'user_id', 'id');
}
We can use query builder - Join - for that :
$branches = DB::table('Branches')
->join('users', 'users.id', '=', 'Branches.user_id')
->where('users.type', '=', 'BranchAdmin')
->get();
and this should give you the data you need.
You have to make the relationship in branch model:
public function user(){
return $this->belongsTo('App\User');
}
Then in blade file you get branch admin name as:
#foreach($branch as $brnch)
<tr class="branchies-row">
<td>{{ $brnch->user->branchadmincloumn }}</td>
</tr>
#endforeach
branchadmincloumn is the cloumn name from your users table where your branch admin name is saving. So change branchadmincloumn to your desired column name.
Hope it helps..

Laravel 5.2 How can I display member list with his / her groups in the same row (One to Many ( or Many to Many ) data display)

I am new to Laravel and learning Laravel 5.2 with MariaDB 10.0.xx.
There are several tables but in brief 'Member' can have many 'Group' and I would like to display Member's First Name and Last Name with the name of groups that member already joined in the same row.
I don't have any clear idea to achieve this but what I am trying to do is to create two separate queries ($memberLists and $groupLists) and get the group list by inputting first query ($memberLists)'s primary key to second query ($groupLists).
or if you have better idea please let me know.
I want display as below
first Name / Last Name / Groups
john Doe A group, B group
Jane Taylor B group
Nick Kay A group, B group, C group
My controller
$memberLists = DB::table('Member')
->select('firstNm','lastNm')
->join('Center', 'Member.mbrCd', '=', 'Center.mbrCd')
->join('Org', 'Center.orgCd', '=', 'Org.orgCd');
$groupLists = DB::table('Group')
->select('groupNm')
->join('Center','Center.orgCd','=','Group.orgCd')
->join('GroupMember ','GroupMember.mbrCd','=','Center.mbrCd')
->where ('GroupMember.mbrCd',$Member_primaryKey_from_memberLists);
view return view('member.memberList')
->with('memberLists', $memberLists)
->with('groupLists',$groupLists);
This is my view
#extends('masterLayOut')
<table>
<thead>
<th> First Name </th>
<th> Last Name </th>
<th> Groups </th>
</thead>
<tbody>
#foreach ($memberLists as $memberList)
<tr>
<td> {{ memberList->firstNm}}</td>
<td> {{ memberList->lastNm }}</td>
<td> {{ groupLists->groupNm }} </td>
</tr>
#endforeach
</tbody>
<table>
Well this is grate example why is good to use the Eloquent models instead of queries. You could resolved this in tho lines with eager loading.
In your example you'll have group everything first of all (I'll omit all the joins besause I don't know if we need it here):
Controller
//I'm selecting also member id (I don't know how you call it)
$memberLists = DB::table('Member')
->select('id', 'firstNm','lastNm')
->get();
$groupLists = DB::table('Group')
->select('groupNm', 'mbrCd')
->get();
$groups = [];
foreach($groupLists as $group) {
if (!isset($group[$group->mbrCd])) {
$group[$group->mbrCd] = [];
}
$group[$group->mbrCd][] = $group->groupNm;
}
return view('member.memberList')
->with('memberLists', $memberLists)
->with('groupLists', $groups);
Then in your view:
#extends('masterLayOut')
<table>
<thead>
<th> First Name </th>
<th> Last Name </th>
<th> Groups </th>
</thead>
<tbody>
#foreach ($memberLists as $memberList)
<tr>
<td> {{ memberList->firstNm}}</td>
<td> {{ memberList->lastNm }}</td>
<td> {{ isset($groups[$memberList->id]) ? implode(', ', $groups[$memberList->id]) : '' }} </td>
</tr>
#foreach
</tbody>
<table>

Categories