This is my Controller:
public function create(){ $categories =DB::select('select Code from ItemCategory'); return view('item')->with('ItemCategory', $categories); My View file:
<div class="form-group"> {!! Form::label('Link Category') !!}<br /> {!! Form::select('categories', (['0' => 'Select a Category'] + $categories), null, ['class' => 'form-control']) !!} </div> But when I run this , I get following error
InvalidArgumentException in FileViewFinder.php line 137: View [item] not found.
PS:
My database table:
+----------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------------+-------------+------+-----+---------+-------+ | ID | int(11) | NO | PRI | NULL | | | Code | varchar(45) | NO | | NULL | | | ItemCategotyID | int(11) | NO | | NULL | | | ItemLevelID | int(11) | NO | | NULL | | | isActive | varchar(45) | YES | | NULL | | +----------------+-------------+------+-----+---------+-------+ I have tried this example. What is the mistake here? How to get values for a drop down using MySQL table?
4 Answers
Answers 1
in your create method, do this suppose ItemCategory is your model
$categories = \ItemCategory::lists('name', 'id'); intead of this line
$categories =DB::select('select Code from ItemCategory'); it will give you this type output
array( 1=>'Cat 1', 2=>'Cat 2', 3=>'Cat 3' ....... ) Answers 2
This does not seem to be an issue with what you are retrieving from the DB but rather with your view that you referenced
return view('item')->... the view 'item' might not be where you think it is. referenced from laravel docs:
Of course, views may also be nested within sub-directories of the resources/views directory. "Dot" notation may be used to reference nested views. For example, if your view is stored at resources/views/admin/profile.php, you may reference it like so:
return view('admin.profile', $data); On a side note regarding your DB query, you can try to use collections to make your life easier when dealing with views.
Answers 3
In fact you don't need to use "raw html". You can use
{!! Form::select('categories', $categories->pluck('Code', 'ID')->all(), null, ['class' => 'form-control']) !!} Answers 4
To load values to drop-down, you have to use route.php.
Route::get('additem',function(){ $categories = ItemCategory::all(); return view('***/***.add_item')->with('categories',$categories); }); You must include your models in route.php
use App\ItemCategory; Then in your view file, use raw html.
<div class="form-group"> {!! Form::label('Category', 'Category:') !!} <select class="form-control input-sm" name=""> @foreach($categories as $cats) <option value="{{$cats->ID}}">{{$cats->Code}}</option> @endforeach </select> </div> This is how it looks after dropdown list gets its values from the database:
0 comments:
Post a Comment