Reputation: 31
I pretty new to Laravel 7, I have been learning the framework at the same time trying to create a system. I am stuck on Undefined variable error were create a form called create inside equipment folder it has several fields. I want to pull data from the db table called equipcat and display it on select tag. If use {{form}} method it works but the below method fails, i prefer to use the below method instead of {{form}}
create.blade.php
<select class="form-control input-sm" id="category_name" name="category_name">
<option value="">--Choose Category--</option>
@foreach ($categories as $row)
<option>{{$row->name}}</option>
@endforeach
</select>
CategoryController:
$categories = DB::table('equipcat')->pluck("name","cat_id");
return view('equipment.create',compact('categories'));
Web.app
Route::get('equipment.create', 'CategoryController@getcategory');
Upvotes: 1
Views: 576
Reputation: 432
The best way is use model for querying. Change your CategoryController to:
$categories = Category::select("name","cat_id")->get();
return view('equipment.create',compact('categories'));
I hope you have model of Category.
Upvotes: 0
Reputation: 31
I found a solution. In my create.blade.php I added this line @php($categories=\App\Category::all()) to declared the variable and it worked. my create.blade.php looks like this:
@php($categories=\App\Category::all())
@foreache($categories as $row)
<option value="">{{ $row->name }}</option>
Upvotes: 2
Reputation: 290
you can use "select" instead of "pluck"
$data = DB::table('equipcat')->select('name','cat_id')->get();
try this hope it help
Upvotes: 1