在laravel中

时间:2018-01-08 11:45:18

标签: sql laravel

我在数据库中有3个名为 tbl_product_manager,tbl_tags,tbl_categories 的表。 tbl_product_manager tbl_categories 与一对一关系相关联, tbl_tags tbl_product_manager 多个关联许多关系与数据透视表 tbl_product_tag 。我曾经为此滔滔不绝地说过。我能够在这些表中执行插入和更新操作,但我仍然坚持通过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>&nbsp;&nbsp'.'</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>&nbsp;&nbsp'.'</td>';
        }
        $products .= '<td>'.$product->updated_at.'</td>';
        $products .= '<td>'.
                    '<a href="product_management/edit/'.$product->id.'"><i class="icon-pencil"></i> Edit</a>&nbsp;&nbsp;'.
                    '<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)也与数据透视表链接。

1 个答案:

答案 0 :(得分:1)

您没有展示如何显示标签。您似乎只是为每个产品显示所有标签。在这种情况下,load the data

$products = ProductManagementModel::with('tags')->get();

然后显示它:

@foreach ($products as $product)
    {{ $product->name }}
    @foreach ($product->tags as $tag)
        {{ $tag->name }}
    @endforeach
@endforeach