Jun 16, 2018

How to add dummy data into table using Laravel seeder

Manually add data into the database it will take time and fussy for the redundant task. So this tedious job reducing by Laravel using seeder.

Let's check the demo of how to add dummy data in category table using Laravel seeder.

First, you need to install faker into Laravel. Faker is a PHP library that generates fake data for you

Install faker using composer command

composer require fzaninotto/faker

After installing faker you can see in require dev parameter in composer.json file in Laravel as below

composer.json file has below configuration:

"require-dev": {
        "fzaninotto/faker": "~1.4",
    },

For more information about faker, you can visit the links

Create categories seeder class using below command

php artisan make:seeder CategoriesTableSeeder

Run specific seeder using below command

php artisan db:seed --class=CategoriesTableSeeder

Refresh seeder class using below command with migrate.

php artisan migrate:refresh --seed

Categories Databse: CategoryID, CategoryName, Description - Here CategoryID is primary key

Category Model :
 
namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{

    public $timestamps=false;
}


I am adding 20 records as
1,category1, et sit et mollitia sed.
2,category2, et sit et mollitia sed.
etc into categories table.

 

use Carbon\Carbon;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class CategoriesTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        //
   DB::table('categories')->delete();
   
        $faker = Faker\Factory::create();
        for ($i = 1; $i < 20; $i++) {
            DB::table('categories')->insert([
                'CategoryName' => 'Category'.$i,'Description'=> $faker->text
        
            ]);
        }
  
    }
}

For the run, this CategoriesTableSeeder add this call into DatabaseSeeder class.
 
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // $this->call(UsersTableSeeder::class);
  $this->call(CategoriesTableSeeder::class);
    }
}

How to display category name in the product listing page using ORM in Laravel

Many developers facing the issue regarding category name display in the product listing page.

I also facing the same issue so I have figured it out using the relationship.

Can't display category name in Product List page because of category and product both are the different entity so both data stored into a different table

For that, need to give a relationship between product and category.

Let's see below example

The relationship between category and product:

A product belongs to category and category hasMany products.

My Database:

Category: CategoryID, CategoryName - Here CategoryID is primary key

Product: ProductID, CategoryID, Product Name, Image, Status, Created date - ProductID is Primary key and CategoryID is reference key

Product Model:
 

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    
    public $timestamps=false; // default created_at and updated_at given so I passed as false;
    protected $fillable = [
        'category_id','product_name','image','status','created_date'
    ];
    public function category()
    {
        return $this->belongsTo(Category::class);
    }
}

Category Model:
 
namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    //
    public $timestamps=false;
    public function Product()
    {
        return $this->hasMany(Product::class);
    }

}
Once we have done with configuration model than see how to access category name in product listing template file.

Using below syntax it will display category name of that particular product.

{{$product->category->category_name}} -> Here category is relationship defined in product model.

create.blade.tpl
 

        @if(count($product) > 0 )
    @foreach ($product as $product)
     // You can access using category relationship in product model

        
    @endforeach
    @else
    
    @endif
    
    
    
Product Name Category Name Image Status Created Date
{{ $product->product_name}} {{$product->category->category_name}} @if ($product->status == '1') Active @else Inactive @endif {{ date('F d, Y', strtotime($product->created_date)) }}
No Record Found!!
Controller Code: How to fetch product data from the database and rendering create view.
 
public function create()
    {
        $product = Product::all(); 
        
        $params = [
            'title' => 'Product Listing',
            'product' => $product        ];

        return view('products.create')->with($params);
  
    }