【问题标题】:Accessing Pivot table data in laravel在 laravel 中访问数据透视表数据
【发布时间】:2018-01-08 11:45:18
【问题描述】:

我在名为 tbl_product_manager、tbl_tags、tbl_categories 的数据库中有 3 个表tbl_product_managertbl_categories 链接到一对一关系, tbl_tagstbl_product_manager 链接到 ma​​ny 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>&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)也与数据透视表链接。

【问题讨论】:

    标签: sql laravel


    【解决方案1】:

    您没有显示您是如何尝试显示标签的。看起来您只是显示每个产品的所有标签。在这种情况下,load the data

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

    然后显示出来:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-19
      • 1970-01-01
      • 2016-11-14
      • 2018-04-10
      • 1970-01-01
      • 2022-06-20
      相关资源
      最近更新 更多