2016-03-03 94 views
0

我想在laravel 5.2应用程序中创建一个下拉列表。我想在我的视图页面中加载类别项目。但是当我加载页面时,它显示以下错误。Laravel 5.2:类别类别未找到

FatalErrorException在routes.php文件第47行:类“分类”未找到

如果有谁知道出了什么问题,恳求帮助我完成它。

这里是我的类别型号:

<?php 

namespace App; 
use Illuminate\Database\Eloquent\Model; 

class Category extends Model{ 
    protected $table="categories"; 

    protected $fillable = ['name']; 
} 

这里是路线:

<?php 

Route::get('/', function() { 

    $categories=Category::all(); 
    return view('index')->with ('categories',$categories); 

}); 

这里是如果它需要查看页面:您还没有命名空间

<html> 
    <head> 
     <title>Cascading Dropwon</title> 
     <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
    </head> 
    <body> 
     <div class="container"> 
      <h3>Categories and Subcategories Ajax</h3> 
      <div class="col-lg-4"> 
       {!! Form::open(array('url' => '','files'=>true)) !!} 
       {!! Form::token(); !!} 
        <div class="form-group"> 
         <label for="">Categories</label> 
         <select class="form-control input-sm" name=""> 
         @foreach($categories as $category){ 
         <option value="{{$category->id}}">{{$category->name}}</option> 
         } 
         @endforeach 
         </select> 
        </div> 

        <div class="form-group"> 
         <label for="">Sub Categories</label> 
         <select class="form-control input-sm" name=""> 
         <option value=""></option> 
         </select> 
        </div> 


       {!!Form::close()!!} 
      </div> 
     </div> 
    </body> 

</html> 
+0

您试过'composer dump-autoload'吗? – Ymartin

回答

6

您的路线中的模型(类别)。 更改为

Route::get('/', function() { 

    $categories=\App\Category::all(); 
    return view('index')->with ('categories',$categories); 

}); 
+0

谢谢..它的工作。但是,你能告诉我为什么我们需要添加\ App \ Category吗? – Hola

+0

由于类别位于应用程序空间(应用程序\类别)中,并且您从App \ Http \ Controllers命名空间中不存在的引用它。 – oseintow

+1

你能接受答案吗? – oseintow