Zakaria Mountassir
Zakaria Mountassir

Reputation: 102

i Can't pass three arrays to laravel view

I tried to pass three arrays to a view in laravel but I got this problem

Undefined variable: demmande (View: C:\wamp\www\project\resources\views\demmande\demmandes.blade.php)

i change the order of the arrays i can't pass the third array here is my function in the controller

public function ViewDemmandes(){
  $listdemmande=Demmande::all();
  $listvillee=Ville::all();
  $listcategorie=Categorie::all();

  $villes = array('villes' =>   $listvillee, );
  $demmande = array('demmande' => $listdemmande, );
  $categorie = array('categorie' => $listcategorie, );

  return view("demmande.demmandes",$villes,$categorie,$demmande);
}

Upvotes: 1

Views: 839

Answers (6)

Alihossein shahabi
Alihossein shahabi

Reputation: 4352

A simple example

View the controller in app/Http/Controllers/SampleController.php

 /**
 * @return View
 */
public function index()
{
    $movieList = [
        'Shawshank redemption',
        'Forrest Gump',
        'The Matrix',
        'Pirates of the Carribean',
        'Back to the future',
    ];

    return view('welcome', compact('movieList'));
}

and for example, See latest movie views in resources/views/welcome.blade.php

@section('content')
    <h1>Latest Movies</h1>
    <ul>
    @foreach($movieList as $movie)
        <li class="list-group-item"><h5>{{ $movie }}</h5></li>
    @endforeach
    </ul>
@endsection

Upvotes: 0

William Perron
William Perron

Reputation: 1398

According to the official documentation you can either pass an array as second parameter, as opposed to the list of all the parameters.

return view("demmande.demmandes", [
    'villes' => $villes,
    'categorie' => $categorie,
    'demande' => $demande
]);

or chain the with method to add more parameters (see the Github page).

return view("demmande.demmandes")
    ->with('villes', $villes)
    ->with('categorie', $categorie)
    ->with('demande', $demande);

Upvotes: 1

Chirag Patel
Chirag Patel

Reputation: 1642

You can use compact() method.

Try this line to return your data.

return view("demmande.demmandes",compact('villes','categorie','demmande'));

Just replace your code with this,

public function ViewDemmandes(){
  $listdemmande=Demmande::all();
  $listvillee=Ville::all();
  $listcategorie=Categorie::all();

  $villes = $listvillee;
  $demmande = $listdemmande;
  $categorie = $listcategorie;

  return view("demmande.demmandes",compact('villes','categorie','demmande'));
}

And you can retrieve those variables by

@foreach ($demmande as $data)
    {{$data->property}} //your property to define
@endforeach

Hope this will work.

Upvotes: 2

jvk
jvk

Reputation: 2201

public function ViewDemmandes(){
  $listdemmande=Demmande::all();
  $listvillee=Ville::all();
  $listcategorie=Categorie::all();

  $villes = array('villes' =>   $listvillee, );
  $demmande = array('demmande' => $listdemmande, );
  $categorie = array('categorie' => $listcategorie, );

  return view("demmande.demmandes",compact('villes','categorie','demmande');

Upvotes: 0

AndyChern
AndyChern

Reputation: 336

The view function will only accept a single array, however, you can nest your arrays within it like this -- and still access them by key from within the view.

return view("demmande.demmandes",['villes'=>$villes, 'categorie'=>$categorie, 'demmande'=>$demmande]);

Upvotes: 0

david
david

Reputation: 633

From reading the Laravel Views documentation I think that the view() method expects you to specify the template parameters using one array. You can combine your three arrays into one:

public function ViewDemmandes(){
  $listdemmande=Demmande::all();
  $listvillee=Ville::all();
  $listcategorie=Categorie::all();

  $data = array(
    'villes' => $listvillee,
    'demmande' => $listdemmande,
    'categorie' => $listcategorie, 
  );

  return view("demmande.demmandes", $data);
}

Upvotes: 1

Related Questions