【发布时间】:2018-01-08 11:45:18
【问题描述】:
我在名为 tbl_product_manager、tbl_tags、tbl_categories 的数据库中有 3 个表。 tbl_product_manager 和 tbl_categories 链接到一对一关系, tbl_tags 和 tbl_product_manager 链接到 many to与数据透视表 tbl_product_tag 的许多关系。为此,我使用了 eloquent。我能够在这些表中执行插入和更新操作,但我被困在通过 HTML 查看数据。
这是我的视图控制器代码:
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));
}
表模型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');
}
}
表 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');
}
}
数据透视表模型 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();
}
}
我正在控制器本身中制作 html 视图。我想使用 eloquent 访问与数据透视表链接的 tbl_tag 的数据,并且父表(tbl_product_manager)也与数据透视表链接。
【问题讨论】: