Laravel 5: class 'Illuminate\Database\Seeder' not found - php

Problem I'm currently facing to has been posted here already, yet none of them could solve my one.
I'm talking about database seeder located under url like http://HOSTNAME/laravelfiles/database/seeds/UsersTableSeeder.php. Its content is as follows:
<?php
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
// What should be being done, it is being done here :)
}
}
Having opened this file directly (by url given above the code), following error is printed:
Fatal error: Class 'Illuminate\Database\Seeder' not found in /var/www/laravelfiles/database/seeds/UsersTableSeeder.php on line 6
I found possible solution. Doesn't work for me:
composer dump-autoload
<?php
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
}
NOTE: Even the code above produces the same error.

You must run seeder class via command line, not browser.
you should open your command line and change directory to laravel root folder.
then you need to run command:
php artisan db:seed
and check your database.
for more info please check laravel documentation(Laravel Database Seeding)

Related

Fatal error: Class 'TestCase' not found in laravel 9

I updated my project from laravel 8 to laravel 9 then I tried to make a test case generated by artisan.
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class SimpleTest extends TestCase
{
/**
* A basic feature test example.
*
* #return void
*/
public function test_example()
{
$response = $this->get('/');
$response->assertStatus(200);
}
}
But when I run phpunit I always get this error
PHP Fatal error: Uncaught Error: Class "Tests\TestCase" not found in path/to/SimpleTest.php:9
How can I fix this?
I created fresh Laravel 9 app to investigate this case.
Apparently the content of TestCase.php in my app was different from the one in fresh app. So I replace TestCase.php file with the one from fresh app and it works now.

Class Sheets not found

Hey guys I have integrated Google sheets using revolution/laravel-google-sheets following this blog https://drivemarketing.ca/en/blog/connecting-laravel-to-a-google-sheet/
Everything works well on my development environment so I have pushed my code to my staging server to test. After pushing my code to the staging server, I ran the following commands composer update, php artisan config:cache and composer dump-autoload in that order. When I test the flow, I get an exception error
ERROR: Class 'Sheets' not found {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0):
Below is a snippet of the controller code
<?php
namespace App\Http\Controllers\API;
use Sheets;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class MainController extends Controller
{
/**
* Write user details to google sheet
*
* #param $user
*/
public function writeUserToSheets($user)
{
Sheets::spreadsheet('google sheet id')->sheet('users')
->append([
[$user->id, $user->email, $user->first_name, $user->last_name, $user->created_at->toDateTimeString()]
]);
}
}
Am I missing anything? How do I get my code to work on my staging server?

Laravel Custom Controller getting 404 error

I have created the new controller using command line on laravel
php artisan make:controller PhotoController --resource
just display the string on a function of PhotoController like below
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PhotoController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function getName()
{
echo 'Dev';
}
Called the controller on route web.php
Route::get('photos', 'PhotoController#getName');
When i trying to load the url of controller
http://localhost/dev_laravel/public/photos
getting 404 Not found error
I also tried
http://localhost/dev_laravel/public/photo/
http://localhost/dev_laravel/public/photo/getname/
I'm beginner or learner for laravel and working on 5.8 version
php artisan route:list
after
Can anyone help me?
Open Terminal
and Run This Command
php artisan serve
Open Second Terminal and run this command
php artisan route:list
And then Your url pattern is like
http://localhost:8000/photos
or
http://127.0.0.1:8000/photos
php artisan serve
it gives a link like http://127.0.0.1:8000/ or your desire port if you set a port for it then just add photos like
http://127.0.0.1:8000/photos

Yii2 call the console command not from the project folder

Hello. I created the console command in Yii2:
projectDir/commands/SomeController.php
<?php
namespace app\commands;
use yii\console\Controller;
/**
* Class SomeController
* #package app\commands
*/
class SomeController extends Controller
{
public function actionTest()
{
//do something
}
}
I want to call this command in cron,and for testing I try to call it from the console, when I'm in the project folder:
php /var/www/projectDir/yii some/test
Everything works fine. But, if I call this command when I'm in a different directory, I get some errors.
First, i got
ReflectionException: Class app\admin\templates\Generator does not exist in /var/www/projectDir/vendor/yiisoft/yii2/di/Container.php:428
Seeing this, I commented configuration of gii in the file projectDir/common/config/config-console.php
After that I get an error:
Unknown command: some/test
Why is this happening?
I call the command with an absolute path, and it works differently when called from different folders!
You need to use magic constant __DIR__ to build absolute paths. Result of realpath('../../') will depend on path where you run command. You should use
$config['basePath'] = realpath(__DIR__ . '/../../')
or (probably better):
$config['basePath'] = dirname(dirname(__DIR__))

Laravel programatically calling artisan command from web.php in production

I have below route:
Route::get('/beneficiaries/seed', function () {
echo "<p>Database seeding started...</p>";
$exitCode = Artisan::call('db:seed');
echo "<p>Database seeding completed.</p>";
});
In my local environment, when I visit '/beneficiaries/seed', it seeds the database. But if I do the same in production, it doesn't. I just copied the seeder classes and route file.
DatabaseSeeder:
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call(BeneficiariesTableSeeder::class);
}
}
BeneficiariesTableSeeder:
class BeneficiariesTableSeeder extends Seeder
{
public function run()
{
//seeding logic...
}
}
Why my production Artisan command doesn't get executed? (I haven't used database transaction. Even w/o it, local db gets seeded since there is no err is raised.)
When you run php artisan db:seed in production, there is a warning that asks you whether you're sure to seed the database in production.
This warning confirmation in production is the reason why Artisan::call('db:seed') isn't working in production.
To circumvent the warning, you can use the --force flag like so: php artisan db:seed --force.
Solution
To do the same in code, use Artisan::call('db:seed', ['--force' => true]);

Categories