Getting breadcrumbs in laravel 4 - laravel-4

I'm using the davejamesmiller package to get the breadcrumbs and I don't think I'm understanding it very well.
I keep getting an error on my dynamically created pages that says
Trying to get property of non-object (View: /Applications/MAMP/htdocs/test/app/views/layouts/home.blade.php) (View: /Applications/MAMP/htdocs/test/app/views/layouts/home.blade.php)
When I use this code
Breadcrumbs::register('page', function($breadcrumbs, $page) {
$breadcrumbs->parent('home');
$breadcrumbs->push($page->title, route('page', $page->id));
});
Here is all my code
home.blade.php
<!-- HEADER -->
<div class="row">
<div class="col-lg-9 col-sm-9 col-xs-9">
<div class="heading">
<h2>Testing</h2>
</div>
{{ Breadcrumbs::render() }}
</div>
</div>
<!-- HEADER -->
and this is my app/breadcrumbs.php
Breadcrumbs::register('home', function($breadcrumbs) {
$breadcrumbs->push('Home', route('home'));
});
Breadcrumbs::register('page', function($breadcrumbs, $page) {
$breadcrumbs->parent('home');
$breadcrumbs->push($page->title, route('page', $page->id));
});
My routes
Route::get('/', ['uses' => 'HomeController#index', 'as' => 'home']);
Route::get('page/{id}', ['uses' => 'ContentController#show', 'as' => 'page']);
If there is anything else that I need to give please let me know

You are getting error because you have pass nothing to your breadcrumbs.
{{ Breadcrumbs::render() }}
You should do something like this:
{{ Breadcrumbs::render('home', $page) }}
Code:
<!-- HEADER -->
<div class="row">
<div class="col-lg-9 col-sm-9 col-xs-9">
<div class="heading">
<h2>Testing</h2>
</div>
{{ Breadcrumbs::render('home', $page) }}
</div>
</div>
<!-- HEADER -->

Related

How do I do pagination on my code in Laravel?

So my front-end Lists of Businesses are not in paginated style. But I do not know how to do it. Can anyone please help? The code I posted is in my BusinessListController.php
BusinessListController.php
`<?php
namespace App\Http\Controllers;
use App\Models\Business;
use App\Models\Category;
use App\Models\Location;
use Illuminate\Http\Request;
class BusinessListController extends Controller
{
public function index(Request $request)
{
$businesses = Business::query()
->with('location')
->whereFilters($request->only(
['search', 'category', 'location']
))
->get();d
return view('pages.business-list', [
'businesses' => $businesses,
'locations' => Location::all(),
'categories' => Category::all()
]);
}
}`
And then here is the code for my view blade front-end
Business-List.blade.php
<div class="row business-list-row mx-auto">
#foreach ($businesses as $business)
<div class="col-md-4">
<div class="card shadow border-light mb-3">
<img
src="https://cdn1.clickthecity.com/images/articles/content/5d6eba1f4795e0.58378778.jpg"
class="card-img-top" alt="...">
<div class="card-body">
<div class="d-flex justify-content-between">
<div>
<h4 class="card-title h6" style="font-weight: bold;">
{{Str::limit($business->name, 20, $end='...')}}
</h4>
<div class="">
<p class="card-text">
{{ $business->location?->name }}
</p>
<p class="card-text" style="color: #32a852;">
{{ $business->category?->name}}
</p>
</div>
</div>
<div class="align-self-center">
<a href="{{ route('store', $business->id) }}" class="btn btn-info stretched-link">
Visit
</a>
</div>
</div>
</div>
</div>
</div>
#endforeach
</div>
So you need to do three things.
In Controller:
$businesses = Business::query()
->with('location')
->whereFilters($request->only(
['search', 'category', 'location']
))
->paginate(15);
put the number of items you need on a single page. here I put 15.
Put this under the </div> of your list.
{{ $business->links() }}
Put this inside the App\Providers\AppServiceProvider boot method.
use Illuminate\Pagination\Paginator;
public function boot()
{
Paginator::useBootstrapFive(); // or
Paginator::useBootstrapFour();
}
This depends upon which version of Bootstrap you are using.
Still confused? Checkout Laravel Pagination Documentation
Just remove ->get();d and add paginate
example
ModelName()->paginate();

URL with two parameters

