【问题标题】:Laravel: 2 DB Queries in Controller while 1 value depens on first queryLaravel:控制器中有 2 个数据库查询,而 1 个值取决于第一个查询
【发布时间】:2016-10-28 07:26:18
【问题描述】:

我有两个表,产品和子产品。 我还有一个显示产品信息的视图(查看从数据库动态生成的数据)。在此视图中,应显示所有子产品。我怎样才能做到这一点?因为我在视图中循环第一个查询,而不是在控制器中。

控制器:

public function showSpecificProduct($name)
    {
        $name = strtolower($name);
        $product = \DB::select('select * from products where name = ? LIMIT 1', [$name]);

        $subProducts = \DB::select('select * from subproducts where mainproduct = ?', [/* id from $product belongs here */]);

        return view('products.single', ['products' => $product, 'subproducts' => $subProducts]);
    }

查看:

@foreach ($products as $product)
            <div class="col-md-4">
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">{{ $product->name }}</h3>
                    </div>
                    <!-- /.box-header -->
                    <div class="box-body">
                        <img src="{{ $product->img }}" alt="{{ $product->name }}" class="img-responsive">
                    </div>
                </div>
                <!-- /.box -->
            </div>
        @endforeach

另外,有没有更好的方法在 laravel 中进行第一次查询?因为我使用的是 foreach 循环,而只有 1 行。

提前谢谢你

【问题讨论】:

    标签: php mysql laravel


    【解决方案1】:

    您最好使用 Laravel Query Builder 进行数据库查询。

    在你使用的方法中,Laravel 返回一个 JSON 对象。它很难用于另一个查询(我在这里被困了一个星期)。

    $product = DB::table('products')->where('name','=','$name')->pluck('id');
    

    这会将所有 ID 作为数组返回。因此您可以将其用于 foreach 循环内的第二个查询。

    【讨论】:

    • 谢谢。但是,第一个查询中总是只返回 1 行。没有更好的方法吗?通过将 $product[0] 用于您的示例的第二行,它不起作用。
    • 为什么不用join操作来得到答案?
    【解决方案2】:

    控制器

    public function showSpecificProduct($name)
        {
            $product = Product::with('subproducts')->where('name', $name)->first();
    
            return view('products.single')->with('products',$product);
        }
    

    查看

    {{ $product->name }} // shows the name of the mainproduct no need for a foreach
    

    要显示子产品,您必须执行一个 foreach 循环(如果它们很多)

    @foreach (($product->subproducts as $subproduct)
    {{ $subproduct->name}}
    @endforeach
    

    要做到这一点,您必须首先在模型和迁移中设置关系

    喜欢这个

    1-迁移

    产品迁移

    public function up()
        {
            Schema::create('products', function (Blueprint $table) {
                $table->increments('id');
                $table->text('name');
                $table->timestamps();
            });
    }
    

    子产品迁移

    public function up()
        {
            Schema::create('subproducts', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('product_id')->unsigned();
                $table->foreign('product_id')->references('id')->on('subproducts');
                $table->text('name');
                $table->timestamps();
            });
    }
    

    如果您没有正确的外键设置,请刷新您的迁移

    2-模型

    将此添加到您的产品模型

    编辑

    public function subproducts() {
        return $this->hasMany('App\Subproduct', 'product_id'); // you can this
    }
    

    子产品模型

    public function product() { // i made a mistake here
        return $this->belongsTo('App\Product');
    }
    

    现在可以随意使用 Eloquent ORM。

    【讨论】:

    • 哇,很好的答案,谢谢。我完全忘记了模型......我已经有了迁移是一个简单的php artisan make:model,或者我之后需要做任何其他事情才能使用产品类Product::如果可能的话,你能详细说明一下吗? hasMany() 又来了?谢谢。
    • 当然可以,一个产品会有很多子产品对吧?所以你必须告诉 eloquent 该模型有很多子产品,所以当你使用我在控制器中向你展示的查询时,雄辩会弄清楚如何为你获取 mainproduct 的相关子产品(反之亦然),是的,你只需要创建如果您还没有模型(并且在控制器顶部不要忘记添加 use App\Product;
    • 快速提问。在“2-Models”下,您有 2 个名为 subproducts() 的函数。名字对吗?这样做后我也遇到了错误。 Column not found: 1054 Unknown column 'subproducts.products_id' in 'where clause' (SQL: select * from subproducts` where subproducts.products_id in (2))` 寻找它大约。 30分钟。 product_id 的 product 后面有一个 s。为什么?我在 db 中的字段称为 product_id,而您的子产品 up() 中的 coee 也没有 s...
    • 检查我的编辑,我犯了一个简单的错误,在您的子产品模型中将函数名称更改为产品,在产品模型中您可以指定外键名称作为第二个参数
    • hasMany 的倒数是belongsTo
    猜你喜欢
    • 2012-04-19
    • 2015-07-23
    • 1970-01-01
    • 2020-09-11
    • 2022-11-30
    • 1970-01-01
    • 2022-08-06
    • 2020-12-06
    • 1970-01-01
    相关资源
    最近更新 更多