How to add custom actions against any button in Laravel? - php

I m making a web app in which there are two tables: classes & sections. My migrations are shown below.
Classes
Schema::create('classes', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->timestamps();
});
Sections
Schema::create('sections', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->integer('class_id')->unsigned();
$table->foreign('class_id')->references('id')->on('classes');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
In my Blade file, I want to show all the buttons of classes. When I click on that button it should show all the sections where class_id in section table is of the selected button.
I'm stuck because of the syntax. I'm new to Laravel and I'm unaware of how to add a response against the button.
My controller for section
public function index()
{
$sections = Section::all();
$class = Class::all();
$users = User::all();
return view('sections.index')->with('sections', $sections)->with('class', $class)->with('users', $users);
}
Blade
#foreach($classs as $cat)
<button class="btn btn-info">{{$cat->title}}</button>
//this shows all buttons with class name
#endforeach
<div class="box-body">
<table class="table table-responsive">
<thead>
<tr>
<th>Section Name</th>
<th>Class</th>
<th>Teacher</th>
<th>Modify</th>
</tr>
</thead>
How can I get all sections of only selected class under these tables?
This is code as suggested in which I have made changes, I m not getting data when click on button. Kinndly tell me what should I do ? Do I have to change controller
#foreach($classs as $cat)
<button class="btn btn-info class_btn" id="{{$cat->class_id}}">{{$cat->title}}</button>
<div class="box-body" style="display:none" id="table_{{$cat->class_id}}">
<table class="table table-responsive">
<thead>
<tr>
<th>Section Name</th>
<th>Class</th>
<th>Teacher</th>
<th>Modify</th>
</tr>
</thead>
<tbody>
#foreach($sections as $sec)
<tr>
<td>{{$sec->title}}</td>
<td>
{{ \Illuminate\Support\Facades\DB::table('classses')->where('id',$sec->class_id)->value('title')}}
</td>
<td>
{{ \Illuminate\Support\Facades\DB::table('users')->where('id',$sec->user_id)->value('name')}}
</td>
<td>
<button class="btn btn-info" data-mytitle="{{$sec->title}}" data-myclassid="{{$sec->class_id}}" data-myuserid="{{$sec->user_id}}" data-secid={{$sec->id}} data-toggle="modal" data-target="#edit">Edit</button>
/
<button class="btn btn-danger" data-secid={{$sec->id}} data-toggle="modal" data-target="#delete">Delete</button>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endforeach
<script>
//onclick show particular table
$('.class_btn').on('click',function(){
$('.box-body').hide();
$('#table_'+$(this).attr('class_id')).show();
});
</script>

You Use Jquery And hide() ,show() function show particular table to particular class Like As
#foreach($class as $cat)
<button class="btn btn-info class_btn" id="{{Your_class_id}}">{{$cat->title}}</button>
<div class="box-body" style="display:none" id="table_{{Your_class_id}}">
<table class="table table-responsive">
<thead>
<tr>
<th>Section Name</th>
<th>Class</th>
<th>Teacher</th>
<th>Modify</th>
</tr>
</thead>
<tbody>
//print your data
</tbody>
</table>
</div>
#endforeach
<script>
//onclick show particular table
$('.class_btn').on('click',function(){
$('.box-body').hide();
$('#table_'+$(this).attr('id')).show();
});
<script>
Another Way You Use Ajax....

Related

Livewire checkbox selecting items on previous page for a paginated page

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.

hidden division is not appearing after operating click event for dynamic table

I am fetching data to a table from mysql database using php and jquery. But I want to keep the "details" column hidden, which I want should be slide down(appear ) in the corresponding table row after clicking a button named "show details". The following Image may clear my thought:
The jquery Code I am using for slide down is :
$(document).ready(function(){
$("#get_details").click(function(){
$("#get_details_button").slideToggle("slow");
});
});
The code I am using to fetch data is :
$sub_array = array();
$sub_array[] = $row["id"];
$sub_array[] = $row["product_name"].'<p id="get_details">'.$row["details"].'</p>';
$sub_array[] = '<button type="button" name="get_details_button" id="get_details_button" class="btn btn-success btn-xs get_details_button">Get Details</button>';
$data[] = $sub_array;
}
Please feel free to ask for any additional code if you need.
the html part is here :
<table id="user_data" class="table table-bordered table-striped">
<thead>
<tr>
<th width="30%">Image</th>
<th width="50%">Product Name</th>
<th width="20%">get Details</th>
</tr>
</thead>
</table>
You need to use class for p tag then whenever button is clicked use $(this).closest('tr').find(".get_details") to get p tag and show/hide same
Demo Code :
$(document).ready(function() {
//onclick of button
$(".get_details_button").click(function() {
//get p tag and show/hide it
$(this).closest('tr').find(".get_details").slideToggle("slow");
});
});
.get_details {
display: none
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="user_data" class="table table-bordered table-striped">
<thead>
<tr>
<th width="30%">Image</th>
<th width="50%">Product Name</th>
<th width="20%">get Details</th>
</tr>
</thead>
<tr>
<td>1
</td>
<td>Soemthig,,
<!--use class-->
<p class="get_details">Soemthing.....</p>
</td>
<td><button type="button" name="get_details_button" class="btn btn-success btn-xs get_details_button">Get Details</button>
</td>
</tr>
<tr>
<td>2
</td>
<td>Soemthig,,2
<p class="get_details">Soemthing2.....</p>
</td>
<td><button type="button" name="get_details_button" class="btn btn-success btn-xs get_details_button">Get Details</button>
</td>
</tr>
</table>
You don't need any ids to make this work. I don't know your markup but here is an example. You will need to work it out for your markup.
<div class="container">
<table>
<tr>
<td>{{ id }}</td>
<td>{{ product_name }}</td>
<td><button>details</button></td>
</tr>
</table>
<div class="product-details" style="display:none">...</div>
</div>
<div class="container">
<table>
<tr>
<td>{{ id }}</td>
<td>{{ product_name }}</td>
<td><button>details</button></td>
</tr>
</table>
<div class="product-details" style="display:none">...</div>
</div>
<div class="container">
<table>
<tr>
<td>{{ id }}</td>
<td>{{ product_name }}</td>
<td><button>details</button></td>
</tr>
</table>
<div class="product-details" style="display:none">...</div>
</div>
jquery
$('.container').find('button').on('click', function(e) {
e.preventDefault();
$(this).parent().parent().parent().parent().find('.product-details').slideToggle();
});

Laravel I want to display only employees in table

Hi I'm building a human resources management system, I want to display only users that are not admin in a data table but I'm getting error dont know what I might be doing wrong. Here is my code;
<table class="table table-hover">
<thead>
<th>Username</th>
<th>Department</th>
<th>Salary</th>
</thead>
<tbody>
#foreach($this->$users()->get() as $user)
#if($user->is_admin !== 1)
<tr>
<td>{{$user->username}} </td>
<td>{{$user->department}} </td>
<td>{{$user->salary}} </td>
</tr>
#endif
#endforeach
</tbody>
Table
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->boolean('is_admin')->nullable();
$table->string('department')->nullable();
$table->integer('salary')->nullable();
$table->string('image',255)->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
you can try #continue to skip loop
<table class="table table-hover">
<thead>
<th>Username</th>
<th>Department</th>
<th>Salary</th>
</thead>
<tbody>
#foreach($this->$users()->get() as $user)
#php
if($user->is_admin == 1){
#continue
}
#endphp
<tr>
<td>{{$user->username}} </td>
<td>{{$user->department}} </td>
<td>{{$user->salary}} </td>
</tr>
#endforeach
</tbody>
Instead of looping through the users table and checking every user to see if they are an admin user, you can extend your query by only selecting users that aren't admin users in the first place.
where('is_admin', '!=' 1)
This way you'll already have all the users that you want to show and you won't have to add extra logic to the view.
I had forgotten to write code in the controller
public function addEmployee()
{
$users = User::all();
return view('admin/addEmployee')->with('users',$users);
}

Passing data to controller on row bases

I have a table display and each row contains a button. With this button l have to pass the id to the controller where using this Admin can view user details one by one.
<table id="example2" class="table table-bordered table-hover" style="text-align: center">
<thead>
<tr>
<th>Applicant ID</th>
<th>Full name</th>
<th>Position applied</th>
<th>Date & time applied</th>
<th>View CV</th>
</tr>
</thead>
<tbody>
#foreach ($jobapplied as $row)
<tr>
<td>{{$row->app_id}}</td>
<td>{{$row->fullname}}</td>
<td>{{$row->position}}</td>
<td>{{$row->created_at}}</td>
<td>
<button type="submit" class="btn btn-
default" name="viewcv"> View</button> <br>
<br>
</td>
</tr>
#endforeach
</tbody>
</table>
Please check the code , hope it helps:
<table id="example2" class="table table-bordered table-hover" style="text-align: center">
<thead>
<tr>
<th>Applicant ID</th>
<th>Full name</th>
<th>Position applied</th>
<th>Date & time applied</th>
<th>View CV</th>
</tr>
</thead>
<tbody>
#foreach ($jobapplied as $row)
<tr>
<td>{{$row->app_id}}</td>
<td>{{$row->fullname}}</td>
<td>{{$row->position}}</td>
<td>{{$row->created_at}}</td>
<td>CV</td>
</tr>
#endforeach
</tbody>
</table>
Assuming you want to pass the field "id" or change it according to your requirement.
If you want to link to a show view, try something like this:
#foreach ($jobapplied as $row)
<tr>
<td>{{$row->app_id}}</td>
<td>{{$row->fullname}}</td>
<td>{{$row->position}}</td>
<td>{{$row->created_at}}</td>
<td>
<a href="{{action('JobController#show', $row->id)}}" class="btn btn-default">View</button>
</td>
</tr>
#endforeach
This will create a link to a view, where you can show the data.
In your controller JobController, you must create a function that receives the data:
public function show(Request $request, $id) {
...
}
Just pass the id of the record to controller function and get the details from database.
<td>
View <br>
</td>

Make survey at laravel 5.4 and MySQL

I need to make a survey for my website. I have this:
Route:
Route::resource('survey','surveyController');
Controller:
public function index()
{
$show=survey_title::find(1);
return view('survey',compact('show'));
}
Here we just read first survey.
Migrations:
Schema::create('survey_titles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->timestamps();
});
Schema::create('survey_questions', function (Blueprint $table) {
$table->increments('id');
$table->integer('survey_title_id')->unsigned()->index();
//$table->integer('user_id')->unsigned()->index();
$table->foreign('survey_title_id')->references('id')->on('survey_titles')->onDelete('cascade')->onUpdate('cascade');
// $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->string('answer');
$table->integer('voted')->nullable();
$table->timestamps();
});
Models:
survey_question:
public function survey_titles(){
return $this->hasOne('App\survey_title');
}
public function users(){
return $this->hasOne('App\User');
}
survey_title:
public function survey_questions(){
return $this->hasMany('App\survey_question');
}
user:
public function survey_questions(){
return $this->hasOne('App\survey_question');
}
View:
<div class="col-md-8 col-md-offset-2">
<table class="table table-hover table-bordered table-striped">
<thead>
<tr>
<th>Voted by 0 People </th>
<th>{{$show->title}}</th>
<th>Question ID: {{$show->id}}</th>
</tr>
<tr>
<th>Check</th>
<th>Answer</th>
<th>ID</th>
</tr>
</thead>
<tbody>
#foreach($show->survey_questions as $ans)
<tr>
<td><input type="radio" name="check[]"></td>
<td>{{$ans['answer']}}</td>
<td>{{$ans['id']}}:</td>
</tr>
#endforeach
</tbody>
</table>
<a class="btn btn-primary" href="{{action('surveyController#update',$ans->id)}}">Vote</a>
</div>
Now I put data manually in database and I retrieve.
When click on my 'Vote' I want it to send my Answer Id but instead it always sends last ID from table.
What's wrong?
This is happening because the:
<a class="btn btn-primary" href="{{action('surveyController#update',$ans->id)}}">Vote</a>
is out of the foreach loop, so after the loop is finished, the last id is being set to the link.
Put the href link inside the foreach loop, so it generates a link for each answer
<tr>
<th>Voted by 0 People </th>
<th>{{$show->title}}</th>
<th>Question ID: {{$show->id}}</th>
<th>Vote</th>
</tr>
#foreach($show->survey_questions as $ans)
<tr>
<td><input type="radio" name="check[]"></td>
<td>{{$ans['answer']}}</td>
<td>{{$ans['id']}}:</td>
<td><a class="btn btn-primary" href="{{action('surveyController#update',$ans->id)}}">Vote</a></td>
</tr>
#endforeach

Categories