Php Laravel:控制器中有2个DB查询,而1个值取决于第一个查询
我有两个表,产品和子产品。 我还有一个视图,其中显示有关产品的信息(查看从数据库动态生成的数据)。在此视图中,应显示所有子管道。我该怎么做?因为我在视图中循环执行第一个查询,而不是在控制器中 控制器:Php Laravel:控制器中有2个DB查询,而1个值取决于第一个查询,php,mysql,laravel,Php,Mysql,Laravel,我有两个表,产品和子产品。 我还有一个视图,其中显示有关产品的信息(查看从数据库动态生成的数据)。在此视图中,应显示所有子管道。我该怎么做?因为我在视图中循环执行第一个查询,而不是在控制器中 控制器: public function showSpecificProduct($name) { $name = strtolower($name); $product = \DB::select('select * from products where name
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作为$product)
{{$product->name}
img}“alt=“{{$product->name}}”class=“img responsive”>
@endforeach
另外,有没有更好的方法来执行laravel中的第一个查询?因为我使用的是foreach循环,而只有一行
提前感谢您您最好使用进行数据库查询 在您使用的方法中,Laravel返回一个JSON对象,很难用于另一个查询(我在这里呆了一周)
这将以数组的形式返回所有ID。因此,您可以将其用于
foreach
循环中的第二个查询。控制器
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循环(如果有多个子管道)
要做到这一点,您必须首先在模型和迁移中设置关系
像这样
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();
});
}
子管道迁移
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 product() { // i made a mistake here
return $this->belongsTo('App\Product');
}
编辑
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');
}
现在可以随意使用雄辩的ORM了。谢谢。但是,在第一个查询中总是只返回一行。难道没有更好的方法吗?使用$product[0]对于示例的第二行,它不起作用。为什么不使用联接操作来获得答案呢?哇,非常好的答案,谢谢。我完全忘记了模型…我已经有了迁移,这是一个简单的
php artisan make:model
足够了,或者我需要在以后做任何其他事情才能使用产品类吗产品::
如果可能的话,你能详细说明一下什么有很多()是吗?谢谢。当然,好的,一个产品会有很多子产品,对吗?所以你必须告诉eloquent,该模型有很多子产品,所以当你使用我在controller中向你展示的查询时,eloquent会找出如何获得主产品的相关子产品(反之亦然)是的,如果您还没有模型,您只需要创建模型(并且在控制器顶部不要忘记添加use App\Product;
quick question.“2-models”下有两个名为subproducts()的函数。名称是否正确?执行此操作后,我还遇到一个错误。列未找到:1054未知列“where子句”中的“subproducts.products\u id”(SQL:select*from
subproducts`wheresubproducts
products\u idin(2))`查找时间约为30分钟。product_id中的product_后有一个s。为什么?数据库中的我的字段称为product_id,子产品up()中的coee也没有s…检查我的编辑,我犯了一个简单的错误,在子管道模型中将函数名称更改为product,在产品模型中,您可以将外键名称指定为第二个参数的倒数hasMany
是belongsTo