I have a little problem trying to seed my comments table.
I'm 100% sure that I have the Class CommentTableSeeder.php in my /database/seeds directory.
CommentTableSeeder.php
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class CommentTableSeeder extends Seeder {
public function run()
{
DB::table('comments')->delete();
Comment::create(array(
'author' => 'Chris Sevilleja',
'text' => 'Look I am a test comment.'
));
Comment::create(array(
'author' => 'Nick Cerminara',
'text' => 'This is going to be super crazy.'
));
Comment::create(array(
'author' => 'Holly Lloyd',
'text' => 'I am a master of Laravel and Angular.'
));
}
}
Then when I run : php artisan db:seed
I kept getting
I've also try running composer update and run : php artisan db:seed - still get the same result.
Any hints / help will be much appreciated !
You need to run
composer dump-autoload
to fix this error. What this does, among other things, is refreshes the list of classes available to your application.
In this case, while the class did exist in the right location, it was unavailable in your autoloaded class list, and thus returning a Not Found error.
Related
I am able to successfully run the migration/factory/seeders from the command line using php artisan migrate:fresh --seed
However when I try to manually create a category using tinker, I'm getting errors:
<warning>PHP Warning: Array to string conversion in /Users/[my_name]/Sites/blog/vendor/laravel/framework/src/Illuminate/Support/Str.php on line 99</warning>
TypeError: Illuminate\Database\Grammar::parameterize(): Argument #1 ($values) must be of type array, string given, called in /Users/[my_name]/Sites/blog/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 886
And
<warning>PHP Warning: Array to string conversion in /Users/[my_name]/Sites/blog/vendor/laravel/framework/src/Illuminate/Support/Str.php on line 99</warning>
=> App\Models\Category {#4529
name: [
"quaerat",
"voluptatem",
],
slug: "array",
}
Factory code:
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class CategoryFactory extends Factory
{
/**
* Define the model's default state.
*
* #return array
*/
public function definition()
{
$name = $this->faker->words(2,true);
$slug = Str::of($name)->slug('-');
return [
'name' => ucwords($name),
'slug' => $slug
];
}
}
I have two questions:
Why do the two methods (make and create) cause different, albeit related, errors?
Why does tinker get tripped up when the normal migration/seed process works fine?
Please and thank you.
I still don't know why the two errors were different, but it's pedantic.
Thank you to #Rwd! I now know that we need to restart tinker after we change our application code in Laravel.
The string to array error was from a previous version of the code before I figured out to add the second parameter to $this->faker->words() — which is why it worked with php artisan migrate:fresh --seed and with in php artisan tinker \App\Models\Category::factory()->create();
My ModelFactory:
<?php
$factory->define(App\Models\Customer::class, function (Faker\Generator $faker) {
return [
'name' => $faker->company,
'email' => $faker->unique()->safeEmail,
'status'=> $faker->numberBetween($min = 0, $max = 2),
'slug'=> $faker->slug,
];
});
Database seeder
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$customers= factory(App\Models\Customer::class, 100)->create();
}
}
When I run
php artisan db:seed
I get the error
[Symfony\Component\Debug\Exception\FatalThrowableError]
Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting '
]'
I've tried everything I can think of, but can't find the problem...
Edit:
I forgot to mention that this was working fine a day earlier and then "broke" as I started to add more ModelFactories (in separate files). I then discarded all my changes (from source control) to be 100% sure I hadn't changed anything. The only other aspect could be that I have something in the .gitignore that may have updated and wasn't rolled back:
/node_modules
/public/storage
/public/hot
/storage/*.key
/.idea
Homestead.json
Homestead.yaml
It seems the problem is this line:
'status'=> $faker->numberBetween($min = 0, $max = 2),
It should be:
'status'=> $faker->numberBetween(0, 2),
Ok, so I found the reason and feel really idiotic about it, but posting it here anyway in case someone follows in my footsteps.
The issue was that there were other ModelFactories in the database/factories folder and it seems that running php artisan db:seed parses those files as well, even though they are not referenced in the DatabaseSeeder class. One of those files had incorrect one-many syntax and that was causing the error.
The only way I realized it was that I ran the factory->create method within php artisan tinker and the error message it threw up referenced this other factory definition.
FWIW, I then used the approach outlined here for my relationships - for the reasons mentioned in the question there...
I want to use php artisan db:seed command on my cmd but it said Label 'DB' already defined,
this is my SeederTableAnggota Code:
<?php
use Illuminate\Database\Seeder;
class SeederTableAnggota extends Seeder {
Public function run()
{
DB:table('anggota')->delete();
$anggota= array(
array('id'=>1,'nama'=>'Rizki Amelia Dewi','alamat'=>'Cilengsi'),
array('id'=>2,'nama'=>'Dewi Ayunindita','alamat'=>'Jatinangor'),
array('id'=>3,'nama'=>'Siti Hajar Riska','alamat'=>'Jakarta')
);
DB:table('anggota')->insert('anggota');
}
}
i already use $this->call('SeederTableAnggota') on my DatabaseSeeder.php.
And i already use composer dump-autoload on my cmd too.
So how can i use db:seed and why it said Label 'DB' already defined? thanks for your help and any help will be very useful
It's because you have written DB:table(...) instead of DB::table(...).
You forgot a colon so PHP thought that it is a constant.
Your seeder seems to be too complicated. Do you have a model?
just completing your answer, in my case the problem was because I was typing db::seed when the correct one is db:seed ie I was putting :: when the correct one is : , silly mistake but when you are tired goes unnoticed.
So I'm switching our application to use Mango rather than the built in ORM within Kohana. I've switched over all the necessary application code to work as expected, but when our CI server runs through our unit tests, I get a "Class 'Mango' not found" error.
Tests provided are dumbed down, but the style I use in the UnitTest is exactly the same way I use them in a regular GET request. It works when I do a GET, but the unit test fails. Now hopefully unrelated, I cannot reproduce this locally, but can't ever get the unit test to work on our CI server.
My guess is that I'm not loading the module properly, but like I said, it works correctly in the application and only my unit tests are failing (with FATAL ERRORs).
application/classes/Model/User.php
class Model_User extends Mango {
protected $_fields = array(
'user_id' => array('type' => 'string', 'required'=>TRUE),
'first_name' => array('type' => 'string', 'required'=>TRUE),
'last_name' => array('type' => 'string', 'required'=>TRUE),
);
}
application/tests/UserTest.php
Class UserTest extends Unittest_TestCase
{
public function testUserCreation()
{
$user_data = array(
"user_id" => "1234asdf",
"first_name" => "Test",
"last_name" => "User",
);
$new_user = Mango::factory("User", $user_data);
$this->assertEquals($user_data, $new_user->as_array());
}
}
EDIT: Here's a link to the Mango module I've brought in: https://github.com/Wouterrr/MangoDB
If anyone stumbles upon this via google, I solved the issue. It appears as though our applications nginx config handles capitalizations more nicely than the CLI. Upon changing "Mango" to "mango" I saw the error message change to not finding it's parent class (for the same casing reasons). While I imagine I could have just changed all the casing, Kohana has a function for casing issues, so in application/bootstrap.php, you just have to run both of the following:
spl_autoload_register(array('Kohana', 'auto_load'));
spl_autoload_register(array('Kohana', 'auto_load_lowercase'));
I am trying to get a scheduler class running on typo3.
ext_autoload.php:
$extensionPath = t3lib_extMgm::extPath('mh_compass');
$loaderClass = array(
'tx_monitorcompassdailyreset_sched' => $extensionPath.'scheduler/class.tx_monitorcompassdailyreset_sched.php',
);
return $loaderClass;
ext_localconf.php:
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['tx_monitorcompassdailyreset_sched'] = array(
'extension' => $_EXTKEY,
'title' => 'Compass Monitor Daily Reset Scheduler',
'description' => 'Reset daily Monitor Jobs',
);
class.tx_monitorcompassdailyreset_sched.php:
class tx_monitorcompassdailyreset_sched extends tx_scheduler_Task {
public function execute() {
error_log( "Start Compass Monitor Daily Reset Scheduled Job" );
//do some stuff
error_log( "Finished Compass Monitor Scheduled Job" );
}
}
When I try to add the task in the scheduler in the backend, I get the following error:
Fatal error: Class 'tx_monitorcompassdailyreset_sched' not found in /var/www/typo3_src-4.5.22/t3lib/class.t3lib_div.php on line 5375
This doesn't make sense as a) I have used debug on that function and it loads all the other classes, and b) I cannot see a discrepancy in my class naming.
The version of php is 5.3.10 (there was a bug like this with 5.3.2, but it disappeared)
Your class naming is obviously wrong. Your extension key is "mh_compass", so your class name needs to start with "tx_mhcompass_". The beginning of the class name always contains "tx_" followed up with the extension key without underscores.
Try renaming your class to "tx_mhcompass_Task_Reset" for example.