【问题标题】:Get the value from 2 table on laravel从 laravel 上的 2 个表中获取值
【发布时间】:2022-01-28 10:15:50
【问题描述】:
Schema::create('discounts', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->dateTime('from', $precision =0);
            $table->dateTime('to', $precision =0);
            $table->enum('status', [0, 1, 2])->default(0)->comment('0:active 1:expired 2:scheduled');
            $table->timestamps();
            $table->softDeletes();
        });

Schema::create('discount_product', function (Blueprint $table) {
            $table->id();
            $table->string('code');
            $table->unsignedBigInteger('discount_id');
            $table->unsignedBigInteger('product_id');
            $table->foreign('discount_id')->references('id')->on('discounts')->onDelete('cascade');
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
            $table->float('rate');
            $table->timestamps();
            $table->softDeletes();
        });


Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique();
            $table->float('price');
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('category_id');
            $table->unsignedBigInteger('brand_id');
            $table->string('image')->nullable();
            $table->text('description')->nullable();
            $table->integer('quantity');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->foreign('brand_id')->references('id')->on('brands')->onDelete('cascade');
            $table->timestamps();
            $table->softDeletes();
        });

public function discountProducts()
    {
        return $this->hasMany(DiscountProduct::class);
    }


 public function discount()
    {
        return $this->belongsTo(Discount::class);
    }

    /**
     * Get product of discount
     */
    public function product()
    {
        return $this->hasMany(Product::class);
    }

我有 3 个这样的表:折扣、折扣产品、产品,在我的 detailProduct.blade.php 中,我想让产品有折扣,但我不知道该怎么做。有人能帮我吗 ?非常感谢你

这是我的:查看详细功能:我得到了 discount_products

 public function show($id)
    {
        $discount = $this->discountRepository->getDiscountById($id);
        $discount_products = $this->discountRepository->getDiscontProductOnDiscount($id);

        return view('user.discounts.detailDiscount', 
                   ['discount_products' => $discount_products, 'discount' => $discount]);
    }

【问题讨论】:

    标签: php html laravel laravel-5 laravel-8


    【解决方案1】:

    你可以这样做

    $data['discount'] = $this->discountRepository->getDiscountById($id);
    $data['discount_products'] = $this->discountRepository->getDiscontProductOnDiscount($id);
    
    return view('user.discounts.detailDiscount', $data);
    

    在您看来,您可以访问像这样的数据 $discount$discount_products

    如果你的 $product 上有很多数据,你不能先 foreach 并在视图中这样打印,我假设你会在 table 上打印所以会这样

    <tbody>
       @foreach ($products as $row)
        <tr>
         <td>{{$row->name}}</td>
         <td>{{$row->price}}</td>
        <tr>
       @endforeach
    </tbody>
    

    如果您的数据只有 1 行,您可以像这样访问

    $products[0]->name
    

    【讨论】:

    • 顺便说一句,如何访问产品名称
    猜你喜欢
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    相关资源
    最近更新 更多