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..
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 want to implement simple teaming option to my app. I have theese tables:
users/teams /team_members/
-----/---------/------------/
id /id /id /
name /user_id /user_id /
... /... /team_id /
I have 3 models where I have defined relations:
User model:
public function teams() {
return $this->hasOne(Team::class);
}
public function teamMembers() {
return $this->hasManyThrough(Team::class, TeamMembers::class);
}
Team model:
public function user() {
return $this->belongsTo(User::class);
}
public function members() {
return $this->hasMany(TeamMember::class);
}
TeamMember model:
public function user() {
return $this->belongsTo(User::class);
}
User can only create one team, but join many teams. The problem I have is with displaying data in the frontend.
This is the query I make in the controller:
public function index(User $user) {
$teams = Team::where('user_id', auth()->user()->id )->with('members')->get();
return view('teams.index')->with('teams', $teams);
}
I have a table with the members of the current autheticated user team. I want to display the names if each member but the only thing i can do is $team->member->id and I get the ids of the users but I want to get their names too. I can do it with simple query but I dont want to make new query for every row.
This is the blade file:
#if ( $teams->count() > 0 )
#foreach ($teams as $team)
<table class="table table-striped table-sm table-responsive-lg">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Members</th>
<th scope="col">Created</th>
<th scope="col" colspan="2"></th>
</tr>
</thead>
<tbody>
<tr>
<td>{{$team->name}}</td>
<td>{{ $team->members->count('members')}} {{ Str::plural('member', $team->id) }}</td>
<td>{{ $team->created_at->diffForHumans() }}</td>
<td>
Edit
</td>
<td>
<form action="" method="POST">
#method('DELETE')
#csrf
<button class="btn btn-sm btn-danger" type="submit">Delete</button>
</form>
</td>
</tr>
</tbody>
</table>
<br>
<h5>Team members:</h5>
<table class="table table-striped table-sm">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Added</th>
</tr>
</thead>
<tbody>
#foreach ($team->members as $member)
<tr>
<td>{{ $member->user->name }}</td>
<td>{{ $member->created_at->diffForHumans() }}</td>
</tr>
#endforeach
</tbody>
</table>
#endforeach
#else
<div class="container">
<h2>You do not own a team</h2>
<p class="lead text-muted">You can create your own team or join the team of other user.</p>
<p>
Create team
Join team
</p>
</div>
#endif
When I changed $team->member->id with $member->user->name it worked. It shows the names of the users but I get N+1 queries alert and I don't know how to fix it (i tried different queries and relations but it didnt work):
You are accessing the user relationship from each member. So lets eager load that relationship as well.
public function index(User $user)
{
$teams = Team::where('user_id', auth()->user()->id )->with('members.user')->get();
return view('teams.index')->with('teams', $teams);
}
im trying to search for name or id of students in the table but i do not know how to do that with router and controller. I already have the table and search form with button i need the list to show all data initially then display only the entered data when a name is searched
here is my search form
<!-- Search form -->
<form class="d-flex align-items-center flex-nowrap" action="/search">
<input name="q" class="form-control" type="text" placeholder="Search" aria-label="Search">
<button class="btn btn-sm btn-info">Search</button>
</form>
and this is the table
<table class="table">
<thead class="thead-light">
<tr>
<th scope="col">id</th>
<th scope="col">First name</th>
</tr>
</thead>
<tbody>
#foreach($students as $student)
<tr>
<td>{{ $student->cne }}</td>
<td>{{ $student->firstName }}</td>
<td>
Edit
</td>
</tr>
#endforeach
</tbody>
</table>
I would love the realtime search thingy so I can get rid of the search button lol
thank you
This is how I would do it (without the realtime thingy, check my comment for that)
In your controller that returns your students, you can grab the request and if it has a q parameter then you can filter your query.
public function index(Request $request)
{
$students = Student::query();
if($request->has("q") {
$students->where("cne", $request->get("q"))
->orWhere("firstName", $request->get("q")
}
return view("students.index", [
"students" => $students->get();
]);
}
You can use a LIKE operator if you'd prefer.
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.
I am join two tables (one is User and the second table is branch) and print the BranchAdmin name with its branch in the single view, the branchAdmin information is save in the User table, but when i perform the edit function on the branch data, it will show an empty page and only show the edit form when users and branch id is match. So how i manage that when i edit the branch it only concern with the branch id not with the company id that is foreign key in the branch?
Branch Controller:
public function getBranchinfo(){
$branch = DB::table('branch')
->join('users', 'users.id', '=', 'branch.user_id')
->where('users.type', '=', 'BranchAdmin')
->get();
return view('Branch.branchinfo')->with('branch',$branch);
}
BranchDetails 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>
id
</th>
<th>
Branch-Name
</th>
<th>
AdminName
</th>
<th>
Email
</th>
<th>
Contact
</th>
<th>
Location
</th>
<th>
OpenHours
</th>
<th>
Action
</th>
</tr>
</thead>
<tbody>
#foreach($branch as $brnch)
<tr class="branchies-row">
<td>
{{$brnch->id}}
</td>
<td>
{{$brnch->branch_name}}
</td>
<td>
Note->(In this i will take the name of user who is Admin of branch.) like this ($brnch->user_name)
{{$brnch->user_id}}
</td>
<td>
{{$brnch->email}}
</td>
<td>
{{$brnch->contact}}
</td>
<td>
{{$brnch->address}}
</td>
<td>
{{$brnch->open_hours}}
</td>
<td>
<a data-id="{{$brnch->id}}" class="delete-branch">Delete</a> /
Edit
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
Branch Migration:
public function up()
{
Schema::create('branch', function (Blueprint $table) {
$table->increments('id');
$table->integer('company_id')->unsigned();
// here you need to define on deleteCascade, when this records delete, this will also
// delete it's records from other tables that related to this one via a foriegn key.
$table->foreign('company_id')->references('id')->on('company')->onDelete('cascade');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->String('branch_name');
$table->String('email');
$table->String('address');
$table->String('contact');
$table->String('open_hours');
$table->timestamps();
});
}
User Model:
public function branch(){
return $this->hasOne('App\Branch', 'user_id', 'id');
}
i think that the problem is that the two table have a field called id so if you wanna remove the conflict you can just inverse the order of your join so the last id you get from the query is the branch id
$branch = DB::table('user')
->join('branch', 'branch.user_id', '=', 'users.id')
->where('users.type', '=', 'BranchAdmin')
->get();
you can add aliases like this
$branch = DB::table('user')
->select(DB::raw('brunch.*, brunch.id as branch_id, user.*'))
->join('branch', 'branch.user_id', '=', 'users.id')
->where('users.type', '=', 'BranchAdmin')
->get();