-3

There are two tables in my database. They are foods and sub_foods. I want to get the names of the sub_foods in Foods model as a array and pass it to the create.blade.php file for make a dynamic dropdown box inside the form. This is my code,

FoodsController.php

public function create()
{
    $sub_foods = ['' => ''] + Sub_food::lists('name','id')->all();
    return view::make('foods.create',array('sub_foods'=>$sub_foods));
}

create.blade.php

 <form action="/foods" method="post">
    {{ Form::select('sub_foods', $sub_foods) }}
</form>
Laerte
  • 7,013
  • 3
  • 32
  • 50
  • Search your question's title in stackoverflow : https://stackoverflow.com/questions/35326115/laravel5-how-to-pass-array-to-view – Saeed Vaziry Sep 30 '17 at 13:15
  • see [here](https://stackoverflow.com/a/45377476/4881811) !! – Maraboc Sep 30 '17 at 13:23
  • Then I got this error. ErrorException (E_ERROR) Class 'Form' not found (View: C:\xampp\htdocs\Food\resources\views\foods\create.blade.php). How can I import Form class to the blade file. – Yasiru Randeepa Sep 30 '17 at 13:58
  • Please reffer this link https://stackoverflow.com/questions/28753767/laravel-5-class-form-not-found – Suniti Yadav Sep 30 '17 at 17:34

1 Answers1

1

Please try this to pass data from controller to view -

public function create()
{
    $sub_foods = Sub_food::all();
    return view('foods.create')->with('sub_foods',$sub_foods);
}

And reffer this link https://laravelcollective.com/docs/5.0/html for ErrorException (E_ERROR) Class 'Form' not found.

Hope this will help you.

Suniti Yadav
  • 393
  • 2
  • 8