2

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?

5
  • 2
    Looks like the controller can't find your view file not a db problem. I'd be checking file names and paths, imports and delving into how the controller finds view files. Commented Apr 4, 2016 at 10:06
  • 1
    No, I used it properly, all the previous controllers were working well with this view. Commented Apr 4, 2016 at 10:13
  • 1
    I don't know laravel, but that error message is pretty clear. I wouldn't be relying on assumptions from previous controllers, try replacing the entire db call with an empty array.. dollars to doughnuts you get the same error. Did you step through the code of FileViewFinder.php? Commented Apr 4, 2016 at 10:29
  • 1
    What is the full name of the view? Commented Apr 6, 2016 at 12:55
  • What's the full path of the view file? If it is in a subdirectory under /resources/views, then the view need to be called with "subdir.viewname". If this is not the case, then the possible issue is with the namespace. Commented Apr 6, 2016 at 13:51

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

my problem was getting data to a dropdown list. I solved it by using route file. thanks for your help.
2

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']) !!}

1 Comment

View was just a problem, I wanted to get all values from a database table.
2

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'
.......
)

Updated

Laravel deprecated the lists() method, and renamed to pluck. you can read details in following link, Access link and do scroll little up to the Deprecations section.

https://laravel.com/docs/5.2/upgrade#upgrade-5.1.11

2 Comments

This just get an empty array [ ], what I need is, I need these values to be in my Dropdown menu of view page.
Can you share your table structure?
-1

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:

image

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.