Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 内部连接在拉威尔雄辩_Php_Laravel_Orm_Eloquent - Fatal编程技术网

Php 内部连接在拉威尔雄辩

Php 内部连接在拉威尔雄辩,php,laravel,orm,eloquent,Php,Laravel,Orm,Eloquent,我有一个有效的查询,但想知道是否有一个更优雅的方法。我有一张产品表和一张制造商表 这些关系如下: Manufacturer > hasMany > Product Product > belongsTo > Manufacturer 我的问题是: $products = \App\Product::join('manufacturers', 'products.manufacturer_id', '=', 'manufacturers.id')

我有一个有效的查询,但想知道是否有一个更优雅的方法。我有一张产品表和一张制造商表

这些关系如下:

Manufacturer > hasMany > Product
Product > belongsTo > Manufacturer
我的问题是:

$products = \App\Product::join('manufacturers', 
        'products.manufacturer_id', '=', 'manufacturers.id')
    ->where('manufacturers.name', 'like', $needle)
    ->orWhere('products.name', 'like', $needle);
是这样吗?有更好的办法吗?可能不需要在雄辩的模型中使用
join


谢谢。

在您的
产品
型号中创建关系:

public function manufacturer()
{
    return $this->belongsTo(Manufacturer::class, 'foreign_key');
}
然后你可以使用

$products = \App\Product::whereHas('manufacturer', function ($query) use ($needle) {
    $query->where('name', 'like', $needle);
})->orWhere('name', 'like', $needle);

在您的
产品
模型中创建关系:

public function manufacturer()
{
    return $this->belongsTo(Manufacturer::class, 'foreign_key');
}
然后你可以使用

$products = \App\Product::whereHas('manufacturer', function ($query) use ($needle) {
    $query->where('name', 'like', $needle);
})->orWhere('name', 'like', $needle);

你忘了针的封口。你忘了针的封口。