Laravel 5.3, Class 'App\Models\User' not found - laravel

I have laravel app.
It works locally, using php artisan serve command.
After deployment to remote server it does not work.
Every artisan command returns:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Class 'App\Models\User' not found
Excerpt from App\Models\User.php (case-sensitive).
<?
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable {
// code here
}
composer dump-autoload does not fix it.
.gitignore
/vendor
.env
/public/css/
/public/js/
/public/img/
/public/fonts/
/node_modules/
composer.json
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.3.*",
"caouecs/laravel-lang": "~3.0"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~5.0",
"symfony/css-selector": "3.1.*",
"symfony/dom-crawler": "3.1.*"
},
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/",
"Seeds\\": "database/seeds/"
},
"files": [
"app/Http/Helpers.php"
]
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
},
"scripts": {
"post-root-package-install": [
"php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"php artisan key:generate"
],
"post-install-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postInstall",
"php artisan optimize"
],
"post-update-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan optimize"
]
},
"config": {
"preferred-install": "dist"
}
}
Local system: arch linux with php 7.0
Remote system: debian jessie with php 7.0

Add use clause at the top of a controller where you're trying to use the model:
use App\Models\User;
Alternatively, you can use full namespace:
$user = App\Models\User::find(1);
Also, make sure User.php is in the app\Models directory and make sure you've changed model in config\auth.php file:
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Model\User::class,
],
],

Finally, I found a solution.
In php.ini set short_open_tag = On and voila - it works.

Check the generated cached files, might still be referencing the old namespace.
rm -r bootrap/cache should to the trick

I was using use App\Models\User; in my controller when I copied my models from root of app folder to Models folder inside app folder.
An additional change was required which was using namespace App\Models; Inside my models so now my User model looks like:
<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
class User extends Model implements AuthenticatableContract, AuthorizableContract

Related

Add custom package to composer.json file

I have a custom package that is not uploaded on github or packagist and I need to add it to a Laravel 5.1 project.
This is my package folder structure:
Packages
\_ christian
\_ smsservice
\_src
\_ Facades
\_ MySMS.php
\_ SMSServiceServiceProvider.php
\_ vendor
\_ composer
\_ autoload.php
I have edited my root composer.json to add the package:
"psr-4": {
"App\\": "app/",
"Christian\\SMSService\\": "app/Packages/christian/smsservice/src/"
},
Then I have added the service provider and the facade to the app.php file but when I try to use the package I get:
FatalErrorException in ProviderRepository.php line 146:
Class 'Christian\SMSService\SMSServiceServiceProvider' not found
But the ServiceProvider exists and the namespace is correct:
namespace Christian\SMSService;
use Illuminate\Support\ServiceProvider;
use Illuminate\Routing\Router;
class SMSServiceServiceProvider extends ServiceProvider {
//Code
}
I needed that functionality too. I'm using the following code for one of my local Laravel projects:
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"repositories": [
{
"type": "path",
"url": "../../GitHub/laravel-page-visits-counter"
}
],
"require": {
"php": ">=5.6.4",
"laravel/framework": "5.4.*",
"laravel/tinker": "~1.0",
"cyrildewit/laravel-page-visits-counter": "dev-master"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~5.7"
},
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"scripts": {
"post-root-package-install": [
"php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"php artisan key:generate"
],
"post-install-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postInstall",
"php artisan optimize"
],
"post-update-cmd": [
"Illuminate\\Foundation\\ComposerScripts::postUpdate",
"php artisan optimize"
]
},
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
}
}

Laravel 4 - Class 'Stripe' not found

