Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.1 - Pass parameter to Breadcrumbs

I use davejamesmiller Breadcrumbs package. I am wondering how to pass a parameter to a breadcrumb, something like an id.

In the docs (here) it says that is possible, but can't find the way to do it.

My goal es to do a breadcrumb like this: Dashboard \ User \ New Model. Where New Model its a form to add model data with some relationship with the user. Without the user_id param the link for User won't work.

Any idea?

like image 332
Programador Adagal Avatar asked Sep 26 '22 19:09

Programador Adagal


2 Answers

You can pass global variable

\View::share ( 'variable2', $variable2 );

if render breadcrumbs in layout

or You need render breadcrumbs in `user.new_model.blade

@section('content')
{!! Breadcrumbs::render('page', $page) !!}
@stop`

my way

Create template

breadcrumbs.blade.php

with content

@if(!empty($breadcrumbs))
    <ol class="breadcrumb">
    <li>{!! link_to_route('main', 'Home') !!}</li>
    @foreach($breadcrumbs as $bread)
        @if(isset($bread['url']))
            <li>{!! link_to($bread['url'], $bread['name']) !!}</li>
        @else
            <li>{!! $bread['name'] !!}</li>
        @endif
    @endforeach
    </ol>
@endif

and connect it to layout

@include('breadcrumbs')

and in your action pass array of links

\View::share('breadcrumbs', [
        ['url' => route('collection.show', ['id'=>$data->collection, 'url'=>$data->collection]), 'name' => $data->collection->name],
        ['name' => $data->article]
    ]);
like image 74
Yurich Avatar answered Sep 29 '22 03:09

Yurich


There is another way. As general, in each view just calling Breadcrumbs::render() should create the hierarchy of the breadcrumbs links depending on the routes defined in routes/breadcrumbs.php.

There are two essential points that you have keep in mind to go further with this solution:

  1. The callback function that found in breadcrumbs.php routs definition is the place from which you should pass your parameters.
  2. Giving correct name to your web route from routes/web.php which will be used later in routes/breadcrumbs.php

Checkout the following code snippets that demonstrates the above two points:

//Point1: routes/breadcrumbs.php

Breadcrumbs::register('job.edit', function($breadcrumbs, $job, $title)
{
    $breadcrumbs->parent('job','job');  
    $breadcrumbs->push($title, route('job.edit', $job));
});
Breadcrumbs::register('job.edit.install', function($breadcrumbs, $job, $title)
{   
    $breadcrumbs->parent('job.edit',$job, $title);  
    $breadcrumbs->push('Job Install Equipments', route('job.edit.install','job'));
});

In the above code we passed $job and $title through the callback function.

//Point2 routes/web.php
Route::get('/job/edit/{job}', 'JobController@edit')->name('job.edit');
Route::get('/job/install-equipments/{job}', 'JobController@installEquipments')->name('job.edit.install');

We give a name to the route through the name method , Laravel 5.4, which allow us to define the routes correctly in Point1.

The last step, is what you have do in the view file. Here I will show you the last one regarding /job/install-equipments which should be rendered in the breadcrumb as the last element and its parent is job/edit with parameter job which handles the primary key id

//install.equipments.blade.php
@extends('layouts.main')
@section('content')
{!! Breadcrumbs::render('job.edit.install',$job->id, __('Edit').': '.$job->title) !!}

The above will render a breacrumbs look like:

Home / Job / Edit: title of job / Job Install Equipment

The required parameters that handles breadcrumbs render are supplied un the above render method i.e through $job->id and __('Edit').': '.$job->title) , the last just adjusting the text and it could be done inside the callback function of breadcrumbs routes.

like image 40
SaidbakR Avatar answered Sep 29 '22 05:09

SaidbakR