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.
Related
I am looking at creating user ID automatically upon user registration. I've coded the files using Laravel and the database uses MySQL. The format should be S0001, S0002, S0003, so on and so forth.
What kind of datatype I should change it to in MySQL? Currently it is set at BIGINT.
If yes, how do I proceed with the code then?
This code below is from viewuser.blade.php:
{{View:: make('title')}}
{{View:: make('menu')}}
<div class="table-responsive">
<table class="table table-striped table-sm">
<!-- <caption>List of Users</caption> -->
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">User ID</th>
<th scope="col">Full Name</th>
<th scope="col">Email Address</th>
<th scope="col">Password</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<td>{{ $loop->iteration}}</td>
<td>{{ $loop->iteration}}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->password }}</td>
<td>{{ date('D, d F Y', strtotime($user->created_at)) }}</td>
<td>
Edit
Delete
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<div class="pagination">
{{ $users->links() }}
</div>
And this is the Register Page code, in case it's useful:
{{View:: make('title')}}
<div>
<form action="register" method="post">
#csrf
<div>
<label for="exampleInputEmail1">Full Name</label>
<input type="text" name="fullname" id="exampleInputName1">
</div>
<div>
<label for="exampleInputEmail1">Email Address</label>
<input type="email" name="email" id="exampleInputEmail1" aria-describedby="emailHelp">
<div id="emailHelp">We'll never share your email with anyone else
</div>
</div>
<div>
<label for="exampleInputPassword1">Password</label>
<input type="password" name="password" id="exampleInputPassword1">
</div>
<button type="submit">Register</button>
<button type="button" onclick="javascript:history.back()">Cancel</button>
</form>
</div>
{{View:: make('footer')}}
Why not just use the existing id column on the Users table, then define a prefix column in your Users table that you can prepend to the id whenever you want to display it.
Users table migration
$table->string('prefix')->default('S');
Users model
Then you can do something like define an accessor on your model to return the combined value of the id and prefix columns:
class User extends Model
{
protected function matriculationNumber()
{
return $this->prefix . str_pad($this->id, 3, '0', STR_PAD_LEFT);
}
}
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
Please help, I'm currently working on this livewire project. The page is paginated with checkboxes. When I check boxes on the next page, it reloads the data on the first page and selects another item of the same number in the row. So if try to select a checkbox of row 3 on the second page, data will refresh to show items of previous page, and select the checkbox of row 3
I'm unable to figure it out, someone please help.
Below is my code
{
$tracking_dates = Tracking::select(DB::raw('max(created_at) as created_at'))
->groupBy('tracking_number')
->get();
$this->trackings = Tracking::query()
->when(Auth::user()->hasRole('user'), function($query){
$query->whereHas('PurchaseRequest', function($sub_query) {
$sub_query->where('user_id', '=', Auth::id());
});
})
->whereIn('created_at', $tracking_dates)
->paginate(20);
return view('livewire.index-tracker', [
'trackings' => $this->trackings,
'trackingStatus' => TrackingStatus::all(),
]);
}
My view Code
<form>
<table id="datatable-buttons" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">
<thead>
<tr>
#role('superadministrator')
<th>Multiple Action Select</th>
#endrole
<th>Item(s) Type</th>
<th>Tracking Number</th>
<th>Tracking Status</th>
<th>Invoice</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#if(count($trackings) > 0)
#foreach($trackings as $key=>$tracking)
<tr>
#role('superadministrator')
<td>
{{$tracking->id}}
<input type="checkbox" value="{{$tracking->id}}" wire:model="selected">
</td>
#endrole
<td>
{{$tracking->purchase_request_id != "" ? "Purchase Request" : NULL}}
{{$tracking->shipment_id != "" ? "Shipment" : NULL}}
</td>
<td>{{$tracking->tracking_number}}</td>
<td>{{$tracking->TrackingStatus->name}}</td>
<td>
<i class="mdi mdi-eye font-18"></i>
</td>
<td>
<i class="mdi mdi-eye font-18"></i>
{{-- #role('superadministrator')--}}
<i class="mdi mdi-pencil font-18"></i>
{{-- #endrole--}}
</td>
</tr>
#endforeach
#else
<tr>
<td colspan="5">No data available</td>
</tr>
#endif
</tbody>
</table>
#role('superadministrator')
<div class="form-group row">
<button wire:click.prevent="store()" class="btn btn-primary btn-md">Track</button>
</div>
</form>
#if(count($trackings) > 0)
{{ $trackings->links() }}
#endif```
You can call the javascript page.dt() event and store all ids you have checked into local storage.
Please refer below code for that.
var checkedData = [];
$('#tableID').on( 'page.dt', function () {
let allIds = localStorage.getItem("row-data-ids").split(",");
$('.checkboxes:checkbox:checked').each(function(index, rowId){
if($.inArray( $(this).val(), allIds) == -1){
checkedData.push($(this).val());
}
});
localStorage.setItem('row-data-ids', checkedData);
} );
You can also store local storage values to hidden variable to get into POST Request.
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);
}
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..