Last active
May 7, 2019 07:06
-
-
Save MaheKarim/4bf61a9f3a95759e382797f684fbd0b6 to your computer and use it in GitHub Desktop.
Seeding Error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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), | |
]); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(), | |
]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here Is Error