I'm using sort function and pagination but the problem when I use sort for any column works fine but when I click to the next page using pagination the sort change and have to click on the column again for sorting.
so how to keep sorting while using pagination?
<tr>
<th>
<div class="checkbox checkbox-replace">
<input type="checkbox" id="chk-3">
</div>
</th>
<th>#sortablelink('details','Details')</th>
<th>#sortablelink('postingdate','Date')</th>
<th>#sortablelink('description','Description')</th>
<th>#sortablelink('amount','Amount')</th>
<th>#sortablelink('type','Type')</th>
<th>#sortablelink('slip','Slip#')</th>
<th>#sortablelink('vendor_id','Vendor')</th>
<th>#sortablelink('category_id','Category')</th>
</tr>
public function index()
{
$checks = Checks::orderBy('id', 'asc')->get();
$checks= Checks::sortable()->paginate(5);
return view('home',compact('checks'));
}
You can append query string to your link by doing this :
{{ $users->appends(['sort' => 'votes'])->links() }}
replace sort by your sortable query, and votes by something like request()->sort
According to this page, you also can do your pagination like this:
//Controller
public function index()
{
$checks = Checks::orderBy('id')->paginate(5);
$links = $checks ->appends(['sort' => 'id'])->links();
return view('home', compact('checks', 'links'));
}
And in your view, you can just call your pagination links like this
{{ $links }}
Hope it helps. Cheers!
{{links()}} will still give a messed up sort when you paginate. This is the same as {!! link() !!} also and {{ $checks->links()}}
Rather, use the code below. It will correct the error.
//for the Controller
public function index()
{
$checks = Checks::sortable()->paginate(5);
return view('home', compact('checks'));
}
//for the blade template in laravel append this in div after the table
{!! $checks->appends(\Request::except('page'))->render() !!}][1]
you can use this line in your blade file
{!! $product->appends(\Request::except('page'))->render() !!}
Related
I am trying to use pagination for my view pages but getting error as : Call to a member function links() on array
Limiting to display records using paginate in controller is fine,but issue is when passing to the view. Controller code is:
$referredProcedureBookings = Gateway::procedurebooking()->getReferredProcedureBooking()->paginate(5);
$new_records = [];
foreach ($referredProcedureBookings as $referredProcedureBooking)
{
$records['ProcedureId'] = $referredProcedureBooking->id;
$records['ProcedureName'] = $referredProcedureBooking->procedure_name;
$new_records[] = $records;
}
return view('procedurebookings',compact('new_records');
In my view its like this:
#foreach($new_records as $new_recordss)
<td><div class="col-md-2"><h5 class="panel-body">{{ $new_recordss['Name'] }}</h5></div></td>
<td><div class="col-md-2 col-md-pull-6"><h5 class="panel-body">{{ $new_recordss['Age'] }}</h5></div></td>
#endforeach
{{ $new_records->links() }}
Error i am getting is :Call to a member function links() on array.
Even if i use :{{ $new_records[links()] }}
Then i get :Call to undefined function links()
how should i use the links method?
links method available on $referredProcedureBookings
$referredProcedureBookings->links()
so pass $referredProcedureBookings to view first.
On my index.blade i like to show two different sets of data, one is a list of computers the other one list of categories. two different type of data. in my controller I have
public function index()
{
// Select data from table assets
$assets = DB::table('assets')->where('status', '1')->orderBy('type', 'asc')->paginate(10); //pass all the employees to a Var and send it to our page using "with" also puts page # at the bottom
//select data from table assetrefs
$categories = DB::table('assetrefs')->get();
//bind data and send using 'with'
return view('assets.index')->with('assets', $assets)->with('categories', $categories);
}
on my view side I have these two loops
#if(count($categories) > 1)
#foreach($categories as $row)
{!!Form::open(['action' => ['AssetsController#showType', $row->title], 'method'=> 'POST', 'class'=> 'pull-left'])!!}
{{Form::submit('Show '. $row->title, ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
#endforeach
#else
<p> no content </p>
#endif
This shows a bunch of button with categories
<div class="well"></div>
<!-- lists all the available assets in a div all types are mixed but sorted -->
#if(count($assets) > 1)
#foreach($assets as $asset)
<div class="well">
<h3>{{$asset->type}}, {{$asset->make}} - {{$asset->model}} </h3>
<small> {{$asset->sn}}</small>
<small> View this account</small>
</div>
#endforeach
{{$assets->links()}}
#else
<p> no content </p>
#endif
this shows a list of all computers
When I load the page I get
2/2 ErrorException
Undefined variable: categories (View: resources\views\assets\index.blade.php)
Can you please pass data to view by making use of array instead of with like below:
public function index()
{
// passing data without with
$data['assets'] = DB::table('assets')->where('status', '1')->orderBy('type', 'asc')->paginate(10);
$data['categories'] = DB::table('assetrefs')->get();
return view('assets.index',$data);
// in case if you want to use with then
// return view('assets.index')->with($data);
}
Or even if you don't want to use $data variable want to keep $asset and $categories variable as it is then
public function index()
{
// passing data without with
$assets = DB::table('assets')->where('status', '1')->orderBy('type', 'asc')->paginate(10);
$categories = DB::table('assetrefs')->get();
return view('assets.index',compact('assets','categories'));
}
You can check how compact function is working in php by url -> http://php.net/manual/en/function.compact.php
Try this in your Controller in place of your view() call:
return view('assets.index', compact('assets', 'categories'));
Have a look at Passing Data To Views and you'll see view()->with() is suited for passing individual pieces of data whereas passing an array as a second argument is required for multiple variables. Here we're passing an array of your already defined assets and categories via PHP's compact function.
i'm new in laravel, i have some problem to make database as variable to be shown on blade template,
example i want to get any data from database :
{{$get = DB::table('perangkat')->get()}}
then:
#foreach ($get as $got)
{{$got->jabatan}}
#endforeach
if the result of the $got->jabatan is kepala, But i want the result is Change To "Kepala Desa", How can i do ??
Please Help Mee ...
Write database related stuff in model or controller in a method and pass that value to view. It will be very neat and clear
public function getData(){
$get = DB::table('perangkat')->get();
return view('myblade', ['data' => $get]);
}
In your view
#foreach ($data as $got)
{{$got->jabatan}} Desa
#endforeach
Also can you update where you get Desa .so i can update answer according to that.
Updated
you keep different roles in array and do some think like this
<?php
$role=['a'=>'operator','b'=>'admin'] ; ?>
#foreach ($data as $got)
<?php $result = isset($role[$got->jabatan]) ? $role[$got->jabatan] : null; ?>
{{$result}}
#endforeach
company.php
function showdata{
$sql= "this syntax sql";
}
//show data
$company = new company();
#foreach ($company->showdata() as $got)
{{$got->jabatan}}
#endforeach
I need to find out unique value. So I tried bellow code. It is through undefined variable error.
Controller:
$employee = Employee::all();
Return view ('page', compact('employee'));
Page view:
$uniqueEmpLoc = $employee->unique('location')->values()->list('location')->toArray();
#Foreach($uniqueEmpLoc as $empLoc)
{{ $empLoc }}
//this is select box used for search
#endforeach
//Display Entire data
#foreach($employee as #employee)
//Display all value
#endforeach
But I got an uniqueEmpLoc is undefined error. I'm using LARAVEL 5.1. Please help me to solve this problem.
There are some errors in your code:
I don't think compact(employee) would work. Shouldn't that suppose to be compact('employee') ?
In Blade, there is no need of putting curly braces at all. Remove them.
Try out the following:
$employees = Employee::unique('locations')->values()->list('location')->toArray();
return view('page', compact('employees'));
And then in your view:
#foreach($employees as $employee)
{{ $employee }}
#endforeach
Use this query in controller
$employees = Employee::distinct()->list('location')->toArray();
return view('page', compact('employees'));
In view
#foreach($employees as $employee)
{{ $employee }}
#endforeach
I agree with the other answers here, this code should be in the controller. You shouldn't be doing logic in the views.
In the controller do:
$uniqueEmpLoc = $employee->unique('location')->values()->list('location')->toArray();
$employee = Employee::all();
Return view ('page', compact('employee', 'uniqueEmpLoc'));
The reason your code isn't working is the line that defines $uniqueEmpLoc is interpreted by blade as text, not code.
If you really want to do that in the view, you need to wrap it in #php tags.
#php
$uniqueEmpLoc = $employee->unique('location')->values()->list('location')->toArray();
#endphp
#Foreach($uniqueEmpLoc as $empLoc)
{{ $empLoc }}
//this is select box used for search
#endforeach
I'm new to Laravel and I'm getting an error which I think has more to do with logic than anything else but I can't quite seem to grasp how to overcome it.
So, I have a page with a simple form to search for a particular string in my database. But I want to have the result show up on the same page.
Here's what I have so far:
This is my Route.php:
Route::get('/', 'HomeController#index');
Route::post('find', 'HomeController#find');
This is my HomeController:
public function index()
{
return View::make('index');
}
public function search()
{
return View::make('index');
}
public function find()
{
$match = Input::get('find');
if($match) {
$results = Book::where('title', 'like', '%'.$match.'%')
->orWhere('author', 'like', '%'.$match.'%')
->get();
return View::make('index', array('results', $results));
} else {
return Redirect::to('/')->with('flash_error', 'No string added!');
}
}
And my View (index.blade.php):
{{ Form::open(array('url' => 'find', 'method' => 'POST')) }}
{{ Form::text('find', '', array('class' => 'search-query', 'placeholder' => 'Search')) }}
{{ Form::submit('Submit', array('class' => 'btn btn-info')) }}
{{ Form::close() }}
#if (Session::has('flash_error'))
{{ Session::get('flash_error') }}
#endif
#foreach ($results as $result)
{{$result->title}}
#endforeach
(eventually the foreach will be replaced by some ajax loading to display each result)
And the error says "undefined variable: results" and shows the foreach.
I get why that error shows up since on the first pass to this page the results haven't been loaded yet but how can I overcome this? I really want the data to be shown on the same page without having to go to another page to display them.
Like I said, I think this is mostly logic related (although I'm very new to Laravel so it might be that too)
Any help would be greatly appreciated !
you need to pass an associative array as your second param of the make method
return View::make('index', array('results' => $results);
The problem here is that in your use of index.blade.php in multiple controllers, you forgot which controllers provide which variables (and as a result, which variables may be omitted).
When you request / (HomeController#index), index.blade.php is rendered, but since no $results are passed to the view, you see the Undefined Variable warning. This is not a problem in HomeController#find, because you define $results. To combat this, you'll need to do something along the lines of an isset() check on $results before you foreach over it. Like so:
#if(isset($results))
#foreach ($results as $result)
{{$result->title}}
#endforeach
#endif
Your logic may vary based on your page's layout (you might want to add an else and display some alternate placeholder content).
Also, if abstracting the call to View::make() with $results into index_gen() isn't keeping your code DRY, then I'd suggest replacing it in find() with the call to View::make().