【问题标题】:How to show values of a lookup table in a many to many relationship如何在多对多关系中显示查找表的值
【发布时间】:2021-03-11 21:30:49
【问题描述】:

我有很多表如下

植物:模型

  public function species()
     {
         return $this->belongsToMany(Species::class,'phyto_species')->withPivot(['destination_id', 
 'preservation_id','weight']);
   
     }

物种:模型

  public function phytos()
     {
         return $this->belongsToMany(Phyto::class,'phyto_species')->withPivot(['destination_id', 
 'preservation_id','weight']);
   
     }

数据透视表

  public function up()
{
    Schema::create('phyto_species', function (Blueprint $table) {
        $table->id();
        $table->integer('phyto_id')->unsigned();
        $table->integer('species_id')->unsigned();
        $table->integer('destination_id')->unsigned();
        $table->integer('preservation_id')->unsigned();
        $table->double('weight', 8,2);
        // $table->double('charge', 8,2);
        $table->softdeletes();
        $table->timestamps();
    });
}

我的另外两个查找表的目的地和保存如下

 public function up()
{
    Schema::create('destinations', function (Blueprint $table) {
        $table->id();
        $table->string('destination_name');
        $table->softdeletes();
        $table->timestamps();
    });
}

 public function up()
{
    Schema::create('preservations', function (Blueprint $table) {
        $table->id();
        $table->string('preservation_name');
        $table->softdeletes();
        $table->timestamps();
    });
}

我的问题是我能够显示重量、物种名称保存 ID 和目的地 ID,但我不想显示保存 ID 和目的地 ID,而是显示保存名称和目的地名称

      @foreach ($phyto->species as $item) 
         <ul>
            <li> 
            {{$item->pivot->weight}} {{$item->species_name}} {{$item->pivot->preservation_id}} {{$item->pivot->destination_id}}
            </li>
        </ul>
        @endforeach

我可以使用下面显示的 sql 查询来获取保存名称和目的地名称,但我正在寻找在关系中使用函数的其他方法

 select phytos.customer_name, phytos.phyto_number, phyto_species.weight, 
 preservations.preservation_name, destinations.destination_name from phytos 
 left join phyto_species on phytos.id = phyto_species.phyto_id left JOIN 
 destinations on destinations.id = phyto_species.destination_id left JOIN 
 preservations on preservations.id = phyto_species.preservation_id limit 5

请帮忙!

【问题讨论】:

    标签: laravel


    【解决方案1】:

    第 1 步:

      public function species()
         {
             return $this->belongsToMany(Species::class,'phyto_species')->withPivot(['destination_id', 'preservation_id','weight'])->using(PhytoSpecies::class); //use keyword using and pass pivot table class as a parameter
          }
    

    第 2 步:

     <?php
    
     namespace App\Models\Consignment;
    
     use Eloquent as Model;
     use Illuminate\Database\Eloquent\SoftDeletes;
     use \Illuminate\Database\Eloquent\Relations\Pivot; //import pivot class
    
     /**
      * Class PhytoSpecies
      * @package App\Models\Consignment
      * @version November 24, 2020, 3:32 am UTC
      *
      * @property integer $phyto_id
      * @property integer $species_id
      * @property integer $destination_id
      * @property integer $preservation_id
      * @property number $weight
      */
     class PhytoSpecies extends Pivot //replace Model with Pivot
     {
         use SoftDeletes;
    
         public $table = 'phyto_species';
    
         const CREATED_AT = 'created_at';
         const UPDATED_AT = 'updated_at';
    
    
         protected $dates = ['deleted_at'];
    
    
    
         public $fillable = [
            'phyto_id',
            'species_id',
            'destination_id',
            'preservation_id',
            'weight'
         ];
    
         /**
         * The attributes that should be casted to native types.
         *
         * @var array
         */
         protected $casts = [
             'id' => 'integer',
             'phyto_id' => 'integer',
             'species_id' => 'integer',
             'destination_id' => 'integer',
             'preservation_id' => 'integer',
             'weight' => 'float'
         ];
    
         /**
          * Validation rules
          *
          * @var array
          */
         public static $rules = [
             'phyto_id' => 'required|integer',
             'species_id' => 'required|integer',
             'destination_id' => 'required|integer',
             'preservation_id' => 'required|integer',
             'weight' => 'required|numeric',
             'deleted_at' => 'nullable',
             'created_at' => 'nullable',
             'updated_at' => 'nullable'
         ];
    
         public function preservation(){
             return $this->belongsTo(Preservation::class); // add this relationship
         }
    
         public function destination(){
        return $this->belongsTo(Destination::class); // add this relationship
         }
    
    
    }
    

    第 3 步:

     @foreach ($phyto->species as $item) 
           <ul>
              <li> 
                 <strong>
                     {{$item->species_name}} &nbsp&nbsp {{$item->pivot->weight}} {{$item->pivot->preservation->preservation_name}}
                 </strong>
              </li>
           </ul>
     @endforeach
    

    参考:Accessing Pivot table "3rd Model relationship" in Laravel Eloquent

    How to add additional column relationship in pivot table in Laravel

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 2014-03-29
      • 2010-11-25
      • 1970-01-01
      • 2013-03-08
      • 2021-11-13
      相关资源
      最近更新 更多