Laravel Blade: pass array as parameter to yield section
Asked Answered
C

2

12

i would like to pass an array as parameter from my controller to the blade template.

My controller looks like this:

$myArray = array('data' => 'data');
return View::make('myTableIndex')
    ->nest('myTable', 'my_table_template', $myArray)

In my blade template i've got a yield like this:

@yield('myTable', $myArray)

But i've get the error:

Error: Array to string conversion

That's because the yield function only accepts strings, right?

Background is: I want a table template which i can use dynamically for multiple purpose or multiple data, so i can use the same template for multiple tables and just pass the columns and content as array.

How can i pass an array to my yield section?

Canvasback answered 17/2, 2016 at 14:12 Comment(8)
Why not use a section instead?Liquefy
Yes, that's it use a section in a separate file and include it.Liquefy
But with include i just can use 1 variable. The one i give in the controller. So it's not dynamic anymore because in my included template i have to use the specific variable.Canvasback
You can pass a name for your variable: @include('viewname', 'variablename') so you can use variable name like $variablename in your included view.Liquefy
Interesting. But where is my variable(array) than? @include('viewname', 'variablename', $array) doesn't work.Canvasback
It should be @include('view.name', ['varname' => $array]).Liquefy
It works!! Thanks alot, very nice. Thank you :) Would you like to write an answer?Canvasback
WC, Probably I should answer it for future vieewers.Liquefy
L
14

You may use a separate file and include the file using @include while you may pass the data with a dynamic variable name so you'll be able to use that variable name in your included view, for example:

@include('view.name', ['variableName' => $array])

So, in the view.name view you can use/access the $array using $variableName variable and you are free to use any name for variableName.

So, in the separate view i.e: view.name, you may use a section and do whatever you want to do with $variableName.


Note: The problem was solved in the comment section but added as an answer here for the future reference so any viewer come here for similar problem will get the answer easily.

Liquefy answered 17/2, 2016 at 14:42 Comment(1)
This is not an answer.Metathesis
R
5

@yield produce a section, like an include but with the included template defined from the children

@yield does not accept parameters, and it makes sense since the parent does not have to knows what children will implement.

If you want to pass parameters to the template, an @include probably suits better the case

@include('subview', ['parameter_name' => 'parameter_value'])

Otherwise a simple php variable declared by the parent and used by the children can work with a @yield (and also with @include)

@php
$my_var='my_value'
@endphp
@yield('my_section')

If you want to send data from children to the parent, you can do it at the parent declaration, where you add it:

@extends('parent_view',['parameter_name'=>'parameter_value'])
Rioux answered 28/12, 2020 at 17:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.