这是我的控制器,但我可以下载excel。控制器和模型的位置都是相同的,我已经检查了它但仍然有一个错误,它说“Class”App \ Item“Not Found”。
namespace Vanguard\Http\Controllers\Web;
use Input;
use App\Item;
use DB;
use Excel;
use Illuminate\Http\Request;
use Vanguard\Http\Controllers\Controller;
use Cache;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class MaatwebsiteDemoController extends Controller
{
public function importExport()
{
return view('importExport');
}
public function downloadExcel()
{
$data = Item::get()->toArray();
return Excel::create('itsolutionstuff_example', function($excel) use ($data) {
$excel->sheet('mySheet', function($sheet) use ($data)
{
$sheet->fromArray($data);
});
})->download($type);
}
public function importExcel()
{
if(Input::hasFile('import_file')){
$path = Input::file('import_file')->getRealPath();
$data = Excel::load($path, function($reader){
})->get();
if(!empty($insert)){
foreach ($data as $key => $value) {
$insert[] = ['title' => $value->title, 'description' => $value->description];
}
if(!empty($insert)){
DB::table('items')->insert($insert);
dd('Insert Record succesfully');
}
}
}
return back();
}
}
然后这就是我的模型。
<?php
namespace Vanguard\App;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
public $fillable = ['title','description'];
}
答案 0 :(得分:3)
您模型的FQCN为Vanguard\App\Item
,而不是App\Item
。
答案 1 :(得分:3)
您正在致电App\Item
课程,而您的课程为Vanguard\Item
的 1。使用Vanguard\Item
模型
use Input;
// use App\Item; // REMOVE that line
use Vanguard\Item; // ADD this line
use DB;
use Excel;
use Illuminate\Http\Request;
use Vanguard\Http\Controllers\Controller;
use Cache;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class MaatwebsiteDemoController extends Controller
{
...
}
<强> 2。修改模型命名空间。
namespace Vanguard;
use Illuminate\Database\Eloquent\Model;
class Item extends Model
{
public $fillable = ['title','description'];
}
第3。并运行composer dump-autoload