I'm trying to install Stripe into my project but I'm having issues. I'm getting the error
Class 'Stripe' not found
I used
composer require stripe/stripe-php
and everything installed fine, but I'm still having issues.
This is the line that is causing the problem:
Stripe::setApiKey(Config::get('stripe.secret_key'));
This is my composer.json file:
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"laravel/framework": "4.2.*",
"intervention/image": "dev-master",
"moltin/laravel-cart": "dev-master",
"stripe/stripe-php": "~2.0#dev"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/libs",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "dev",
"prefer-stable": true
}
There is nothing about Stripe in my app/config/app.php file... I'm not sure if there should be a service provider for Stripe.
Looking at packagist, you can see the composer package stripe/stripe-php point to the following GitHub repository. Based on that repository's code and the Stripe API documentation, it doesn't look like there's a global class named Stripe defined. So when you say
Stripe::setApiKey(Config::get('stripe.secret_key'));
and PHP says
Class 'Stripe' not found
PHP is telling you the truth. There is a class named Stripe\Stripe -- so you probably want to do this
\Stripe\Stripe::setApiKey(Config::get('stripe.secret_key'));
or import the class into your current PHP file with the use statement
<?php
//...
use Stripe\Stripe;
//...
Stripe::setApiKey(Config::get('stripe.secret_key'));

Laravel cannot find self defined service providers

I'm using Laravel for my project, I created a service provider and it's located in app/services/ToolboxServiceProvider.php, and I added
'providers' => array(
....
'services\ToolboxServiceProvider',
);
in my app.php config file. Now when loading the app, it says the service provider cannot find, I know that something's wrong with my path setting in that providers array, question is: how to make it right? Thanks in advance.
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"laravel/framework": "4.2.*"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "stable"
}
You should add into autoload => classmap section of your composer.json:
"app/services",
so it should look like:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/services",
]
},
In your provider file, at the beginning you should have:
<?php namespace services;
(in lower case).
And after those changes you should run:
composer dump-autoload
to rebuild classmap
Create the service provider using something like this in app/services folder, notice the namespace:
<?php
namespace Services;
use Illuminate\Support\ServiceProvider;
class ToolboxServiceProvider extends ServiceProvider
{
//...
}
Then in the providers array add 'Services\ToolboxServiceProvider'. Then add "app/services" in the classmap section in composer.json file and dump the autoloader.
What namespace is ToolboxServiceProvider using? Have you added autoloading in the composer.json file? Have you tried a composer dump-autoload?

Classes don't seem to load while running tests

I'm new to Laravel and PhpUnit and I'm trying to run some tests located in the \App\Tests\Unit folder on customs classes located in the \App\Musibits directory.
I get the following when I run phpunit in the \App\Tests\Unit directory:
Fatal error: Class 'Tonality' not found in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\musibits\app\tests\TonalityTest.php on line 8
Tonality.php contains my class and is in the \App\Musibits directory
I read numerous posts about autoloading and bootstrap, but I can't seem to make it work :-(
Here is my composer.json
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"laravel/framework": "4.2.*"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/musibits",
"app/tests"
]
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "stable"
}
There is three autoload files included with Laravel, one for composer, one for phpunit and one for Laravel, I didn't change any.
Any cues would be greatly appreciated.
Thanks,
Phil
You should probably run
composer dump-autoload
to generate new class map

Laravel 4 Using HybridAuth: Not Getting Autoloaded in class map

After downloading hybridauth from composer, I always need to manually add the hybridauth's directory in /vendor/composer/autoload_classmap.php, this should be automatic.
Below is my composer.json, can someone point me the problem why hybridauth is not getting written in autoload automatically?
{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"require": {
"laravel/framework": "4.0.*",
"way/generators": "dev-master",
"hybridauth/hybridauth": "*",
"intervention/image": "dev-master"
},
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
},
"scripts": {
"post-install-cmd": [
"php artisan optimize"
],
"pre-update-cmd": [
"php artisan clear-compiled"
],
"post-update-cmd": [
"php artisan optimize"
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
},
"minimum-stability": "dev"
}
Have you tried
using "hybridauth/hybridauth": "dev-master"
also try
artisan dump-autoload command
Looks like in the latest update they have changed lot of things and your old code won't work with it.
If you are not getting autoload classes in classmap file then better use older version of HybridAuth i.e. 2.9.1
use "hybridauth/hybridauth": "2.9.1" instead of "hybridauth/hybridauth": "dev-master"
Classes will be loaded automatically on autoload classmap file.
This works well on laravel 4 and 5 both.

Resources