I want to generate URL with two parameters. In my web.php I created a route:
Route::get('/pojedinacni-turnir/{godina}/kolo/{kolo}', [
'uses' => 'Frontend\PojedinacniTurnirController#show',
'as' => 'pojedinacni.turnir',
]);
where are my two parameters are godina and kolo.
In my Controller I create show function with two parameters: id from godina and id from kolo.
Here is code from my controller:
<?php
namespace App\Http\Controllers\Frontend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Sezona;
use App\TurnirPojedinacni;
class PojedinacniTurnirController extends Controller
{
public function show($id_sezona, $id_kolo)
{
$sezone = Sezona::orderBy('godina', 'desc')->get();
$sezona = Sezona::findOrFail($id_sezona);
$kola = TurnirPojedinacni::where('sezona_id', $id_sezona)->orderBy('id', 'asc')->get();
$kolo = TurnirPojedinacni::findOrFail($id_kolo);
return view('frontend.pojedinacni_turnir', compact('sezone', 'sezona', 'kola', 'kolo'))->render();
}
}
When I try to check my URL i get this message:
Missing required parameters for [Route: pojedinacni.turnir] [URI: pojedinacni-turnir/{godina}/kolo/{kolo}]. (View: C:\WebSites\TkPazin\TK_Pazin\resources\views\frontend\pojedinacni_turnir.blade.php)
I don't understand error because i try URL with two parameters. I created URL with id's that exist like this:
{{ route('pojedinacni.turnir', ['godina' => 1, 'kolo' => 1]) }}
and even then i get the same error message.
Update: added blade file
#extends('layouts.frontend')
#section('title', 'TK Pazin | Pojedinačni turnir')
#section('css')
<link href="/css/style_pojedinacni_turniri.css" rel="stylesheet"/>
#endsection
#section('content')
<!-- Page Content -->
<div class="container">
<!-- Page Heading -->
<h1 class="my-4" style="text-align:center; color: #ba3631;">Pojedinačni turniri {{ $sezona->godina }}</h1>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
#foreach($sezone as $sezona)
<li class="breadcrumb-item">{{ $sezona->godina }}</li>
#endforeach
</ol>
</nav>
<div class="row mb-5">
<div class="col-12 col-md-2 mb-5">
<div class="list-group">
#foreach($kola as $kolo)
<button type="button" class="list-group-item list-group-item-action">{{ $kolo->naziv }}</button>
#endforeach
</div>
</div>
</div>
</div>
<!-- /.container -->
#endsection
You blade file shows that you have only added 1 parameter
#foreach($sezone as $sezona)
<li class="breadcrumb-item">{{ $sezona->godina }}</li>
#endforeach
Add the additional parameter
{{ $sezona->godina }}

Two controllers one route laravel

