Ad

Uncaught Error "Class Not Found" In Laravel 5.8

- 1 answer

I am trying to call my model file from another folder. I have provided both of these file structure.

I am getting this error:

Uncaught Error: Class 'App\Models\Providers' not found in /Applications/XAMPP/xamppfiles/htdocs/pro/app/Scripts/Providers/1/Scrape.php:17

I am calling the model file from a script folder located :

app/Scripts/Providers/1/Scrape.php

In this class I have the below :

namespace App\Scripts\Providers\1;
use App\Models;

Model file is located :

app/Models/Providers.php

Within this file I have the below:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

I have not shared the full content that I have in both of these files. If you would like to see the full content of these files please let me know.

This is how the Scrape.php looks like

<?php
namespace App\Scripts\Providers\1;
use App\Models\Providers;

class Scrape {
    public function __construct() {
        $test = new \App\Models\Providers();
        die(print_r($test, true));
    }
}

$obj = new Scrape();
Ad

Answer

You can't have a namespace that starts with a number.

Namespaces follow the same basic rules for variable names:

A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores

(Emphasis mine).

Thus, your declaration

namespace App\Scripts\Providers\1

is basically invalid.

From that point forward, all bets are off.

First, change your namespace to a valid identifier (and I would advise choosing something more reasonable and recognizable than numbers, you can have descriptive names and there is simply no reason not to):

namespace App\Scripts\Providers\GroupWhatever

Logically, you'll have to rename the folder where this file resides. It used to be

app/Scripts/Providers/1/Scrape.php

so rename that directory to

app/Scripts/Providers/GroupWhatever/Scrape.php

(In both cases, replace GroupWhatever with something that makes sense for your application and domain).

From that point forward, if the class \App\Models\Providers exists at app/Models/Providers.php, it should work.

Important:

Another problem that there could exist, is that is not very clear what Scripts/Scrape.php is or how is it called.

This should work if you are executing Scrape.php from within Laravel, by calling a Laravel controller or console application.

If you are calling this script directly (e.g. by executing php app/Scripts/Providers/1/Scrape.php (or the corrected app/Scripts/Providers/GroupWhatever/Scrape.php) this simply won't work, since the autoloading logic is not run at all.

If you are executing your script manually, on top of the above changes you need to include composer autoload script, which is located at vendor/autoload.php.

Basically, add this line close to the top of your Scrape.php:

require_once dirname( __DIR__ ) . '/../../../vendor/autoload.php';

(I think I put the appropriate amount of go-up-one-dir path-segments, but you make sure it matches the correct path in your installation).

Once that is in place, the autoloader will be run, and classes will be found.

Ad
source: stackoverflow.com
Ad