Laravel-Pasar más de una variable para ver


Tengo este sitio y una de sus páginas crea una lista simple de personas de la base de datos. Necesito agregar una persona específica a una variable a la que pueda acceder.

¿Cómo modifico la línea return $view->with('persons', $persons); para pasar también la variable ms ms a la vista?

    function view($view)
    {
        $ms = Person::where('name', 'Foo Bar');

        $persons = Person::order_by('list_order', 'ASC')->get();

        return $view->with('persons', $persons);
    }
Author: Antonio Carlos Ribeiro, 2013-11-21

7 answers

Simplemente pásalo como una matriz:

$data = [
    'name'  => 'Raphael',
    'age'   => 22,
    'email' => '[email protected]'
];

return View::make('user')->with($data);

O encadenarlos, como @Antonio mencionó.

 68
Author: rmobis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-23 13:24:16

Así es como lo haces:

function view($view)
{
    $ms = Person::where('name', '=', 'Foo Bar')->first();

    $persons = Person::order_by('list_order', 'ASC')->get();

    return $view->with('persons', $persons)->with('ms', $ms);
}

También puedes usar compacto():

function view($view)
{
    $ms = Person::where('name', '=', 'Foo Bar')->first();

    $persons = Person::order_by('list_order', 'ASC')->get();

    return $view->with(compact('persons', 'ms'));
}

O hacerlo en una línea:

function view($view)
{
    return $view
            ->with('ms', Person::where('name', '=', 'Foo Bar')->first())
            ->with('persons', Person::order_by('list_order', 'ASC')->get());
}

O incluso enviarlo como una matriz:

function view($view)
{
    $ms = Person::where('name', '=', 'Foo Bar')->first();

    $persons = Person::order_by('list_order', 'ASC')->get();

    return $view->with('data', ['ms' => $ms, 'persons' => $persons]));
}

Pero, en este caso, tendrías que acceder a ellos de esta manera:

{{ $data['ms'] }}
 63
Author: Antonio Carlos Ribeiro,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-11 13:43:59

Use compacto

function view($view)
{
    $ms = Person::where('name', '=', 'Foo Bar')->first();

    $persons = Person::order_by('list_order', 'ASC')->get();
    return View::make('users', compact('ms','persons'));
}
 24
Author: sumit,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-24 20:13:04

Pasando múltiples variables a una vista Laravel

//Passing variable to view using compact method    
$var1=value1;
$var2=value2;
$var3=value3;
return view('viewName', compact('var1','var2','var3'));

//Passing variable to view using with Method
return view('viewName')->with(['var1'=>value1,'var2'=>value2,'var3'=>'value3']);

//Passing variable to view using Associative Array
return view('viewName', ['var1'=>value1,'var2'=>value2,'var3'=>value3]);

Lea aquí sobre Pasar datos a Vistas en Laravel

 18
Author: Akshay Khale,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-28 15:05:38

Se encontró con un problema similar, pero si no necesariamente desea devolver una vista con archivo de vista, puede hacer esto:

return $view->with(compact('myVar1', 'myVar2', ..... , 'myLastVar'));
 3
Author: Makan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-27 03:37:02

Para pasar datos de múltiples matrices desde el controlador a la vista, inténtelo. Está funcionando. En este ejemplo, estoy pasando los detalles del asunto de una tabla y los detalles del asunto contienen el id de categoría, los detalles como nombre si el id de categoría se obtiene de otra categoría de tabla.

$category = Category::all();
$category = Category::pluck('name', 'id');
$item = Subject::find($id);
return View::make('subject.edit')->with(array('item'=>$item, 'category'=>$category));
 0
Author: Deepak Kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-27 10:06:29

Por favor intente esto,

$ms = Person::where('name', 'Foo Bar');
$persons = Person::order_by('list_order', 'ASC')->get();
return View::make('viewname')->with(compact('persons','ms'));
 0
Author: Pillai,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-21 18:29:55