How to pass an array to a #slot in laravel 5.4? - php

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!

Related

how to pass multiple values to routes from controller in laravel

route
Route::get('/dashboard/view-sub-project/{pid}/{sid}', 'SubProjectController#view')->name('sub-project.view')->middleware('auth');
View
View
Values of var
request()->route()->parameters['id'] is 2
$update->id is 1
I have defined router correctly on web.php and view but still, it throws an error
Missing required parameters for [Route: sub-project.view] [URI:
dashboard/view-sub-project/{pid}/{sid}]. (View:
/var/www/html/groot-server/resources/views/project/view.blade.php)
I have tried to change my router like this also
Route::get('/dashboard/view-sub-project/{pid}{sid}', 'SubProjectController#view')->name('sub-project.view')->middleware('auth');
Still got the same error.
Try adding parameters in array.
Route::get('/dashboard/view-sub-project/{pid}/{sid}','SubProjectController#view')
->name('sub-project.view')
->middleware('auth');
<a href="{{ route('sub-project.view',
[
'pid' => request()->route()->parameters['id'],
'sid' => $update->id
]
) }}" class="btn btn-primary project-view">
View
</a>
Hope this helps.
On your view, since you're using the route function to build the url you can do the following.
<a href="{{ route('sub-project.view', [
'pid' => request()->route()->parameters['id'],
'sid' => '$update->id'
]) }}" class="btn btn-primary project-view">View</a>
You can also view it in the Laravel Helper Function.
If you only have one parameter in the route you can just pass the value. Let's say you had a route that only took a post ID, Route::get('/posts/{post}/edit')->name(edit). On your view you can then do {{ route('edit', $post->id) }}.
When you have multiple values being passed to the route url as you have in your case you pass an array of item with the key being the same as the route parameter.
Let's say you have another route Route::get('/posts/{post}/comments/{comment}')->name(post.comment). On your view you can do {{ route('post.comment', ['post' => $post->id, 'commment' => $comment->id]) }}.

Laravel linking using controller

see this apple icons, these are rendering from foreach loop in view, defined variable in controller . i can change each icons but not able to assign link to each icon.
Hello guys i am stuck in linking using laravel controller.
my code is something like that: (i have a service page which has sub services page there is only one view which is using controller variable to showing data. i want each page button have different links. links can be static so i can putt it from controller. dont have any database attached to this solution. i want each href as a separate link)
This is view:
<?php $index=-1; ?>
#foreach ($third_fold_3_technologies_list as $item)
<?php $index++; ?>
<span class="{{ $item }}"></span>
#endforeach
This is controller:
$third_fold_3_technologies_list = [
'icon-appleinc',
'icon-appleinc',
'icon-appleinc',
];
You can use associative array.
$third_fold_3_technologies_list = [
'http://<link1>' => 'icon-appleinc',
'/<link2>' => 'icon-appleinc',
'<link3>' => 'icon-appleinc',
];
Then in your view
#foreach ($third_fold_3_technologies_list as $link => $class)
<span class="{{ $class }}"></span>
#endforeach

How to build path to route in Laravel with parameter?

I have this route
Route::get('org/edit/{id}/', ['as' => 'org.edit', 'uses' => 'OrgController#edit']);
And then create a link to this route by using Laravel Blade template:
<span class="glyphicon glyphicon-edit"></span>
What I expect to see:
/org/edit/123/
What I get:
/org/edit?123
What am I doing wrong?
Try passing it as a key value pair.
{{ route('org.edit', ['id' => $org->id]) }}
Dmitriy,
in this case you can try two ways.
{{ route('org.edit', ['id' => $org->id,]) }}
{{ route('org.edit', $org->id) }}
Check this out. :)

How can I set the route parameter name using a variable in Twig?

