0

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'];
}

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

11 Comments

@gwapo, I would suggest not changing the default namespace structure unless you specifically have a reason to. Normally, Models are under the App namespace.
I changed it to that one then it says Class "Vanguard\App\Item" Not found
@gwapo Is your PSR4 namespacing setup correctly? What does your "autoload" section in composer.json look like? What does your directory structure look like?
"psr-4": { "Vanguard\\": "app/" }, Thats how mmy autoload in composer.json @patricus
try composer dump-autoload
|
3

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

4 Comments

you have the problem into the Model Namespace, I just read the comment in the other answer and your autoload is Vanguard not Vanguard\App
How to run composer dump-autoload? @Maru Amallo
yes, like you run composer install, composer update or composer require . You need to run composer dump-autoload or composer dumpautoload
Or composer du. :)
0

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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.