我的控制器(AlbumController)有问题,我想在这个控制器中使用我的一个模型(Album),但是我找不到错误Class'Album' AlbumController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
class AlbumController extends BaseController
{
public function getList()
{
$albums = \Album::find(1);
return View::make('index')
->with('albums' , $albums);
}
}
型号:Album.php(App / Models / Album)
<?php
namespace App\Models\Album;
class Album extends Model
{
protected $table = 'albums' ;
protected $fillable = ['name' , 'description' , 'cover_image'] ;
public function Photos()
{
return $this->hasMany('images');
}
}
?>
答案 0 :(得分:2)
您不在控制器中使用use Album;
试试这个
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use App\Album;
class AlbumController extends BaseController
{
public function getList()
{
$albums = Album::find(1);
return View::make('index')
->with('albums' , $albums);
}
}
答案 1 :(得分:2)
你收到命名空间不匹配的错误。有两种方法可以解决问题,就是在类顶层使用你的命名空间
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use App\Models\Album;
class AlbumController extends BaseController
{
public function getList()
{
$albums = Album::find(1);
return View::make('index')
->with('albums' , $albums);
}
}
另一个是在方法
中使用完整命名空间namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
class AlbumController extends BaseController
{
public function getList()
{
$albums = \App\Models\Album::find(1);
return View::make('index')
->with('albums' , $albums);
}
}