【问题标题】:Displaying records based on a relationship table in Laravel在 Laravel 中基于关系表显示记录
【发布时间】:2020-07-09 09:25:30
【问题描述】:

我是初学者网络开发人员。我在 Laravel 7 中制作我的项目。

我有这个代码:

迁移:

Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name', 155);
            $table->string('title', 155);
            $table->string('description', 155)->nullable();
            $table->string('keywords', 155)->nullable();
            $table->longText('content')->nullable();
            $table->string('delivery_time', 155)->nullable();
            $table->string('small_description', 155)->nullable();
            $table->smallInteger('vat_id')->unsigned()->default(1);
            $table->foreign('vat_id')->references('id')->on('vat');
            $table->bigInteger('category_id')->unsigned()->default(0);
            //$table->foreign('category_id')->references('id')->on('categories');
            $table->char('enable', 1)->default(0);
            $table->char('product_type', 1)->default(0);
            $table->string('slug', 160)->nullable();
            $table->engine = "InnoDB";
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_ci';
        });



Schema::create('selected_product_features', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('product_id')->unsigned();
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
            $table->bigInteger('feature_id')->unsigned();
            $table->foreign('feature_id')->references('id')->on('product_features')->onDelete('cascade');
            $table->string('key', 50);
            $table->text('description')->nullable();;
            $table->timestamps();
        });

我需要显示 selected_product_features key = "form-2" 和 description = 1 中的所有产品。

我该怎么做?

【问题讨论】:

  • 您有 Product 和 SelectedProductFeature 模型吗?您是否定义了从 Product 到 SelectedProductFeature 的关系?
  • 请遵循迁移文档。这样的解释是有的。 laravel.com/docs/7.x/migrations#introduction

标签: php laravel laravel-5 laravel-7


【解决方案1】:

首先,您必须在产品模式中像这样将产品表与所选产品功能表建立关系

public function productSelectedFeature()
{
    return $this->hasMany('App\Models\SelectedProductFeatures', 'product_id', 'id');
}

然后你可以在控制器中编写你的查询

$products = Products::with('productSelectedFeature')->where('description', 1)
            ->whereHas('productSelectedFeature', function($q) {
                $q->where('key', "form-2");
            });

【讨论】:

    【解决方案2】:

    我将从雄辩的文档开始,以获取有关数据库表如何在您的代码中可用的信息。请参阅https://laravel.com/docs/7.x/eloquent 以开始使用 eloquent。

    为了让您快速入门,下面的(伪)代码应该是您的情况的示例。 (1):选择正确的产品功能 (2):获取匹配特征的集合 (3):将特征映射到产品

    $flights = App\SelectedProductFeatures::where('key', 'form-2')
        ->where('description' 1) // (1)
        ->get() // (2)
        ->map(function($feature) { return $feature->product; }); // (3)
    
    

    【讨论】:

      猜你喜欢
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      相关资源
      最近更新 更多