2016-02-14 24 views
2

我在我的控制器(AlbumController)中有一個問題,我想在此控制器中使用我的模型(相冊)之一,但我得到了錯誤類'相冊'找不到 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(應用程序/模型/專輯)

<?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'); 
} 
} 
?> 

回答

2

您收到此錯誤命名空間mismatch.There是要解決一個是使用你的命名空間中的類的頂部

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); 
    } 
} 
+0

我得到了錯誤:Class'App \ Models \ Album'找不到 –

+0

您可能在您的模型類中使用了錯誤的命名空間。可能會命名空間App \ Models; –

+0

是的,我在我的模型的命名空間是錯的,因爲你說它應該是App \ Model,謝謝! –

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); 
    } 
} 
+0

我2路出現錯誤:找不到類'App \ Models \ Album'! –