So I noticed something very strange when I deployed my project on a server (It works on local server) , one of my models doesn't work in "models" directory I've created, it only works when I put the model.php in \App and it has the namespace as the other models , how come?
namespace App\Http\Models;
Error: Class 'App\http\Models\Themodel' not found
If your models folder is in App\Models then the namespace should be:
namespace App\Models;
In your controller import the model in your controller like this:
use App\Models\Themodel;
Related
What I've done
I used the command php artisan make:model User without mentioning model folder as the command should have been php artisan make:model Models\User.
I am an amateur in terms of laravel and just started php and laravel last month and made a whole multi vendor project around it without the models being in models folder.
What I want
Now, I'm just trying to structure my code in a cleaner manner and facing errors as have to change the path of models wherever it is mentioned. I tried doing them but still can't get rid of the errors.
Latest version of Laravel, by default, file is already created inside the Models folder. If you have a model outside folder and what you want is to incorporate it there, the best thing to do is to change the built-in namespace at the top of everything. For example:
namespace App\Models;
If you want is to completely change the path, first create the model and then move folder manually. Finally, in namespace mention it:
namespace FolderA\FolderB\FolderC;.
Although it is always better to follow the default Laravel structure, is cleaner.
You need to modify your
Project/config/auth.php
file on
'model'=> AppName\Models\User::class,
The best thing to do is to move 1 model at a time and replace all of its import namespaces. Test your application if everything works and then continue with the next model and so on.
Example:
Move User.php from App\ to App\Models folder
Update the namespace of User.php from namespace App; to namespace App\Models;
Find and replace in your project (global search) for: use App\User.php and replace all results with use App\Models\User.php
Check your application and run tests if you have them.
If everything is ok, you can continue moving the next model to the Model/ folder and repeat step 1 to 5
For the model User.php: As #saeed mentioned, update your config/auth.php if needed.
Note that IDE's these days can do the most work for you, like PhpStorm can do all the find & replaces for you when you pressing F6 on the User.php file in the tree and update its path.
I am using Laravel 7. I use PhpStorm, and great fan of it.
I want to store all of my model files in a single folder named Models into app folder. I changed this line:
namespace App; -> namespace App/Models.
But PhpStorm is giving me an error. It doesn't recognize my folder:
Undefined constant 'Models'
How can I fix this?
Use the anti slash for namespace
namespace App\Models;
use Illuminate\Http\Resources\Json\ResourceCollection;
works and can be retrieved inside a Lumen project. But, using
use Illuminate\Http\Resources\Json\JsonResource;
will return
Class 'Illuminate\Http\Resources\Json\JsonResource' not found`
So, how can I add JsonResource inside a Lumen project?
i think you can check your autoload_classmap.php in vendor/composer folder
otherwise you simply use this use Illuminate\Http\Resources\Json\JsonResource;
I have 2 models in Laravel 5.5 under the "Models" folder. I then created a folder called "Internal" inside the Models folder and inserted my models in that folder. I updated all the namespaces and also did composer dump-autoload. But I now get an error that states that
"Class 'App\Models\Internal\LeaveApplication' not found".
Here is the Controller namespace:
use App\Models\Internal\LeaveApplication;
use App\Models\Internal\LeaveType;
Here is the model's namespace:
namespace App\Models\Internal;
use Illuminate\Database\Eloquent\Model;
I am running the Yii2 framework locally, and I want to reuse an model I created in a earlier project.
So I copy the file TestForm.php to the models directory, change the namespaces from namespace backend\models to namespace app\models and try to create an object from it with:
$model = new \app\models\TestForm;
Which gives me
Unable to find 'app\models\TestForm' in file: /var/www/html/operators/basic/models/TestForm.php. Namespace missing?
Which is weird because the namespace is correct.
However, if I create the file TestForm.php myself and copy the contents of the older file over, everything works fine.
What's going on?
(I use ubuntu 15.04)
I think in your /models/TestForm.php you don't have specified the correct namespace eg :
namespace basic\models;