Pagination on 2 separate tables - php

Controller
public function getSearch(Request $request)
{
$search = $request->input('search');
$clients = Client::search($search)
->paginate(5);
$bankings = Banking::search($search)
->paginate(5);
return view('search/search',compact(['clients','bankings']));
}
View
#extends('layouts.master')
#section('content')
<div class="content">
<div class="row search">
<div class="col-xs-12">
<div class="box" >
<div class="box-body" >
<table class="table table-bordered table-hover" id="clients">
<thead>
<th>Customer Code</th>
<th>Surname</th>
<th>Name</th>
<th>Company Name</th>
<th>ID Number</th>
<th>Email</th>
<th>Client Type</th>
<th>Created </th>
</thead>
#foreach($clients as $client)
<tr>
<td>{{$client->customercode}}</td>
<td>{{$client->name}}</td>
<td>{{$client->surname}}</td>
<td>{{$client->companyname}}</td>
<td>{{$client->idnumber}}</td>
<td>{{$client->email}}</td>
<td>{{$client->clienttype}}</td>
<td>{{$client->created_at}}</td>
</tr>
#endforeach
</table>
{{$clients->appends(Request::except('page'))->links()}}
</div>
</div>
</div>
</div>
</div>
<div class="content">
<div class="row search">
<div class="col-xs-12">
<div class="box" >
<div class="box-body" >
<table class="table table-bordered table-hover" id="bankings">
<thead>
<th>Customer Code</th>
<th>Account Holder's Name</th>
<th>Bank</th>
<th>Account Type</th>
<th>Account Number</th>
</thead>
#foreach($bankings as $banking)
<tr>
<td>{{$banking->customercode}}</td>
<td>{{$banking->acc_holders_name}}</td>
<td>{{$banking->bank}}</td>
<td>{{$banking->acc_type}}</td>
<td>{{$banking->acc_no}}</td>
</tr>
#endforeach
</table>
{{$bankings->appends(Request::except('page2'))->links()}}
</div>
</div>
</div>
</div>
</div>
#stop
So i have a search functionality that goes through 2 tables namely Clients & Bankings to search based on user input and bring back the results matching the input. So i added simple laravel pagination on both tables. My Issue is when i click page 2 on Clients table results it also paginates Bankings table results. How can i make it to only paginate one table at a time not both ? Please help

You can add 'names' to the things your paginating:
In the url it will now be ?clients=1&bankings=3 etc.
public function getSearch(Request $request)
{
$search = $request->input('search');
$clients = Client::search($search)
->paginate(5, ['*'], 'clients');
$bankings = Banking::search($search)
->paginate(5, ['*'], 'bankings');
return view('search/search',compact(['clients','bankings']));
}
https://laravel.com/api/5.5/Illuminate/Database/Eloquent/Builder.html#method_paginate

Related

how to download pdf for a line from a table in laravel