I can currently show the index page along with a list of posts. The user can select a post to view post details. For this I have:
Routes:
Route::get('/', 'PostsController#showPosts');
Route::get('post/{slug}', 'PostsController#showPostDetails');
Controller:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function showPosts()
{
$posts = Post::simplePaginate(2);
return view('index', ['posts' => $posts]);
}
public function showPostDetails($slug)
{
$post = Post::findBySlug($slug);
return view('post.show',['post'=>$post]);
}
}
Model:
public static function findBySlug($slug)
{
return static::where('slug', $slug)->first();
}
index
#extends('layouts.index')
#section('header')
<!-- Page Header -->
<header class="masthead" style="background-image: url('img/home-bg.jpg')">
<div class="overlay"></div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="site-heading">
<h1>Train Testing</h1>
<span class="subheading">Testing Times</span>
</div>
</div>
</div>
</div>
</header>
#stop
#section('content')
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
#foreach ($posts as $post)
#include('partials.post', ['post' => $post])
#endforeach
{{ $posts->links() }}
</div>
</div>
#stop
partials/post.blade.php
<div class="post-preview">
<a href="/post/{{ $post->slug }}">
<h2 class="post-title">
{{ $post->title }}
</h2>
<h3 class="post-subtitle">
{{ $post->excerpt }}
</h3>
</a>
<p class="post-meta">Posted by
{{ $post->author->name }}
on {{ $post->created_at->format('l d F, Y') }}</p>
</div>
<hr>
I now want to show the posts on all other pages. My other pages currently have the route Route::get('{slug}', 'PagesController#show'); And it makes sense initially to refer back to existing code so use Route::get('{slug}', 'PostsController#showPosts'); like I did on the home page to display posts there.
However I am not sure of the best way to deal with this as I believe you can not have two controllers for one route.
For other pages I currently have:
Controller:
<?php
namespace App\Http\Controllers;
use App\Page;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function show($slug)
{
$page = Page::findBySlug($slug);
return view('page', ['page' => $page]);
}
}
page.blade:
#extends('layouts.index')
#section('header')
<!-- Page Header -->
<header class="masthead" style="background-image: url('/storage/{{ $page->image }}')">
<div class="overlay"></div>
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<div class="page-heading"> <h1>{!! $page->title !!}</h1>
<span class="subheading"></span>
</div>
</div>
</div>
</div>
</header>
#stop
#section('content')
<!-- Main Content -->
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
{!! $page->body !!}
</div>
</div>
</div>
#stop
Pages Controller:
use App\Page;
use App\Post;
use Illuminate\Http\Request;
class PagesController extends Controller
{
public function show($slug)
{
$posts = Post::simplePaginate(2);
$page = Page::findBySlug($slug);
return view('page', ['page' => $page, 'posts' => $posts]);
}
Then in page.blade.php I can call $posts without any errors:
#foreach ($posts as $post)
#include('partials.post', ['post' => $post])
#endforeach
{{ $posts->links() }}
Not sure if this is the neatest way but it works.

Comparing dates in view and applying class

I am currently displaying projects within a view using a foreach loop.
#foreach ($projects as $project)
<div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
{!! Form::open(array('class' => 'form-inline delete', 'method' => 'DELETE', 'route' => array('projects.destroy', $project->id))) !!}
<div class="panel panel-default">
<div class="panel-heading projectPanelHeading">
<h4>{{ $project->projectName }}</h4>
</div>
<div class="panel-body projectPanel">
<p>Deployment Date: {{ ($project->deploymentDate ? date('d-m-Y', strtotime($project->deploymentDate)) : '') }}</p>
<p>Status: {{ $project->status or '' }}</p>
</div>
</div>
{!! Form::close() !!}
</div>
#endforeach
As you can see, each project has a $project->deploymentDate which I convert to a date object with the format d-m-Y.
What I am trying to do is compare the deploymentDate with the current date, and if the deploymentDate has passed, to apply a panel-red class to the panel.
I was thinking about doing something like this
{!! Form::open(array('class' => 'form-inline delete', 'method' => 'DELETE', 'route' => array('projects.destroy', $project->id))) !!}
#if(date('d-m-Y', strtotime($project>deploymentDate)) < date('d-m-Y'))
<div class="panel panel-default panel-red">
<div class="panel-heading projectPanelHeading">
<h4>{{ $project->projectName }}</h4>
</div>
<div class="panel-body projectPanel">
<p>Deployment Date: {{ ($project->deploymentDate ? date('d-m-Y', strtotime($project->deploymentDate)) : '') }}</p>
<p>Status: {{ $project->status or '' }}</p>
</div>
</div>
#else
<div class="panel panel-default">
<div class="panel-heading projectPanelHeading">
<h4>{{ $project->projectName }}</h4>
</div>
<div class="panel-body projectPanel">
<p>Deployment Date: {{ ($project->deploymentDate ? date('d-m-Y', strtotime($project->deploymentDate)) : '') }}</p>
<p>Status: {{ $project->status or '' }}</p>
</div>
</div>
#endif
{!! Form::close() !!}
What I do not like about this approach is the repetition of the panel block. Additionally, there are a lot of mistakes with panel-red being applied to projects who's deploymentDate has not yet passed, and vice versa.
Is there a better way to achieve what I am after which is both correct, and does not require repetition?
Thanks
First of all, add the deploymentDate attribute to the $dates array in your Project model. This will ensure you have Carbon instances for your deploymentDate.
Project.php
protected $dates = ['deploymentDate'];
Once achieved you can check the if it's less than by simple function:
#if($project->deploymentDate->lt(\Carbon\Carbon::Now()))
This can also be achieved by a method on the model, in your Project.php
public function checkDeploymentDate()
{
return $this->deploymentDate->lt(\Carbon\Carbon::Now());
}
Then in your views:
#if($project->checkDeploymentDate())
Finally for the panel part, you can add a Blade directive for it:
AppServiceProvider.php
<?php
namespace App\Providers;
use Blade;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* #return void
*/
public function boot()
{
Blade::directive('deploymentDate', function($expression) {
return "<?php echo($expression); ?>";
});
}
}
Blade directives will help you extend new functionality to your Blade templates. So in the above example, in your view template:
{{#deploymentDate(test)}} will echo test

how to clear cache search results from server in laravel 5?

i'm new to laravel and i've created a simple search to query my database.
the problem is that everytime i refresh the page all the results from my last search are still showing. also when i load the page after couple of days,results are the. it seems like there is a cache working which i dont know about.
i want to clear these results/cache without clearing the cache for all my app.
Here is the code.
in my routs file:
Route::get('men',function()
{
$query=Request::get('q');
if ($query)
{
$users= \App\User::where('first_name','LIKE',"%$query%")
->orwhere('last_name','LIKE',"%$query%")
->get();
}
else
{
}
return view('page')->withUsers($users);
});
My View:(page.blade.php)
{!! Form::open(['method' => 'GET']) !!}
<div class="container col-lg-6 form-group" style="float:none;margin:auto">
<div class="input-group">
{!! Form::input('search','q', null, ['placeholder' => 'search' ,
'class' => 'form-control', 'autocomplete' => 'off']) !!}
<div class="input-group-btn">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Search</button>
</div><!-- /btn-group -->
</div><!-- /input-group -->
</div>
{!! Form::close() !!}
<div class="container col-lg-6" style="float:none;margin:auto">
#if ($users->count())
<ul class="list-group" style="list-style-type: none;">
#foreach($users as $user)
<li class="list-group-item" style="cursor:pointer">
{{$user->first_name}} {{$user->last_name}}
</li>
#endforeach
</ul>
#else
<h3>Sorry,no users found...</h3>
#endif
</div>
</div> <!--container-->
any Ideas ??..
i've managed to clear last query string in the browser addess bar with:
within my view:
<head>
<script type="text/javascript">
var uri = window.location.toString();
if (uri.indexOf("?") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("?"));
window.history.replaceState({}, document.title, clean_uri);
}
</script>
</head>
whenever i refresh the page it clears up the results since nothing is coming from the server.

Resources