2
votes

I have a problem in my controller(AlbumController) , I want to use one of my model(Album) in this controller but I got the error Class 'Album' not found 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);
}
}

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

2 Answers

2
votes

you are getting this error for namespace mismatch.There is 2 way to solve one is use your namespace in top of class

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

another is use full namespace in method

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);
  }
}
2
votes

You not use use Album; in yor controller
Try this

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