i want to generate pdf from the database for 1 row in a table , the table name is "listings"
i used that script for the controller :
class CreatePdfController extends Controller
{
public function pdfForm(Request $request,$id){
$listing = Listing::findOrFail($id);
view()->share('listing',$listing);
if($request->has('download')){
$pdf = PDF::loadView('pdf_download');
return $pdf->download('pdf_download.pdf');
}
return view('show_details')->with('listing', $listing);
}
}
and for the show_details its the view where can i fetch just the data for 1 line in the table :
<div class="col-sm-8 offset-sm-2">
Download PDF
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header col-xs-6">
<div class="col-xs-3">
<h2>Listing Ref n° :{{$listing->id}}</h2>
{{$listing->owner->owner_lastname}}</h4>
</div>
<div class="col-xs-3">
<img src="{{ asset('/assets/images/listings/'.$listing->listing_Fimage) }}" width="120px" height="100px" alt="image" >
</div>
</div>
<div class="card-body">
<div class="col-xs-7 well">
<table id="example1" class="table table-bordered">
<thead>
<tr>
<th scope="row">listing_title:</th>
<td>{{$listing->listing_title}}</td>
</tr>
<tr>
<th scope="row">listing_type:</th>
<td>{{$listing->listing_type}}</td>
</tr>
<tr>
<th scope="row">listing_purpose:</th>
<td>{{$listing->listing_purpose}}</td>
</tr>
<tr>
<th scope="row">listing_location:</th>
<td>{{$listing->listing_location}}</td>
</tr>
<tr>
the pdf_download is the view that i want to generated as pdf :
<table id="example1" class="table table-bordered">
<thead>
<tr>
<th scope="row">listing_title:</th>
<td>{{$listing->listing_title}}</td>
</tr>
<tr>
<th scope="row">listing_type:</th>
<td>{{$listing->listing_type}}</td>
</tr>
<tr>
<th scope="row">listing_purpose:</th>
<td>{{$listing->listing_purpose}}</td>
</tr>
<tr>
<th scope="row">listing_location:</th>
<td>{{$listing->listing_location}}</td>
</tr>
<tr>
<th scope="row">listing_emirate:</th>
<td>{{$listing->listing_emirate}}</td>
</tr>
<tr>
<th scope="row">listing_community:</th>
<td>{{$listing->listing_community}}</td>
</tr>
<tr>
<th scope="row">listing_price:</th>
<td>{{$listing->listing_price}}</td>
</tr>
</table>
and the route that i passed is :
Route::get('/show_details/{id}' ,array('as'=>'show_details',
'uses'=>'CreatePdfController#pdfForm'));
the problem is i get that error and i cannot download the pdf file :
**Symfony\Component\ErrorHandler\Error\FatalError
Maximum execution time of 60 seconds exceeded
http://127.0.0.1:8000/show_details/3?download=pdf**

search Undefined variable in laravel

I want to write a search function but I got the error when I click the search button (Undefined variable: requests)..
this is my form
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<form action="/member_search" methode="get">
<div class="input-group">
<input type="search" name="searchinput" class="form-control">
<span class="input-group-prepend">
<button type="submit" class="btn btn-primary">search</button>
</div>
</form>
</div>
<div class="card-body">
<table class="table">
<thead>
<tr>
<th scope="col">status</th>
<th scope="col">type</th>
<th scope="col">date</th>
<th scope="col"> request number</th>
</tr>
</thead>
<tbody>
#foreach ($requests as $request)
<tr>
<th scope="col">{{$request->status->name}}</th>
<th scope="col">{{$request->type->name}}</th>
<th scope="row">{{$request->created_at}}</th>
<th scope="row">{{$request->uniid}}</th>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
and this is my function in my controller and I got the error Undefined variable: requests with this function
public function searchmember(Request $request)
{
$searchinput= $request->get('searchinput');
$posts= DB::table('students')->where ( 'uniid', 'LIKE', '%' . $searchinput . '%' )->paginate(1);
return view('Committee.Request.index', ['posts' => $posts]);
}
and I this is my route I try to do it as (get) but it does not work
Route::any('/member_search','RequestController#searchmember');
You should use the variable in the loop which variable is passing to view:
#foreach ($posts as $post)
<tr>
<th scope="col">{{$post->status->name}}</th>
<th scope="col">{{$post->type->name}}</th>
<th scope="row">{{$post->created_at}}</th>
<th scope="row">{{$post->uniid}}</th>
</tr>
#endforeach

index.blade.php file not returning any view or scripts?

I'm facing an issue regarding my recent project, where i was trying to save some date through a form in the database (which is working as expected) but when i tried to fetch that data from my database it is not showing me any thing (as if code is not even there) in the view not even scripts but the same code is working in an other view.
Controller code:
public function index()
{
$products = Product::all();
return view('product.index',compact('products'));
}
Blade code:
#include('includes.header')
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12" style="margin: 30px" >
<table class="table table-bordered col-md-12 table-sm">
<thead>
<tr>
<th>ID</th>
<th>Image</th>
<th>Company Name</th>
<th>Title</th>
<th>Price</th>
<th>Status</th>
<th>Description</th>
<th>Created At</th>
<th>Updated At</th>
</tr>
</thead>
<tbody>
#if($products)
#foreach($products as $product)
<tr>
<td>{{$product->id}}</td>
<td><img src="" alt="">{{$product->photo ? $product->photo->file : 'no item photo'}}</td>
<td>{{$product->cname}}</td>
<td>{{$product->title}}</td>
<td>{{$product->price}}</td>
<td>{{$product->status == 1 ? "Active" : "Not Active"}}</td>
<td>{{$product->description}}</td>
<td>{{$product->created_at->diffForHumans()}}</td>
<td>{{$product->updated_at->diffForHumans()}}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
</div>
</div>
</div>
<!-- /#page-wrapper -->
#include('includes.footer')
The main issue is it is behaving like the route is not defined and its view are not set (which is really not the case). I have defined it like
Route::resource('/admin/product','ProductController');
and it is returning me
how could that be possible if the routes are defined and it's not returning it's view.
I have created a custom route and view for it, and returned it's(index.blade.php) view to it and now it's working.

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>

