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:
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
Controller Code: How to fetch product data from the database and rendering create view.
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!! | ||||
public function create()
{
$product = Product::all();
$params = [
'title' => 'Product Listing',
'product' => $product ];
return view('products.create')->with($params);
}
0 comments:
Post a Comment