Reputation: 113
I have 3 tables in database named tbl_product_manager, tbl_tags, tbl_categories. tbl_product_manager and tbl_categories are linked to one to one relations and tbl_tags and tbl_product_manager is linked with many to many relations with pivot table tbl_product_tag. I had used eloquent for this. I am able to perform insert and update operation in those tables but I am stuck in viewing data through HTML.
This is my controller code for view:
public function getProducts()
{
$productList = ProductManagementModel::getAllProducts();
//var_dump($productList); die();
$i = 1;
$products = '';
foreach($productList as $product) {
$products .= '<tr class="odd gradeX">';
$products .= '<td>'.$i++.'</td>';
$products .= '<td>'.$product->product_name.'</td>';
$products .= '<td>'.$product->category_name.'</td>';
$products .= '<td>'.$product->product_cost .'</td>';
if($product->is_active=='1') {
$products .= '<td>'.'<a href="javascript:void(0)" class="unpublish-selectedproduct" id="'.$product->id.'" >'.
'<span class="label label-success"><i class="icon-ok"></i></span></a>  '.'</td>';
} else {
$products .= '<td>'.'<a href="javascript:void(0)" class="publish-selectedproduct" id="'.$product->id.'" >'.
'<span class="label label-warning"><i class="icon-minus-sign"></i></span></a>  '.'</td>';
}
$products .= '<td>'.$product->updated_at.'</td>';
$products .= '<td>'.
'<a href="product_management/edit/'.$product->id.'"><i class="icon-pencil"></i> Edit</a> '.
'<a href="javascript:void(0)" class="delete-selectedproduct" id="'.$product->id.'" >'.
'<i class="icon-trash" ></i> Delete</a>'.'</td>';
$products .= '</tr>';
}
return View::make('admin.product_management.list', array('products' => $products));
}
Model of table tbl_product_manager
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class ProductManagementModel extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
protected $table = 'product_manager';
public function categories(){
return $this ->hasOne('CategoriesModel','id');
}
public function tag()
{
return $this->hasMany('TagModel','id');
}
public static function getAllProducts(){
return $product = DB::table('product_manager')
->join('categories', 'product_manager.category_id', '=','categories.id')
->select('product_manager.id', 'categories.id','product_manager.*', 'categories.category_name')
->groupby('product_manager.id')
->get();
}
public function tags() {
return $this->belongsToMany('TagModel', 'product_tag', 'product_id', 'tag_id');
}
}
Model of table tbl_tag
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class TagModel extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'tags';
public function productManagement()
{
return $this->hasMany('productManagementModel');
}
public static function getAlltags()
{
return $tags = DB::table('tags')
->get();
}
public function products() {
return $this->belongsToMany('productManagementModel', 'product_tag', 'tag_id', 'product_id');
}
}
model of pivot table tbl_product_tag
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class ProductTagModel extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
protected $table = 'product_tag';
public $limit;
public static function productTags()
{
return $productTag = DB::table('product_tag')
->get();
}
}
I am making the html view in controller itself. I want to use eloquent for to access the data of tbl_tag which is linked with the pivot table and the parent table(tbl_product_manager) is also linked with the pivot table.
Upvotes: 2
Views: 853
Reputation: 163758
You didn't show how are you trying to display tags. Looks like you just to display all tags for each product. In this case, load the data:
$products = ProductManagementModel::with('tags')->get();
And then display it:
@foreach ($products as $product)
{{ $product->name }}
@foreach ($product->tags as $tag)
{{ $tag->name }}
@endforeach
@endforeach
Upvotes: 2