Laravel :"php artisan db:seed" doesn't work - php

I am try to run "ServiceTableSeeder" table in database i got an error msg.
I try run "php artisan db:seed"
Msg:
[symfony\component|Debug\Exception\FetalErrorException]
cannot redeclare DatabaseSeeder::run()
DatabaseSeeder .php
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Eloquent::unguard();
$this->call('ServiceTableSeeder');
}
}
ServiceTableSeeder.php
<?php
class ServiceTableSeeder extends Seeder {
public function run()
{
Service::create(
array(
'title' => 'Web development',
'description' => 'PHP, MySQL, Javascript and more.'
)
);
Service::create(
array(
'title' => 'SEO',
'description' => 'Get on first page of search engines with our help.'
)
);
}
}
how to fix this issue .i am new in laravel anyone please guide me.

For those who are facing the same issue, confirm your APP_ENV variable from .env file cause Laravel don't let us to run db:seed if we set
'APP_ENV = Production'
for the sake of database records.
So, make sure you set value of APP_ENV to 'staging' or 'local' and then run php artisan db:seed

Considering that Service is a model you created, and that this model is inside the app folder, within the App namespace, try this:
Fix your ServiceTableSeeder.php header:
<?php
use Illuminate\Database\Seeder;
use App\Service;
class ServiceTableSeeder extends Seeder {
public function run()
{
Service::create(
array(
'title' => 'Web development',
'description' => 'PHP, MySQL, Javascript and more.'
)
);
Service::create(
array(
'title' => 'SEO',
'description' => 'Get on first page of search engines with our help.'
)
);
}
}
As you have moved your models to app\models, you must declare that in each model file:
Models.php:
namespace App\Models;
And in your seed file, use:
use App\Models\Service.php;
Are you using composer to autoload your files? If so, update your composer.json file to include your models location:
"autoload": {
"classmap": [
"database",
"app/Models"
],
"psr-4": {
"App\\": "app/"
}
},
And finally, run this in your command line:
composer dump-autoload

I think the problem is your ServiceTableSeeder.php file. You should make sure the class filename in this file is ServiceTableSeeder and not DatabaseSeeder

Related

Target class [DataTypesTableSeederCustom] does not exist

i'm using laravel6, and voyager, i'm creating artisan command for installation use cmd following php artisan make: command EcommerceInstall
, I recopy DataTypesTableSeeder from database/seeder and I rename it by DataTypesTableSeederCustom, I modify my file which I wrote EcommerceInstall.php but it gives me error class [DataTypesTableSeederCustom] does not exist.
i think i forget import the class but i don't know where .
EcommerceInstall.php
public function handle()
{
if ($this->confirm('this well delete all you current data and install the dummy default data, Are you sure ?')) {
File::deleteDirectory(public_path('storage/products/dummy'));
$this->callSilent('storage:link');
$copySuccess = File::copyDirectory(public_path('img/products'),public_path('storage/products/dummy'));
if($copySuccess){
$this->info('images succefully copied to storage folder');
}
$this->call('migrate:fresh', [
'--seed' => true,
]);
$this->call('db:seed', [
'--class' => 'DataTypesTableSeederCustom'
]);
$this->info('Dummy data installed');
}

OwenIt \ Auditing \ Exceptions \ AuditingException Invalid UserResolver implementation using owen-it/laravel-auditing

I am using owen-it/laravel-auditing for keeping history of changes of prices of products. But I am getting error while updating prices.
OwenIt \ Auditing \ Exceptions \ AuditingException Invalid
UserResolver implementation
Prices do get updated but is history is not updated in database
Products.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use OwenIt\Auditing\Contracts\Auditable;
use OwenIt\Auditing\Contracts\UserResolver;
class Products extends Model implements Auditable, UserResolver
{
use \OwenIt\Auditing\Auditable;
protected $table = 'products';
protected $fillable = ['name','price','season','category','description','stock','image'];
protected $auditInclude = [
'name',
'price',
];
public static function resolveId()
{
return Auth::check() ? Auth::user()->getAuthIdentifier() : null;
}
}
From what I can see, #Faiez is using version 4.x or 5.x of the Auditing package, which registers the User resolver differently, when compared to the new 6.x version, which is what #btl was answering for.
You'll have to update your audit.php configuration file with the following, to make it work:
return [
// ...
'user' = [
// ...
'resolver' => App\Products::class,
// ...
],
// ...
];
However, I would advise you to move the User resolver to a different class (the User model, perhaps?), since it doesn't make much sense to have that in the Products model.
When in doubt, check the documentation
You need to set the resolver class in the config/audit.php file:
'resolver' => [
'user' => App\Products::class,
'ip_address' => OwenIt\Auditing\Resolvers\IpAddressResolver::class,
'user_agent' => OwenIt\Auditing\Resolvers\UserAgentResolver::class,
'url' => OwenIt\Auditing\Resolvers\UrlResolver::class,
],

Elastic search configurations not working in laravel v5.3

I have setup new laravel v5.3 project and install elastic search driver to implement elastic search via composer. But when I reload my page then I always receive This page isn’t working even the elastic search is running on my system below is my complete code that I code.
composer.json
"require": {
"php": ">=5.6.4",
"elasticsearch/elasticsearch": "^6.0",
"laravel/framework": "5.3.*"
},
web.php
Route::get('/',array('uses' => 'ElasticSearch#addPeopleList'));
Controller
<?php
namespace App\Http\Controllers;
class ElasticSearch extends Controller
{
// elastic
protected $elastic;
//elastic cliend
protected $client;
public function __construct(Client $client)
{
$this->client = ClientBuilder::create()->build();
$config = [
'host' =>'localhost',
'port' =>9200,
'index' =>'people',
];
$this->elastic = new ElasticClient($config);
}
public function addPeopleList(){
echo "<pre>";
print_r($this->$elastic);
exit;
}
}
But when I refresh the page then This page isn’t working i received this message and page not loaded one thing that I want to let you know that I made no changes in app.php file of configuration. Please eduacate to solve this issue.
if You want to instantiate an elastic client with some configuration, You should use method ClientBuilder::fromConfig(array $config).
In your case it should be
<?php
$client = ClientBuilder::fromConfig([
'hosts' => [ 'localhost:9200' ]
]);
As You can notice above hosts must be provided as array.
Also I'm not sure that Elasticsearch client that You use have ElasticClient class.
Also if You provided actual code from your controller than it contains an error. You should call class properties like that: print_r($this->client) (without $ near the property name).
Finaly your controller should looks like this:
<?php
namespace App\Http\Controllers;
use Elasticsearch\ClientBuilder;
class ElasticSearch extends Controller
{
/**
* #var \Elasticsearch\Client
*/
protected $client;
public function __construct()
{
$this->client = ClientBuilder::fromConfig([
'hosts' => [
'localhost:9200',
],
]);
}
public function addPeopleList(){
echo "<pre>";
print_r($this->client);
exit;
}
}
And to add a document to the index You need to call this command according to the official documentation
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',
'body' => ['testField' => 'abc']
];
$response = $client->index($params);
print_r($response);
Official documentation can be found here https://github.com/elastic/elasticsearch-php
P.S. Sorry for my English. It is far from perfect.