I am trying to use the url Twig function with Silex to generate a route, but when I use the variable name that I have passed to the template it generates a warning that I have not supplied the parameter.
This is the array I am passing to the template:
[
"total_pages" => $pages,
"current_page" => $page,
"route_name" => "gallery_album",
"route_parameter" => "groupname",
"route_value" => $groupname
]
And in the template I am trying to use:
{{ url(route_name, {route_parameter: route_value, 'page': page} ) }}
(The page variable value is worked out in the template)
This is part of a pagination template that I am building so I need the parameter to be a variable so it can be applied to different pages. This is the error I get when I run this:
I feel this is something that is very simple, I am just missing something fundamental.
It thinks that route_parameter is a string key name and not a variable:
You can do for example:
{% set params = {'page': page, (route_parameter): route_value } %}
{{ url(route_name, params) }}
You can use {{ app->path}} or {{ app->url }}
if you using Silex\Application\UrlGeneratorTrait in you Application class
or alternative using this
{{ app.url_generator.generate('homepage') }}

laravel passing database array from controller to view

hello people here is my code below for controller
$siteinformation = DB::table('siteinformation')->where('Site',$myarr[0]['site'])->select()->get();
print_R($siteinformation); will display
Array ( [0] => stdClass Object ( [Site] => site1 [Nickname] => friend [ProgramID] => 1 [CreateDateTime] => 2014-06-03 18:05:39 ) )
I am trying to pass it to view
return Redirect::to('sbs_site')->with('siteinformation',$siteinformation);
In my view i have...
#if(Session::has('siteinformation'))
#foreach ($siteinformation as $key => $value)
{{ $value->Nickname }}
#endforeach
#endif
Iam gettng an error Undefined variable: siteinformation
my route file contains... Route::post('sbs_site', array('uses' => 'myController#sys_config'));
what could be the problem??
I think you don't need to redirect; probably you are doing it wrong, instead you should pass the data to the view directly using something like this:
$siteinformation = DB::table('siteinformation')
->where('Site',$myarr[0]['site'])
->get();
// View Name: "sbs_site.blade.php"
return View::make('sbs_site')->with('siteinformation', $siteinformation);
Then in the view:
#foreach ($siteinformation as $value)
{{ $value->Site }}
{{ $value->Nickname }}
#endforeach
But if your really need to use a redirect then you may do it like this:
// Route Is: "sbs_site"
return Redirect::to('sbs_site')->with('siteinformation', $siteinformation);
Then in your view:
#if(Session::has('siteinformation'))
#foreach (Session::get('siteinformation') as $value)
{{ $value->Site }}
{{ $value->Nickname}}
#endforeach
#endif
This worked for me...
in view
#if(Session::has('siteinformation'))
#foreach (Session::get('siteinformation')as $key => $value)
{{ $value->Nickname}}
#endforeach
#endif
In controller
return Redirect::to('sbs_site')->with('siteinformation',$siteinformation);
Your view file is not getting session set if(Session::has('siteinformation')), because you did not set session in your controller. You set in your controller 'siteinformation' as a normal variable. If you want to set session, instead of this.
Redirect::to('sbs_site')->with('siteinformation',$urserializedobj);
Do this
Session::put('siteinformation',$urserializedobj);
Redirect::to('sbs_site');
OR
Simply don't check session in your view instead of this
if(Session::has('siteinformation'))
Just do this
if (!empty($siteinformation)) :
I don't know why u are redirecting to pass it to the view
return View::make('yourview',array('siteinformation'=>$siteinformation));
This will do the trick if you can make the view directly from the controller
BUT
if u need to redirect than i suppose you can serialize your array or change it to json first because the with function passes the data as flash so only strings
$urserializedobj = $siteinformation.toJson();//if this doesn't work try serealizing
return Redirect::to('sbs_site')->with('siteinformation',$urserializedobj );
now in your view you can do this
#if(Session::has('siteinformation'))
$siteInformation = json_decode(Session::get('siteinformation'));
#foreach ($siteinformation as $key => $value)
{{ $value->Nickname }}
#endforeach
#endif

Categories