Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MaheKarim/4bf61a9f3a95759e382797f684fbd0b6 to your computer and use it in GitHub Desktop.
Save MaheKarim/4bf61a9f3a95759e382797f684fbd0b6 to your computer and use it in GitHub Desktop.
Seeding Error
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name', 128)->unique();
$table->string('slug', 191)->unique();
$table->tinyInteger('active')->default(1);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('category_id');
$table->string('name');
$table->string('slug')->unique();
$table->text('description');
$table->unsignedBigInteger('quantity');
$table->decimal('price', 15);
$table->tinyInteger('active')->default(1);
$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
<?php
use Illuminate\Database\Seeder;
use App\Category;
class CategoryTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
$categories = ['Laravel Developer','Web Developer','E-Commerce Developer'];
foreach($categories as $category){
Category::create([
'name' => $category,
'slug' => str_slug($category),
'description' => str_random(120),
]);
}
}
}
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
// $this->call(UsersTableSeeder::class);
// $this->call(CategoryTableSeeder::class);
// $this->call(ProductTableSeeder::class);
// factory(App\Category::class,7)->create();
// factory(App\Product::class,20)->create();
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
//
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $guarded = [''];
public $timestamps = false;
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
//
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $guarded = [''];
public $timestamps = false;
}
<?php
use Illuminate\Database\Seeder;
use App\Product;
use Carbon\Carbon;
class ProductTableSeeder extends Seeder
{
public function run()
{
// $products= ['Hotel Management','School Management'];
for ($i=1; $i <= 100 ; $i++){
Product::create([
'category_id' => rand(1,5),
'name' => 'Product' .$i,
'slug' => str_slug('Product' .$i),
'description' => str_random(120),
'quantity' => rand(1,10),
'price' => rand(100,5000),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
]);
}
}
}
@MaheKarim
Copy link
Author

MaheKarim commented May 7, 2019

ProductTableSeedProb
Here Is Error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment