I'm using Laravel 5.2 and I would like to use breadcrumbs by davejamesmiller. I have set route Route::resource('admin/users', '\Easyk\Controllers\Admin\UsersController'); and according documentation created file Http/breadcrumbs.php where content is:
Breadcrumbs::register('admin.users.index', function ($breadcrumbs) {
$breadcrumbs->push('Users', route('admin.users.index'));
});
When I try in template {!! Breadcrumbs::render() !!} then it will render without problem. But I would like to render it manualy. According documentation I created file config/breadcrumbs.php with content:
return [
'view' => 'partials/_breadcrumbs',
];
but when I try to render it there is no $breadcrumbs variable:
#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
Do you have any experience how to get $breadcrumbs variable to the view specified in file?
I already solved it. In layout I need {!! Breadcrumbs::renderIfExists() !!} and then in partials/_breadcrumbs I will get $breadcrumbs variable. If I get it right then method renderIfExists() works as include.
Also in config/breadcrumbs.php must be dot convention:
return [
'view' => 'partials._breadcrumbs',
];
Related
I am trying to use it in my layout and I'm suddenly bump-up with the idea of using the new features of Laravel 5.4 #slot.
Just wondering if passing an array to a slot possible?
#section('SampleSection')
#component( 'mylayouts.partials.contentheader' )
#slot('title')
Sample Title
#endslot
#slot('indexes')
Pass array here // example [ 'a', 'b', 'c' ]
#endslot
#endcomponent
#endsection
As far as I know, it isn't possible to pass an array to a component via #slot. However, it is possible to pass and an array to a component directly, without using a slot.
From the documentation:
Passing Additional Data To Components
Sometimes you may need to pass additional data to a component. For
this reason, you can pass an array of data as the second argument to
the #component directive. All of the data will be made available to
the component template as variables:
#component('alert', ['foo' => 'bar'])
...
#endcomponent
So let's say you have an array of data:
$my_array = ['a', 'b', 'c']
You can pass it to the component like so:
#component('mylayouts.partials.contentheader', ['my_array' => $my_array])
...
#endcomponent
Or you can just pass it directly, without using a variable:
#component('mylayouts.partials.contentheader', ['my_array' => ['a', 'b', 'c']])
...
#endcomponent
Then, within your component file, you will have access to a $my_array variable. For example, in your contentheader.blade.php file you can do this:
<ul>
#foreach($my_array as $value)
<li>{{ $value }}</li>
#endforeach
</ul>
I need the same.
This works as a temporally solution, but is not the best form for do it.
#component('component.tab.header')
#slot('navs', [
"a" => "Pepe",
"b" => "Maria"
])
#endcomponent
EDIT:
Finally, in my case, I solved doing this:
#component('component-name')
#slot('slot-name')
Value1|Value2|Value2
#endslot
#endcomponent
And in the component code:
#foreach(explode('|', $slot-name) as $value)
The value is {{ $value }} <br/>
#endforeach
Here is a sample component. Assign number of items to $numberOfSlots variable and it will search for slot blocks one by one (body_1, body_2, body_3)
Because I build a form wizard and it's too ugly to define html blocks between " " so I prefer #slot.
Component:
<div class="component">
#for($i = 1; $i <= $numberOfSlots; $i++)
<div class="item">
{{ ${"body_{$i}"} }}
</div>
#endfor
</div>
Blade:
#component(['numberOfSlots' => 3])
#slot('body_1')
Dynamically created slot 1
#endslot
#slot('body_2')
Dynamically created slot 2
#endslot
#slot('body_3')
Dynamically created slot 3
#endslot
#endcomponent
I may be wrong but couldn't you just pass the data to the component and handle that in the component? Docs show
Sometimes you may need to pass additional data to a component. For this reason, you can pass an array of data as the second argument to the #component directive. All of the data will be made available to the component template as variables:
#component('alert', ['foo' => 'bar'])
...
#endcomponent
deal with the data in the component instead of using a slot?
it's so simple and easy send array into component template without any #slot
in view page
view.blade.php
#component('components.button',[
'tooltipTitle'=>'buttorr33',
'addStyle'=>'pointer',
])
#endcomponent
in component php file component\button.php
<div class="block-buttons {{$addStyle or NULL}}"
{{--if set tooltip, add atributes--}}
#if (isset($tooltipTitle))...
How I do it:
Include #PageNav component tag in any view
#PageNav([
'menus' => [
[
'title' => 'foo',
'url' => '/foo/index'
],
[
'title' => 'bar',
'url' => '/bar/page'
]
]
]) #endPageNav
Create view/components/PageNav.php file
#foreach ($menus as $menu)
<li class="nav-item">
<a class="nav-link" href="{{ $menu['url'] }}">{{ $menu['title'] }}</a>
</li>
#endforeach
Register the component: (AppServiceProvider.php -> Boot)
Blade::component('components.PageNav'); // "/views/components/PageNav.php"
Hope it's help!
Taken from lrvl 5.1 documentation, I read:
using these lines in the controller:
$this->validate($request, [
'title' => 'required|unique:posts|max:255',
'author.name' => 'required',
'author.description' => 'required',
]);
If validation doesn't pass controller stops execution and redirect back to previous location.
This is happening correctly.
Then doc says:
"$errors variable will always be available in all of your views on every request"
And then suggests the following blade code:
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
But actually I'll get a ErrorException undefined variable errors....
What am I missing?
Thanks
I'll answer myself to this question,
just in case It was not clear the commented solution.
(Thanks to train_fox for the hint).
Just add 'web' middleware usage.
in your routing that is target in form action (get/post)
Example:
Route::group(['middleware' => 'web'],
function(){
Route::post('/edit' , 'My_Controller#edit');
});
Variable $errors the becomes available on view to be parsed.
Halo here is my need;
i want to include different view as apart of different view in laravel php frame work.
class DashboardController extends BaseController {
public function comments( $level_1=''){
// process data according to $lavel_1
return View::make('dashboard.comments', $array_of_all_comments);
}
public function replys( $level_2=''){
// process data according to $lavel_1
return View::make('dashboard.replys', $array_of_all_replys);
}
these both data can now accessed from
www.abc.com/dashboard/comments
www.abc.com/dashboard/replys
And in my view what i need is to generate replys according to the comments id ($lavel_2)
// dashboard/comments.blade.php
#extends('layout.main')
#section('content')
#foreach($array_of_all_comments as $comment)
comment {{ $comment->data }},
//here is what i need to load reply according to the current data;
//need to do something like this below
#include('dashboard.replys', $comment->lavel_2) //<--just for demo
.................
#stop
and in replys also got
#extends('layout.main')
#section('content')
// dashboard/replys.blade.php
#foreach($array_of_all_replys as $reply)
You got a reply {{ $reply->data }},
...........
#stop
is there any way i can achieve this on laravel 4?
Please help me i wanted to load both comments and replays in one go and later need to access them individually via ajax also
please help me thank you very much in advance
halo i found the solution here
all we need is to use App::make('DashboardController')->reply();
and remove all #extends and #sections from including view file
the change are like this
// dashboard/comments.blade.php
#extends('layout.main')
#section('content')
#foreach($array_of_all_comments as $comment)
comment {{ $comment->data }},
//<-- here is the hack to include them
{{-- */echo App::make('DashboardController')->reply($comment->lavel_2);/* --}}
.................
#stop
.............
and in replys is now changed to
// dashboard/replys.blade.php
#foreach($array_of_all_replys as $reply)
You got a reply {{ $reply->data }},
...........
#endforeach
-------------
thanks
You probably want to rework your views and normalise your data. Comments and replies are (probably) the same.
If you make a Comment model which belongsTo "parent" (another Comment model) and hasMany "children" (many Comment models), then simply set parent_id to 0 for a top-level Comment, and set it to the ID of another Comment to make it a Reply.
Then your Blade views do something like:
comments.blade.php
#foreach ($comments AS $comment)
#include( 'comment', [ 'comment' => $comment ] )
#endforeach
comment.blade.php
<div>
<p>{{{ $comment->message }}}</p>
#if( $comment->children->count() )
<ul>
#foreach( $comment->children AS $comment )
<li>
#include( 'comment', [ 'comment' => $comment ] )
</li>
#endforeach
</ul>
#endif
</div>
I have a simple blog with Post resource and Comment nested resource.
Until now I can see all the comments belonging to a post and create a new comment for a post.
I want to give the possibility to delete a specific comment, but somehow I am making some mistakes.
This is the view comments.index with all the comments:
#extends('master')
#section('blog')
#foreach($comments as $comment)
<div class="span11 well">
<ul>
<li><strong>Body: </strong> {{ $comment->body }} </li>
<li><strong>Author: </strong> {{ $comment->author }}</li>
</ul>
{{ Form::open(array('method' => 'DELETE', 'route' => array('posts.comments.destroy', $post_id), $comment->id)) }}
{{ Form::submit('Delete', array('class' => 'btn btn-danger')) }}
{{ Form::close() }}
</div>
#endforeach
{{ link_to_route('posts.index', 'Back to Post index') }}
This is the error i get running the index: Parameter "comments" for route "posts.comments.destroy" must match "[^/]++" ("" given) to generate a corresponding URL.
This is the Index method inside CommentsController:
public function index($post_id)
{
$comments = Post::find($post_id)->comments;
return View::make('comments.index', compact('comments'))->with('post_id', $post_id);
}
And this is the Destroy method inside CommentsController:
public function destroy($post_id, $comment_id)
{
$comment = $this->comment->find($comment_id)->delete();
return Redirect::route('posts.comments.index', $post_id);
}
Someone can tell me please where I am making the mistake?
This the routes:
Route::resource('posts', 'PostsController');
Route::resource('posts.comments', 'CommentsController');
You have put a regexp tester on your route, to check your comments parameter.
This error message says that parameter that you give to Laravel isn't good.
If your parameter is only a decimal id, use \d+ regexp instead.
Without your routes.php file - I cant be sure, but I think this might be the problem.
Change
{{ Form::open(array('method' => 'DELETE', 'route' => array('post.comments.destroy', $post_id), $comment->id)) }
to
{{ Form::open(array('method' => 'DELETE', 'route' => array('post.comments.destroy', array ($post_id, $comment->id))) }
If this does not work - please post your routes.php file.
Edit: You have defined your route as a "resource". This means your destroy route is defined with only one variable. You dont actually need the $post included, so just define this:
{{ Form::open(array('method' => 'DELETE', 'route' => array('posts.comments.destroy', $comment->id))) }}
and change your destroy method to this - there is no need for the $post to delete a $comment:
public function destroy($comment_id)
{
$comment = $this->comment->find($comment_id)->delete();
return Redirect::back();
}
How can i use something like {{#something}} and it will run a controller that checks for "something" so i can return it to translteable text?
My current blade template looks like following:
#layout("layouts.default")
#section("inner")
<h1>Velkommen til pornobiksen</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection
I mean, how can i change the "Velkommen til pornobiksen" tekst? I know i can make something like
View::make("template")->with("h1_text","Velkommen til pornobiksen");
But is there not a module/plugin to make it easier? By making like {{#h1_text}} and it will takes from my database or something?
What is the easists way to make this?
You need to use {{ $h1_text }} to put the variable into your blade template.
#layout("layouts.default")
#section("inner")
<h1>{{ $h1_text }}</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection
EDIT
I think I misunderstood you, it seems you are looking for localization
#layout("layouts.default")
#section("inner")
<h1>{{ Lang::get('messages.welcome') }}</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection
For localization , you can use helper function : trans
home.php
return [
'welcome' => 'Velkommen til pornobiksen'
];
View
#layout("layouts.default")
#section("inner")
<h1>{{trans('home.welcome')}}</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection