How do I replace the custom laravel pagination with images?
I would also like to get the next set of data without reloading the page.
My controller looks like this.
class HomeController extends Controller
{
public function index()
{
$featured_products = DB::table('products')
->where('feature_type','=',3)
->orderBy('created_at','DESC')
->simplePaginate(4);
$latest_products = DB::table('products')
->orderBy('created_at','DESC')
->simplePaginate(4);
return View::make('pages.home')
->with(['featured_products'=>$featured_products,'latest_products'=>$latest_products]);
}
}
The easiest way would be to just edit the blade file for the pagination.
Firstly, in your console run:
php artisan vendor:publish --tag=laravel-pagination
Then go to resources/views/vendor/pagination/default.blade.php and you'll see:
#if ($paginator->onFirstPage())
<li class="disabled"><span>«</span></li>
#else
<li>«</li>
#endif
...
#if ($paginator->hasMorePages())
<li>»</li>
#else
<li class="disabled"><span>»</span></li>
#endif
You can just replace the « and » with the images you want to use.
https://laravel.com/docs/5.3/pagination#customizing-the-pagination-view
Hope this helps!
In 5.3 you can customize pagination views.
In other versions you can try to override this with CSS or you could try to show links manually instead of using $results->render() method:
$results->count()
$results->currentPage()
$results->firstItem()
$results->hasMorePages()
$results->lastItem()
$results->lastPage() (Not available when using simplePaginate)
$results->nextPageUrl()
$results->perPage()
$results->previousPageUrl()
$results->total() (Not available when using simplePaginate)
$results->url($page)
Related
I have a Blade template that I would like to use from multiple Controllers, as although the DB table is different, everything else is the same.
For example, I have the following line in the template:
<li>View All Manufacturers</li>
I would like to make this dynamic, so I can improve so that it is:
<li>View All {{ name }}</li>
But how do I also apply this to the first part?
You have to extend the blade for this.
create a custom directive for this.
in your AppServiceProvider
public function boot()
{
Blade::directive('myDirective', function ($expression) {
return "<li>View All ". ucfirst($expression)."</li>";
}
And now in your view you can use
#myDirective('manufacturer')
it will return your desired input.
I am new to Laravel , I am getting 404 not found error when returning view to salary report from my controller. The below mentioned is my function which returns my simple view to salary report.
public function getSalaryReport()
{
return view('Company.salaryReport');
}
the routes.php file ahs route to company controller.
Route::group(['middleware' => 'auth.company'], function () {
Route::get('company/notice-board/create', 'CompanyController#getNoticeBoardCreate');
Route::get('company/notice-board/{id}/edit', 'CompanyController#getNoticeBoardEdit');
Route::get('company/designation/{id}/edit', 'CompanyController#getDesignationEdit');
Route::get('company/all-user/{id}/force', 'CompanyController#getForce');
Route::post('company/all-user/{id}/force', 'CompanyController#postForce');
Route::controller('company', 'CompanyController')
this is my view which i am trying to display from my controller.
#extends('Company.CompanyLayout')
#section('content')
<div>
<ul class="breadcrumb">
<li>
Home <span class="divider">/</span>
</li>
<li>
<a href='{!! URL::to("company/report-summery") !!}'>Summery Report</a>
</li>
</ul>
</div>
#endsection
where i am going wrong and what should be done to make my view visible. Thanks to all in advance.
Route::controller is depricated in the latest versions of Laravel, try not to use it anymore.
You can use Route::resource or create a specific route for your salary report like this:
Route::get('company/salary-report', 'CompanyController#getSalaryReport');
Also make sure that you have resources\views\Company\salaryReport.blade.php as your view.
404 not found is an error because you don't have any routes for the given url. And I didn't find any routes in your example for the function getSalaryReport()
if you want to call this method, at least add this to your routes:
Route::get('company/report-summery', 'CompanyController#getSalaryReport');
I have a problem when I learning laravel. My views include master.blade.php and top.blade.php file.
In master.blade.php, I used #include('top') command to get content show UI. But now I don't know how to get and passing database to top.blade.php. I was used direct App\Article; to do this. can anyone help me? thanks.
Master.blade.php file
#include('top')
Top.blade.php file
<?php
use App\Article;
$articles = Article::orderBy(DB::raw('RAND()'))->take(1)->get();
?>
#foreach ($articles as $a)
{{ $a->title }}
#endforeach
You could do this:
<?php
$articles = \App\Article::orderBy(DB::raw('RAND()'))->take(1)->get();
?>
But it's better to keep data logic in a model or a controller and pass it to the views.
Juste use a Controller to pass database data in to your view, it should be
ArticlesController.php
$articles = Article::orderBy(DB::raw('RAND()'))->take(1)->get();
return view('master', compact('articles');
It's just an example, but now top.blade.php which is included in master, will contain $articles.
I'm working with laravel pagination, however I wish to display the pagination links regardless of if there is only a single page, or multiple pages.
Currently, it is only displayed if there are multiple pages of results.
Eloquent Call
$products = Product::where('username', Sentry::getUser()->username)->paginate(25);
and then displayed in the view using
{!! $products->links() !!}
How can I force Laravel to display it when there is only a single page?
Publish pagination views:
php artisan vendor:publish --tag=laravel-pagination
This command will place the views in the resources/views/vendor/pagination directory.
Default view is bootstrap-4.blade.php and just delete the #if ($paginator->hasPages()) in this file.
Following Alexey Mezenin's answer, I extended the BootstrapThreePresenter class:
<?php namespace App\Extend;
use Illuminate\Pagination\BootstrapThreePresenter;
class CustomPaginationLinks extends BootstrapThreePresenter {
public function hasPages()
{
return true;
}
}
And was then able to render in the view like so:
{!! with(new App\Extend\CustomPaginationLinks($products))->render() !!}
There is no easy way, because it's hardcoded. However you could extend SimpleBootstrapThreePresenter and override hasPages() method:
public function hasPages()
{
return true;
}
Instead of:
public function hasPages()
{
return $this->paginator->hasPages() && count($this->paginator->items()) > 0;
}
Using the package mcamara/laravel-localization in Laravel 5.1 I managed to localize my site and also to translate the routes. The problem is now the following: How can I add a custom class "active" via blade template depending on the current route?
I have tried so far using HTML::macro but it seems the package laravelcollective/html: "5.1.*" isn't fully compatible (especially macros) with L5.1.
Even if I would manage to use an macro I can not use the Request::is('about') because the routes are translated. I'm pretty sure here has to be an easy approach...
Example routes:
www.sitename.com/en/about = www.sitename.com/ro/despre => route to same controller/action
Try this!
<li class="#if (Request::is('/')) {{'active'}} #endif">Home <span class="sr-only">(current)</span></li>
<li class="#if (Request::is('about')) {{'active'}} #endif">About</li>
<li class="#if (Request::is('contact')) {{'active'}} #endif">Contact</li>
With inspiring help from #keithm and from here I did the following:
First extend blade with a new directive, built directly in the AppServiceProvider.php
Blade::directive('activeState', function($expression) {
return '<?php echo activeClass('. $expression . '); ?>';
});
Then in your helpers file (if you don't have one you can create it into app/Http/helpers.php) add the following
function activeClass($url) {
return Request::url() == $url ? 'active' : '';
}
In blade directly use then the following directive:
<a href="{{ URL::route('front.portfolio.index') }}" class="nav-block #activeState(URL::route('front.portfolio.index'))">
Not sure if I got your question but is this what you're looking for?
<span class="someClass #if (Request::url('/myurl') active #endif"></span>
Sorry missed that part of the question :).
I didn't test it this way but it should work:
#if (Request::url($variable or $pattern .'/restofuri')
A little late to the party, but i also had the same issue and solved it with the laravel helper function url()->current() and the localization package helper function localizeURL
<a href="{{LaravelLocalization::localizeURL(trans('routes.my-route'))}}"
class="{{(url()->current() == LaravelLocalization::localizeURL(trans('routes.my-route'))) ? "active" : "" }}">
{{trans('navigation.my-route')}}</a>