gwapo
gwapo

Reputation: 208

Class 'Model name' not found in Laravel

This is my controller but so that I can download excel. The locations of the controller and the model are both the same I already checked it but still there's an error and it says that "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();
    }
}

Then this is how my Model is.

<?php

namespace Vanguard\App;

use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
    public $fillable = ['title','description'];
}

Upvotes: 0

Views: 811

Answers (3)

Feridun
Feridun

Reputation: 145

issue: Error Class 'App\Model\ModelCustomer' not found

solved: i solved this issue by adding s to Model become Models.

i was using "use App\Models\ModelCustomer"

Upvotes: 0

Maru Amallo
Maru Amallo

Reputation: 430

You are calling App\Item class and your class is Vanguard\Item
1. use Vanguard\Item Model

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. Modify the Model namespace.

namespace Vanguard;

use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
    public $fillable = ['title','description'];
}

3. and run composer dump-autoload

Upvotes: 3

patricus
patricus

Reputation: 62228

Your model's FQCN is Vanguard\App\Item, not App\Item.

Upvotes: 3

Related Questions