ReflectionException Class UsersTableSeeder does not exist , while i use composer dump-autoload still same error?

class UsersTableSeeder extends Seeder
{
public function run()
{
User::create([
'name'=> 'prakash',
'username' => 'prakash',
'number' => '*******',
'active' => '1',
'email'=> 'rock****#gmail.com',
'password' => bcrypt('pokhrel215'),
'remember_token'=> str_random(10),
]);
}
}
I tried lots of time, but still same error what can I do?
I am a beginner at laravel.
Make sure the database directory is included in the composer.json file:
"autoload": {
"classmap": [
"database"
]
}
All php files in that directory is scanned when "dumpautoload"-ing. Hope this helps!
I know this is coming in late, but it might help some future readers with similar problem.
Had this same issue and was able to solve it by doing composer dump-autoload in the root of my application
$ composer dump-autoload
Hey hi seeder should be like this
```class UsersTableSeeder extends Seeder
{
public function run()
{
factory(App\User::class, 30)->create()->each(function($user) {
$user->save();
});
}```
If you want to create a user that should be place in migration file not here. hope it will works for you

Laravel 4 PHP: 'Use' statement with both local files and package files

I am trying to include a custom defined validation file that is local to my system and wish to use it with 'package' files from an application I downloaded online. The purpose is so that I can have my own custom validators since I made modifications to this application.
I keep getting the error -> 'Class 'Models\Validators\Photo' not found'
Controller:
use JeroenG\LaravelPhotoGallery\Controllers\AlbumsController; /* From Package */
use JeroenG\LaravelPhotoGallery\Controllers\PhotosController; /* From Package */
use JeroenG\LaravelPhotoGallery\Models\Album; /* From Package */
use JeroenG\LaravelPhotoGallery\Models\Photo; /* From Package */
use Models\Validators as Validators; /* Custom local file */
class EditPhotosController extends PhotosController {
public function __construct()
{
parent::__construct();
}
public function update($albumId, $photoId)
{
$input = \Input::except('_method');
$validation = new Validators\Photo($input); // Here's where error occurs
/* Validation check and update code etc. */
}
}
}
Photo.php -> File path: Models\Validators\Photo.php
namespace Models\Validators;
class Photo extends Validator {
public static $rules = array(
'album_id' => 'required',
'photo_name' => 'required',
'photo_description' => 'max:255',
);
}
Is this just a simple namespacing issue?
The most likely problem is that composer doesn't add file Models/Validators/Photo.php to the autoload index. Make sure you have provided correct path for your files in composer.json.
Try running
composer dump-autoload
to regenerate the autoload files.

Categories