PHP Display data based on student_name, but not based on student_username

I have a difficulty to display an item of a student based on the student_name. Previously I was able to display an item based on student_username but not using student_name. but now I want to use student_name to display the item which from student_name. I attached together with my code part.
<div class="row">
<!-- Welcome -->
<div class="col-lg-12">
<div class="alert alert-info">
<?php
include("connection.php");
$username=$_SESSION['username'];
$sql=mysql_query("SELECT student_name FROM register WHERE student_username='$username'");
$ee=mysql_fetch_array($sql);
?>
<i class="fa fa-folder-open"></i><b> Hello ! </b>Welcome Back <b><?php echo $ee['student_name']; ?> </b>
</div>
</div>
<!--end Welcome -->
</div>
<div class="row">
<div class="col-lg-12">
<div class="table table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>S.no</th>
<th>Parcel Number</th>
<th>Date Recieved</th>
<th>Date of Collected</th>
<th>Recieved Time</th>
<th>Collect Time</th>
<th>Status</th>
<th>Sent By</th>
<th>Sent To</th>
</tr>
</thead>
<?php
include("connection.php");
$sql1=mysql_query("SELECT * FROM add_parcel WHERE send_to='$username'");
$i=1;
while($row=mysql_fetch_array($sql1)){
echo '<tr class="record"><td>'.$i++.'</td><td>'.$row['date_recieve'].'</td><td>'.$row['date_collect'].'</td><td>'.$row['recieve_time'].'</td><td>'.$row['collect_time'].'</td><td>'.$row['status'].'</td><td>'.$row['sender_name'].'</td><td>'.$row['send_to'].'</td></tr>';
}
?>
add_parcel table = parcel_id,parcel_num,date_recieve,date_collect,recieve_time,collect_time,status,sender_name,send_to
register table = student_id,student_username,student_name,student_address,student_age,student_icenumber, student_email,student_phone,password
Try this code instead:
<div class="row">
<!-- Welcome -->
<div class="col-lg-12">
<div class="alert alert-info">
<?php
include("connection.php");
$username=$_SESSION['username'];
$student_name=$_SESSION['student_name'];
$sql=mysql_query("SELECT student_name FROM register WHERE student_name='$student_name'");
$ee=mysql_fetch_array($sql);
?>
<i class="fa fa-folder-open"></i><b> Hello ! </b>Welcome Back <b><?php echo $ee['student_name']; ?> </b>
</div>
</div>
<!--end Welcome -->
</div>
<div class="row">
<div class="col-lg-12">
<div class="table table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>S.no</th>
<th>Parcel Number</th>
<th>Date Recieved</th>
<th>Date of Collected</th>
<th>Recieved Time</th>
<th>Collect Time</th>
<th>Status</th>
<th>Sent By</th>
<th>Sent To</th>
</tr>
</thead>
<?php
include("connection.php");
$sql1=mysql_query("SELECT * FROM add_parcel WHERE send_to='$student_name'");
$i=1;
while($row=mysql_fetch_array($sql1)){
echo '<tr class="record"><td>'.$i++.'</td><td>'.$row['date_recieve'].'</td><td>'.$row['date_collect'].'</td><td>'.$row['recieve_time'].'</td><td>'.$row['collect_time'].'</td><td>'.$row['status'].'</td><td>'.$row['sender_name'].'</td><td>'.$row['send_to'].'</td></tr>';
}

Categories