I have model Campaigns
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\CampaignsTargetGeo;
use App\CampaignsTargetCategory;
class Campaigns extends Model
{
const DEFAULT_MARGINALITY = 1.2;
protected $table = 'campaigns';
public $fillable = [
'name',
'url',
'user_rate_min',
'user_rate_max',
'network_rate_min',
'network_rate_max',
'daily_limit',
'hourly_limit',
'status_id',
'user_id'
];
and seed CampaignsSeeder
<?php
use Illuminate\Database\Seeder;
use App\Campaigns;
class CampaignsSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run() {
$campaigns = \DB::table('campaigns')->insert([
'id' => 1,
'name' => 'test campaign',
'url' => 'http://example.com?test=true',
'user_rate_min' => '80',
'user_rate_max' => '100',
'network_rate_min' => '100',
'network_rate_max' => '120',
'daily_limit' => '10000',
'hourly_limit' => '1000',
'status_id' => '1',
'user_id' => '1'
]);
}
}
and database seeder class
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Model::unguard();
$this->call('UserSeeder');
$this->call('DictionariesTargetGeoSeeder');
$this->call('DictionariesTargetCategoriesSeeder');
$this->call('DictionariesCampaignStatusesSeeder');
$this->call('CampaignsSeeder');
Model::reguard();
}
}
when I run php artisan db:seed, I have this error
➜ panel git:(master) ✗ pa db:seed
Seeded: UserSeeder
Seeded: DictionariesTargetGeoSeeder
Seeded: DictionariesTargetCategoriesSeeder
Seeded: DictionariesCampaignStatusesSeeder
[Illuminate\Database\QueryException]
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "campaigns" does not exist LINE 1: insert into "campaigns" ("id", "name", "url", "user_rate_min... ^ (SQL: insert into "campaigns" ("id", "name", "url", "user_rate_min", "user_rate_max", "network_rate_min", "network_rate_max", "daily_limit", "hourly_limit", "status_id", "user_id") values (1, test campaign, http://example.com80test=true, 100, 100, 120, 10000, 1000, 1, 1, ?))
[PDOException]
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "campaigns" does not exist LINE 1: insert into "campaigns" ("id", "name", "url", "user_rate_min...
^
But table exists! I check in psql console. May transaction not commit in this moment?
If I ran ➜ panel git:(master) ✗ php artisan db:seed --class=CampaignsSeeder it works correctly
Why? :-)
insert() method expects an array of arrays (one array per row) but you are inserting one single array. Try with
$campaigns = \DB::table('campaigns')->insert([
[
'id' => 1,
'name' => 'test campaign',
'url' => 'http://example.com?test=true',
'user_rate_min' => '80',
'user_rate_max' => '100',
'network_rate_min' => '100',
'network_rate_max' => '120',
'daily_limit' => '10000',
'hourly_limit' => '1000',
'status_id' => '1',
'user_id' => '1'
]
]);
Related
I am having the following db tables
// Table 1: Foos
id, foo_name, foo_type, created_at, updated_at
// Table 2: Bars
id, bar_name, bar_type, parent_id, foo_id [ForeignKey], created_at, updated_at
// Table 3: Quxes
id, qux_name, bar_id [ForeignKey], created_at, updated_at
And I am having the following seeders setup
class FooSeeder extends Seeder
{
public function run()
{
\App\Models\Qux::truncate();
\App\Models\Bar::truncate();
\App\Models\Foo::truncate();
\App\Models\Foo::create([
'foo_name' => 'Foo',
'foo_type' => 'Foo type',
]);
}
}
class BarSeeder extends Seeder
{
public function run()
{
\App\Models\Qux::truncate();
\App\Models\Bar::truncate();
\App\Models\Bar::create([
'bar_name' => 'Bar',
'bar_type' => 'Bar type',
'foo_id' => 1,
'parent_id' => 1,
]);
\App\Models\Bar::create([
'bar_name' => 'Bar Bar',
'bar_type' => 'Bar Bar type',
'foo_id' => 1,
'parent_id' => 0,
]);
}
}
class QuxSeeder extends Seeder
{
public function run()
{
\App\Models\Qux::truncate();
\App\Models\Bar::truncate();
\App\Models\Qux::create([
'qux_name' => 'Qux',
'bar_id' => 1,
]);
\App\Models\Qux::create([
'qux_name' => 'Qux Qux',
'bar_id' => 1,
]);
}
}
When I try to run php artisan db:seed I get the following error
SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint (`mylaravelschema`.`quxes`, CONSTRAINT `qux_bar_id_foreign` FOREIGN KEY (`bar_id`) REFERENCES `mylaravelschema`.`bars` (`id`)) (SQL: truncate table `bars`)
I have been trying to play with the order of truncating the tables on these three seeders and still haven't manage to sort this, any help appreciated.
use it like this
class FooSeeder extends Seeder
{
public function run()
{
\App\Models\Foo::truncate();
\App\Models\Bar::truncate();
\App\Models\Qux::truncate();
\App\Models\Foo::create([
'foo_name' => 'Foo',
'foo_type' => 'Foo type',
]);
}
}
class BarSeeder extends Seeder
{
public function run()
{
\App\Models\Bar::create([
'bar_name' => 'Bar',
'bar_type' => 'Bar type',
'foo_id' => 1,
'parent_id' => 1,
]);
\App\Models\Bar::create([
'bar_name' => 'Bar Bar',
'bar_type' => 'Bar Bar type',
'foo_id' => 1,
'parent_id' => 0,
]);
}
}
class QuxSeeder extends Seeder
{
public function run()
{
\App\Models\Qux::create([
'qux_name' => 'Qux',
'bar_id' => 1,
]);
\App\Models\Qux::create([
'qux_name' => 'Qux Qux',
'bar_id' => 1,
]);
}
}
please use this hierarchy level because you've the foreign keys and when you're going to truncate it cannot find the reference one
Hope it helps
I have an error on Laravel 9 when run seeder, its say Array to string conversion
I have a same seeder type json before this DataMaster table, and its working. But when i run DataMasterSeeder, its not working
My seeder:
<?php
namespace Database\Seeders;
use App\Models\DataMaster;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DataMasterSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
//SDU
DataMaster::create(['formId' => 1, 'userId' => 1, 'kecamatanId' => 1, 'desaId' => null, 'fieldDatas' => [['id' => '1', 'name' => 'jumlah', 'title' => 'Jumlah', 'value' => '4605']], 'level' => 'kecamatan']);
}
}
And my DataMaster migration:
public function up()
{
Schema::create('data_masters', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('formId');
$table->unsignedBigInteger('userId');
$table->unsignedBigInteger('kecamatanId')->nullable();
$table->unsignedBigInteger('desaId')->nullable();
$table->json('fieldDatas');
$table->enum('level', ['kecamatan', 'desa']);
$table->timestamps();
$table->foreign("formId")->references("id")->on("forms")->onDelete('cascade');
$table->foreign("userId")->references("id")->on("users")->onDelete('cascade');
$table->foreign("kecamatanId")->references("id")->on("kecamatans")->onDelete('cascade');
$table->foreign("desaId")->references("id")->on("desas")->onDelete('cascade');
});
}
I have another seeder like fieldDatas json field in this DataMaster seeder, and i run it successfully before run DataMaster seeder.
you should encode the field fieldDatas before inserting
DataMaster::create([
'formId' => 1,
'userId' => 1,
'kecamatanId' => 1,
'desaId' => null,
// here...
'fieldDatas' => json_encode([['id' => '1', 'name' => 'jumlah', 'title' => 'Jumlah', 'value' => '4605']]),
'level' => 'kecamatan',
]);
I am facing an issue with laravel 7 while truncating table, even I have used FOREIGN_KEY_CHECKS enable and disable still is return this type of error "Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint".
Method
DB::statement("SET FOREIGN_KEY_CHECKS=0;");
Artisan::call('db:seed', ["--database" => 'DBNAME', '--force' => true, "--class" => 'StatusTableSeeder']);
DB::statement("SET FOREIGN_KEY_CHECKS=1;");
Seeder File StatusTableSeeder.php
public function run()
{
\DB::table('statuses')->truncate();
\DB::table('statuses')->insert(array (
0 =>
array (
'id' => 1,
'name' => 'Current',
'type' => 'current',
),
1 =>
array (
'id' => 2,
'name' => 'Resolved',
'type' => 'resolved',
),
));
}
I have updated laravel version 6 to 7, this syntax works fine in laravel 6 but when I update it to laravel 7 then after it is working. If anyone Have idea about it what's the actual issues
try this:
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class StatusTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Schema::disableForeignKeyConstraints();
DB::table('statuses')->truncate();
Schema::enableForeignKeyConstraints();
// and the rest of your code...
}
}
I am trying to create a seeder that fills a databases with "Assignments" that are linked with a "Course" database with foreign key constraint.
Since i am pretty new to PHP and Laravel 6 in general i don't really know where to start.
I allready have a seeder that fills my "Course" database, which like this:
class CoursesTableSeed extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('courses')->insert([[
'name' => 'Opleidings- en beroepsoriëntatie',
'ecs' => '2.5'
],[
'name' => 'Computer science basics',
'ecs' => '7.5'
],[
'name' => 'Programming basics',
'ecs' => '5'
],[
'name'=>'Professional skills 1',
'ecs'=>'2.5',
],[
'name'=>'HZ Personality',
'ecs'=>'2.5',
],[
'name'=>'Object-oriented programming',
'ecs'=>'10',
],[
'name'=>'Professional skills 2',
'ecs'=>'2.5',
],[
'name'=>'Professionele werkplek',
'ecs'=>'2.5',
],[
'name'=>'Framework development 1',
'ecs'=>'5',
],[
'name'=>'Framework project 1',
'ecs'=>'5',
],[
'name'=>'Professional skills 3',
'ecs'=>'2.5',
],[
'name'=>'IT Personality 1',
'ecs'=>'2.5',
],[
'name'=>'Framework development 2',
'ecs'=>'5',
],[
'name'=>'Framework project 2',
'ecs'=>'5',
]
]);
}
}
Now i want to do the same thing with my assignment database but i cant figure out how since i also want to have the "Assignments" linked with theyr respective "Course" so when i delete a course it also deletes it associated assignments. If this question comes over a little vague, sorry for that. I am pretty new to PHP laravel and programming in general.
Also, this is my migration for the assignments:
class CreateAssignmentsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('assignments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('course_id');
$table->text('name');
$table->decimal('weight',3,1)->nullable();
$table->decimal('grade', 3, 1)->nullable();
$table->timestamps();
$table->foreign('course_id')
->references('id')
->on('courses')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('assignments');
}
}
Welcome to Stackoverflow!
You can seed your assignments in different ways. For example: update existing seeder, or create another seeder.
Here code for another seeder (you must have Course and Assignment models):
<?php
use App\Assignment;
use App\Course;
use Illuminate\Database\Seeder;
class AssignmentsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$data = [
[
'name' => 'Opleidings- en beroepsoriëntatie',
'assignments' => [
[
'name' => 'Assignment 1',
'weight' => 1.5,
'grade' => 1.1,
],
[
'name' => 'Assignment 2',
'weight' => 2.0,
'grade' => 1.2,
],
]
],
[
'name' => 'Computer science basics',
'assignments' => [
[
'name' => 'Assignment 3',
'weight' => 1.5,
'grade' => 1.0,
],
[
'name' => 'Assignment 4',
'weight' => 1.5,
'grade' => 1.0,
],
[
'name' => 'Assignment 5',
'weight' => 1.5,
'grade' => 1.0,
],
]
],
];
// Loops through courses
foreach ($data as $key => $value) {
$course = Course::where('name', $value['name'])->first();
if ($course instanceof Course) {
if (isset($value['assignments']) && is_array($value['assignments'])) {
// Loops through assignments
foreach ($value['assignments'] as $assignment) {
$assignment['course_id'] = $course->id;
Assignment::firstOrCreate($assignment);
}
}
}
}
}
}
Put this code to database/seeds/AssignmentsTableSeeder.php file and after that call this console commands:
composer dump-autoload
php artisan db:seed --class=AssignmentsTableSeeder
After that you will get the following result
I'm trying to seed my Laravel 5.6 application through faker factory, I went through the link and little bit confused, As I have some basic static data, like for example I've a company model:
class Company extends Model {
use SoftDeletes, HasDataSearchTable, HasSlug;
protected $fillable = [
'name', 'code_link', 'slug', 'establishment', 'parent_id', 'website', 'updates', 'user_id', 'tracked', 'verified', 'active', 'premium', 'status'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'created_at','updated_at','deleted_at'
];
public function roles()
{
return $this->belongsToMany('Noetic\Plugins\Conxn\Models\Variables\Company\Role', 'company_role_relation', 'company_id', 'role_id')->withTimestamps();
}
}
And a relational role model:
class Role extends Model
{
use SoftDeletes , HasDataSearchTable;
protected $table='company_role';
protected $fillable = [
'name', 'parent_id'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'created_at','updated_at','deleted_at'
];
}
and respective database, I'm following the laravel convention, Now I want to seed the data:
I've particular set of roles which I'm seed in manually,
class CompanyRoleSeed extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('company_role')->insert([
['name' => 'Contractor', 'parent_id' => null],
['name' => 'Consultant', 'parent_id' => null],
['name' => 'Manufacturer', 'parent_id' => null],
['name' => 'Miscellaneous', 'parent_id' => null],
['name' => 'Owner', 'parent_id' => null],
['name' => 'Supplier', 'parent_id' => null],
]);
}
}
For company I want to create factory so I did:
$factory->define(Company::class, function (Faker $faker) {
return [
'name' => $faker->company,
'code_link' => rand(5, 10),
'slug' => str_slug($faker->company),
'about' => $faker->paragraphs(),
'establishment' => $faker->randomElement('2015', '2016', '2017', '2018'),
'parent_id' => $faker->randomElement(null, '1', '2', '3'),
'website' => $faker->url,
'user_id' => $faker->randomElement('1', '2', '3', '4', '5'),
'updates' => $faker->paragraphs(),
'tracked' => $faker->boolean,
'verified' => $faker->boolean,
'active' => $faker->boolean,
'premium' => $faker->boolean,
'status' => $faker->randomElement('saved', 'draft')
];
});
And in company seed I'm having:
class CompanySeed extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
factory(Company::class, 10)->create()->each(function ($company) {
$company->roles()->save(); // Don't now how to execute here
});
}
}
Help me at place $company->roles()->save(); What should I do over here.
Any guidance or improvisation welcome.
You can query which roles you want to assign to the companies and related them to the created records like this:
class CompanySeed extends Seeder
{
public function run()
{
$contractorRole = Role::whereName('Contractor')->firstOrFail();
$ownerRole = Role::whereName('Owner')->firstOrFail();
factory(Company::class, 10)->create()->each(function ($company) use ($contractorRole, $ownerRole) {
$company->roles()->attach([
$contractorRole->id,
$ownerRole->id
]);
});
}
}
You can check the doc for relating records https://laravel.com/docs/5.6/eloquent-relationships#inserting-and-updating-related-models
before answering your question you should know that Laravel's documentation explains how to do this.
But in order to save a related Model you first need to create a fake one, or in your case relate a role you have already created. In order to do this you could first create a Role factory using something like this:
$factory->define(App\Role::class, function (Faker $faker) {
$randomRoleAlreadyCreated = \App\Role::all()->random();
return [
'name' => $randomRoleAlreadyCreated->name,
'parent_id' => $randomRoleAlreadyCreated->parent_id
];
});
As you can see on Role factory I created I pull a random Role since you stated that you already created them manually, so if you choose one randomly then your companys will be related to one of your roles randomly!
Once you have: Roles created in DB, factory of roles, you could relate random roles to a company using the factory to save a random instance.
factory(Company::class, 10)->create()->each(function ($company) {
$company->roles()->save(factory(App\Role::class)->make()); // Don't now how to do here
});
Update
If you want to save multiple roles for each company you could do this:
factory(Company::class, 10)->create()->each(function ($company) {
// Instead of 4 you could also create a random number
// using $numberOfRolesToAttach = rand($min,$max)
for($i = 1; $i <= 4; $i++) :
$company->roles()->save(factory(App\Role::class)->make());
endfor;
});