加入 Laravel Eloquent
Joins with Laravel Eloquent
我正在学习一些来自 Symfony 的 Laravel,我对连接如何与 eloquent 一起工作感到有点困惑。这样我就有了返回的对象作为我的模型 class 而不是 stdClass
.
目前我正在这样做:
$query = DB::table('caravan')
->join('type','caravan.type_id', '=', 'type.id')
->join('category','caravan.category_id', '=', 'category.id')
->where('type.name','=', 'New')
->where('category.name', '=', ucwords(strtolower($category)))
->orderBy($orderBy,$order);
这行得通,它通过连接的列显示了所有正确的记录,但我不希望它成为 stdClass
。
我的大篷车模型是这样的:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
/**
* Class Caravan
* @package App\Models
* @property int $stock
* @property int $branch_id
* @property int $category_id
* @property int $type_id
* @property string $reg
* @property string $make
* @property string $model
* @property string $specification
* @property string $derivative
* @property string $engine_size
* @property string $engine_type
* @property string $transmission
* @property string $colour
* @property int $year
* @property int $mileage
* @property boolean $commercial
* @property double $sales_siv
* @property double $retail
* @property double $web_price
* @property string $sub_heading;
* @property string $advertising_notes
* @property string $manager_comments
* @property double $previous_price
* @property double $guide_retail_price
* @property boolean $available_for_sale
* @property boolean $advertised_on_own_website
* @property int $berths
* @property int $axles
* @property string $layout_type
* @property double $width
* @property double $length
* @property double $height
* @property int $kimberley_unit_id
* @property \DateTime $kimberley_date_updated
*
*/
class Caravan extends Model
{
use HasFactory;
/**
* @var string
*/
public $table = 'caravan';
/**
* @var string[]
*/
protected $casts = [
'kimberley_date_updated' => 'datetime:Y-m-d H:i:s',
'created_at' => 'datetime:Y-m-d H:i:s',
'updated_at' => 'datetime:Y-m-d H:i:s',
'web_price' => 'decimal: 2',
'previous_price' => 'decimal: 2'
];
/**
* @return HasOne
*/
public function category() : HasOne
{
return $this->hasOne(Category::class, 'id', 'category_id');
}
/**
* @return HasOne
*/
public function type() : HasOne
{
die("DD");
return $this->hasOne(Type::class, 'id', 'type_id');
}
/**
* @return int
*/
public function getStock(): int
{
return $this->stock;
}
/**
* @param int $stock
* @return $this
*/
public function setStock(int $stock): self
{
$this->stock = $stock;
return $this;
}
/**
* @return int
*/
public function getBranchId(): int
{
return $this->branch_id;
}
/**
* @param int $branch_id
* @return $this
*/
public function setBranchId(int $branch_id): self
{
$this->branch_id = $branch_id;
return $this;
}
/**
* @param Branch $branch
* @return $this
*/
public function setBranch(Branch $branch) : self
{
$this->branch_id = $branch->id;
return $this;
}
/**
* @return int
*/
public function getCategoryId(): int
{
return $this->category_id;
}
/**
* @param int $category_id
* @return $this
*/
public function setCategoryId(int $category_id): self
{
$this->category_id = $category_id;
return $this;
}
/**
* @param Category $cat
* @return $this
*/
public function setCategory(Category $cat) : self
{
$this->category_id = $cat->id;
return $this;
}
/**
* @return int
*/
public function getTypeId(): int
{
return $this->type_id;
}
/**
* @param int $type_id
* @return $this
*/
public function setTypeId(int $type_id): self
{
$this->type_id = $type_id;
return $this;
}
/**
* @param Type $type
* @return $this
*/
public function setType(Type $type) : self
{
$this->type_id = $type->id;
return $this;
}
/**
* @return string
*/
public function getReg(): string
{
return $this->reg;
}
/**
* @param string $reg
* @return $this
*/
public function setReg(string $reg): self
{
$this->reg = $reg;
return $this;
}
/**
* @return string
*/
public function getMake(): string
{
return $this->make;
}
/**
* @param string $make
* @return $this
*/
public function setMake(string $make): self
{
$this->make = $make;
return $this;
}
/**
* @return string
*/
public function getModel(): string
{
return $this->model;
}
/**
* @param string $model
* @return $this
*/
public function setModel(string $model): self
{
$this->model = $model;
return $this;
}
/**
* @return string
*/
public function getSpecification(): string
{
return $this->specification;
}
/**
* @param string $specification
* @return $this
*/
public function setSpecification(string $specification): self
{
$this->specification = $specification;
return $this;
}
/**
* @return string
*/
public function getDerivative(): string
{
return $this->derivative;
}
/**
* @param string $derivative
* @return $this
*/
public function setDerivative(string $derivative): self
{
$this->derivative = $derivative;
return $this;
}
/**
* @return string
*/
public function getEngineSize(): string
{
return $this->engine_size;
}
/**
* @param string $engine_size
* @return $this
*/
public function setEngineSize(string $engine_size): self
{
$this->engine_size = $engine_size;
return $this;
}
/**
* @return string
*/
public function getEngineType(): string
{
return $this->engine_type;
}
/**
* @param string $engine_type
* @return $this
*/
public function setEngineType(string $engine_type): self
{
$this->engine_type = $engine_type;
return $this;
}
/**
* @return string
*/
public function getTransmission(): string
{
return $this->transmission;
}
/**
* @param string $transmission
* @return $this
*/
public function setTransmission(string $transmission): self
{
$this->transmission = $transmission;
return $this;
}
/**
* @return string
*/
public function getColour(): string
{
return $this->colour;
}
/**
* @param string $colour
* @return $this
*/
public function setColour(string $colour): self
{
$this->colour = $colour;
return $this;
}
/**
* @return int
*/
public function getYear(): int
{
return $this->year;
}
/**
* @param int $year
* @return $this
*/
public function setYear(int $year): self
{
$this->year = $year;
return $this;
}
/**
* @return int
*/
public function getMileage(): int
{
return $this->mileage;
}
/**
* @param int $mileage
* @return $this
*/
public function setMileage(int $mileage): self
{
$this->mileage = $mileage;
return $this;
}
/**
* @return bool
*/
public function isCommercial(): bool
{
return (bool)$this->commercial;
}
/**
* @param bool $commercial
* @return $this
*/
public function setCommercial(bool $commercial): self
{
$this->commercial = (int)$commercial;
return $this;
}
/**
* @return float
*/
public function getSalesSiv(): float
{
return $this->sales_siv;
}
/**
* @param float|int $sales_siv
* @return $this
*/
public function setSalesSiv(float $sales_siv = 0): self
{
$this->sales_siv = $sales_siv;
return $this;
}
/**
* @return float
*/
public function getRetail(): float
{
return $this->retail;
}
/**
* @param float|int $retail
* @return $this
*/
public function setRetail(float $retail = 0): self
{
$this->retail = $retail;
return $this;
}
/**
* @return float
*/
public function getWebPrice(): float
{
return $this->web_price;
}
/**
* @param float|int $web_price
* @return $this
*/
public function setWebPrice(float $web_price = 0): self
{
$this->web_price = $web_price;
return $this;
}
/**
* @return string
*/
public function getSubHeading(): string
{
return $this->sub_heading;
}
/**
* @param string $sub_heading
* @return $this
*/
public function setSubHeading(string $sub_heading): self
{
$this->sub_heading = $sub_heading;
return $this;
}
/**
* @return string
*/
public function getAdvertisingNotes(): string
{
return $this->advertising_notes;
}
/**
* @param string $advertising_notes
* @return $this
*/
public function setAdvertisingNotes(string $advertising_notes): self
{
$this->advertising_notes = $advertising_notes;
return $this;
}
/**
* @return string
*/
public function getManagerComments(): string
{
return $this->manager_comments;
}
/**
* @param string $managerComments
* @return $this
*/
public function setManagerComments(string $managerComments): self
{
$this->manager_comments = $managerComments;
return $this;
}
/**
* @return float
*/
public function getPreviousPrice(): float
{
return $this->previous_price;
}
/**
* @param float $previous_price
* @return $this
*/
public function setPreviousPrice(float $previous_price): self
{
$this->previous_price = $previous_price;
return $this;
}
/**
* @return float
*/
public function getGuideRetailPrice(): float
{
return $this->guide_retail_price;
}
/**
* @param float $guide_retail_price
* @return $this
*/
public function setGuideRetailPrice(float $guide_retail_price): self
{
$this->guide_retail_price = $guide_retail_price;
return $this;
}
/**
* @return bool
*/
public function isAvailableForSale(): bool
{
return $this->available_for_sale;
}
/**
* @param bool $available_for_sale
* @return $this
*/
public function setAvailableForSale(bool $available_for_sale): self
{
$this->available_for_sale = (int)$available_for_sale;
return $this;
}
/**
* @return bool
*/
public function isAdvertisedOnOwnWebsite(): bool
{
return $this->advertised_on_own_website;
}
/**
* @param bool $advertised_on_own_website
* @return $this
*/
public function setAdvertisedOnOwnWebsite(bool $advertised_on_own_website): self
{
$this->advertised_on_own_website = (int)$advertised_on_own_website;
return $this;
}
/**
* @return int
*/
public function getBerths(): int
{
return $this->berths;
}
/**
* @param int $berth
*/
public function setBerths(int $berths): self
{
$this->berths = $berths;
return $this;
}
/**
* @return int
*/
public function getAxles(): int
{
return $this->axles;
}
/**
* @param int $axles
* @return $this
*/
public function setAxles(int $axles): self
{
$this->axles = $axles;
return $this;
}
/**
* @return string
*/
public function getLayoutType(): string
{
return $this->layout_type;
}
/**
* @param string $layout_type
* @return $this
*/
public function setLayoutType(string $layout_type): self
{
$this->layout_type = $layout_type;
return $this;
}
/**
* @return float
*/
public function getWidth(): float
{
return $this->width;
}
/**
* @param float $width
* @return $this
*/
public function setWidth(float $width): self
{
$this->width = $width;
return $this;
}
/**
* @return float
*/
public function getLength(): float
{
return $this->length;
}
/**
* @param float $length
* @return $this
*/
public function setLength(float $length): self
{
$this->length = $length;
return $this;
}
/**
* @return float
*/
public function getHeight(): float
{
return $this->height;
}
/**
* @param float $height
* @return $this
*/
public function setHeight(float $height): self
{
$this->height = $height;
return $this;
}
/**
* @return int
*/
public function getKimberleyUnitId(): int
{
return $this->kimberley_unit_id;
}
/**
* @param int $kimberley_unit_id
* @return $this
*/
public function setKimberleyUnitId(int $kimberley_unit_id): self
{
$this->kimberley_unit_id = $kimberley_unit_id;
return $this;
}
/**
* @return \DateTime
*/
public function getKimberleyDateUpdated(): \DateTime
{
return $this->kimberley_date_updated;
}
/**
* @param \DateTime $kimberley_date_updated
* @return $this
*/
public function setKimberleyDateUpdated(\DateTime $kimberley_date_updated): self
{
$this->kimberley_date_updated = $kimberley_date_updated;
return $this;
}
}
所以我在顶部有 HasOne
部分,以便能够使用模型获取 type
和 category
,但是我将如何执行此操作来替换我的查询上面有 where 子句?
我试过类似的方法:
Caravan::with(['type','category'])->where('type.name','New');
这个不行,就是说type
不存在那么多字
所以我想要返回的是模型,就像我 运行 说 Caravan::all()
然后我可以循环并使用类似 $caravan->type->name
编辑
Wahyu 的以下回答:
Caravan::with(['type' => function ($query) {
$query->where('name', 'New');
}, 'category' => function($query) use ($category) {
$query->where('name',ucwords(strtolower($category)));
}])->orderBy($orderBy,$order);
此查询运行良好,但是,它会在类型为 Used 和 New 的地方返回结果,因此查询无法正常工作。
此外,循环结果,我仍然无法在我的模型中使用 HasOne 方法来做 $caravan->type->name
但是,Caravan::all()
作为测试允许我在循环中使用 $caravan->type->name
并且工作正常,所以 where 查询仍然是一个问题
您可以通过将关系数组传递给 with
方法来完成,其中数组键是关系名称,数组值是向预加载查询添加额外约束的闭包:
Caravan::with(['type' => function ($query) {
$query->where('name', 'New');
}, 'category'])->get();
更新(不同的问题):
i still can't use the HasOne
method in my model to do $caravan->type->name
public function type() : HasOne
{
return $this->hasOne(Type::class, 'id', 'type_id');
}
因此,您可以从 Caravan
模型访问 Type
模型。您可以使用 belongsTo
方法定义 hasOne
关系的 inverse:
public function type()
{
return $this->belongsTo(Type::class);
}
调用 type
方法时,Eloquent 将尝试查找具有 id
且与 type_id
列匹配的 Type
模型Caravan
型号。
如果您使用的是 Laravel 的最新版本,您可以执行以下操作:
Caravan::query()
->whereRelation('type', 'name', 'New')
->whereRelation('category', 'name', ucwords(strtolower($category)))
->orderBy($orderBy, $order)
->get();
否则,您可以使用eloquent whereHas()方法达到相同的结果。
Caravan::query()
->whereHas('type', fn (Builder $query) => $query->where('name', 'New'))
->whereHas('category', fn (Builder $query) => $query->where('name', ucwords(strtolower($category))))
->orderBy($orderBy, $order)
->get();
看你的模型关系我认为你的关系应该是one to many
而不是one to one
。如果是这样,您的关系应该是:
public function type()
{
return $this->belongsTo(Type::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
我正在学习一些来自 Symfony 的 Laravel,我对连接如何与 eloquent 一起工作感到有点困惑。这样我就有了返回的对象作为我的模型 class 而不是 stdClass
.
目前我正在这样做:
$query = DB::table('caravan')
->join('type','caravan.type_id', '=', 'type.id')
->join('category','caravan.category_id', '=', 'category.id')
->where('type.name','=', 'New')
->where('category.name', '=', ucwords(strtolower($category)))
->orderBy($orderBy,$order);
这行得通,它通过连接的列显示了所有正确的记录,但我不希望它成为 stdClass
。
我的大篷车模型是这样的:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
/**
* Class Caravan
* @package App\Models
* @property int $stock
* @property int $branch_id
* @property int $category_id
* @property int $type_id
* @property string $reg
* @property string $make
* @property string $model
* @property string $specification
* @property string $derivative
* @property string $engine_size
* @property string $engine_type
* @property string $transmission
* @property string $colour
* @property int $year
* @property int $mileage
* @property boolean $commercial
* @property double $sales_siv
* @property double $retail
* @property double $web_price
* @property string $sub_heading;
* @property string $advertising_notes
* @property string $manager_comments
* @property double $previous_price
* @property double $guide_retail_price
* @property boolean $available_for_sale
* @property boolean $advertised_on_own_website
* @property int $berths
* @property int $axles
* @property string $layout_type
* @property double $width
* @property double $length
* @property double $height
* @property int $kimberley_unit_id
* @property \DateTime $kimberley_date_updated
*
*/
class Caravan extends Model
{
use HasFactory;
/**
* @var string
*/
public $table = 'caravan';
/**
* @var string[]
*/
protected $casts = [
'kimberley_date_updated' => 'datetime:Y-m-d H:i:s',
'created_at' => 'datetime:Y-m-d H:i:s',
'updated_at' => 'datetime:Y-m-d H:i:s',
'web_price' => 'decimal: 2',
'previous_price' => 'decimal: 2'
];
/**
* @return HasOne
*/
public function category() : HasOne
{
return $this->hasOne(Category::class, 'id', 'category_id');
}
/**
* @return HasOne
*/
public function type() : HasOne
{
die("DD");
return $this->hasOne(Type::class, 'id', 'type_id');
}
/**
* @return int
*/
public function getStock(): int
{
return $this->stock;
}
/**
* @param int $stock
* @return $this
*/
public function setStock(int $stock): self
{
$this->stock = $stock;
return $this;
}
/**
* @return int
*/
public function getBranchId(): int
{
return $this->branch_id;
}
/**
* @param int $branch_id
* @return $this
*/
public function setBranchId(int $branch_id): self
{
$this->branch_id = $branch_id;
return $this;
}
/**
* @param Branch $branch
* @return $this
*/
public function setBranch(Branch $branch) : self
{
$this->branch_id = $branch->id;
return $this;
}
/**
* @return int
*/
public function getCategoryId(): int
{
return $this->category_id;
}
/**
* @param int $category_id
* @return $this
*/
public function setCategoryId(int $category_id): self
{
$this->category_id = $category_id;
return $this;
}
/**
* @param Category $cat
* @return $this
*/
public function setCategory(Category $cat) : self
{
$this->category_id = $cat->id;
return $this;
}
/**
* @return int
*/
public function getTypeId(): int
{
return $this->type_id;
}
/**
* @param int $type_id
* @return $this
*/
public function setTypeId(int $type_id): self
{
$this->type_id = $type_id;
return $this;
}
/**
* @param Type $type
* @return $this
*/
public function setType(Type $type) : self
{
$this->type_id = $type->id;
return $this;
}
/**
* @return string
*/
public function getReg(): string
{
return $this->reg;
}
/**
* @param string $reg
* @return $this
*/
public function setReg(string $reg): self
{
$this->reg = $reg;
return $this;
}
/**
* @return string
*/
public function getMake(): string
{
return $this->make;
}
/**
* @param string $make
* @return $this
*/
public function setMake(string $make): self
{
$this->make = $make;
return $this;
}
/**
* @return string
*/
public function getModel(): string
{
return $this->model;
}
/**
* @param string $model
* @return $this
*/
public function setModel(string $model): self
{
$this->model = $model;
return $this;
}
/**
* @return string
*/
public function getSpecification(): string
{
return $this->specification;
}
/**
* @param string $specification
* @return $this
*/
public function setSpecification(string $specification): self
{
$this->specification = $specification;
return $this;
}
/**
* @return string
*/
public function getDerivative(): string
{
return $this->derivative;
}
/**
* @param string $derivative
* @return $this
*/
public function setDerivative(string $derivative): self
{
$this->derivative = $derivative;
return $this;
}
/**
* @return string
*/
public function getEngineSize(): string
{
return $this->engine_size;
}
/**
* @param string $engine_size
* @return $this
*/
public function setEngineSize(string $engine_size): self
{
$this->engine_size = $engine_size;
return $this;
}
/**
* @return string
*/
public function getEngineType(): string
{
return $this->engine_type;
}
/**
* @param string $engine_type
* @return $this
*/
public function setEngineType(string $engine_type): self
{
$this->engine_type = $engine_type;
return $this;
}
/**
* @return string
*/
public function getTransmission(): string
{
return $this->transmission;
}
/**
* @param string $transmission
* @return $this
*/
public function setTransmission(string $transmission): self
{
$this->transmission = $transmission;
return $this;
}
/**
* @return string
*/
public function getColour(): string
{
return $this->colour;
}
/**
* @param string $colour
* @return $this
*/
public function setColour(string $colour): self
{
$this->colour = $colour;
return $this;
}
/**
* @return int
*/
public function getYear(): int
{
return $this->year;
}
/**
* @param int $year
* @return $this
*/
public function setYear(int $year): self
{
$this->year = $year;
return $this;
}
/**
* @return int
*/
public function getMileage(): int
{
return $this->mileage;
}
/**
* @param int $mileage
* @return $this
*/
public function setMileage(int $mileage): self
{
$this->mileage = $mileage;
return $this;
}
/**
* @return bool
*/
public function isCommercial(): bool
{
return (bool)$this->commercial;
}
/**
* @param bool $commercial
* @return $this
*/
public function setCommercial(bool $commercial): self
{
$this->commercial = (int)$commercial;
return $this;
}
/**
* @return float
*/
public function getSalesSiv(): float
{
return $this->sales_siv;
}
/**
* @param float|int $sales_siv
* @return $this
*/
public function setSalesSiv(float $sales_siv = 0): self
{
$this->sales_siv = $sales_siv;
return $this;
}
/**
* @return float
*/
public function getRetail(): float
{
return $this->retail;
}
/**
* @param float|int $retail
* @return $this
*/
public function setRetail(float $retail = 0): self
{
$this->retail = $retail;
return $this;
}
/**
* @return float
*/
public function getWebPrice(): float
{
return $this->web_price;
}
/**
* @param float|int $web_price
* @return $this
*/
public function setWebPrice(float $web_price = 0): self
{
$this->web_price = $web_price;
return $this;
}
/**
* @return string
*/
public function getSubHeading(): string
{
return $this->sub_heading;
}
/**
* @param string $sub_heading
* @return $this
*/
public function setSubHeading(string $sub_heading): self
{
$this->sub_heading = $sub_heading;
return $this;
}
/**
* @return string
*/
public function getAdvertisingNotes(): string
{
return $this->advertising_notes;
}
/**
* @param string $advertising_notes
* @return $this
*/
public function setAdvertisingNotes(string $advertising_notes): self
{
$this->advertising_notes = $advertising_notes;
return $this;
}
/**
* @return string
*/
public function getManagerComments(): string
{
return $this->manager_comments;
}
/**
* @param string $managerComments
* @return $this
*/
public function setManagerComments(string $managerComments): self
{
$this->manager_comments = $managerComments;
return $this;
}
/**
* @return float
*/
public function getPreviousPrice(): float
{
return $this->previous_price;
}
/**
* @param float $previous_price
* @return $this
*/
public function setPreviousPrice(float $previous_price): self
{
$this->previous_price = $previous_price;
return $this;
}
/**
* @return float
*/
public function getGuideRetailPrice(): float
{
return $this->guide_retail_price;
}
/**
* @param float $guide_retail_price
* @return $this
*/
public function setGuideRetailPrice(float $guide_retail_price): self
{
$this->guide_retail_price = $guide_retail_price;
return $this;
}
/**
* @return bool
*/
public function isAvailableForSale(): bool
{
return $this->available_for_sale;
}
/**
* @param bool $available_for_sale
* @return $this
*/
public function setAvailableForSale(bool $available_for_sale): self
{
$this->available_for_sale = (int)$available_for_sale;
return $this;
}
/**
* @return bool
*/
public function isAdvertisedOnOwnWebsite(): bool
{
return $this->advertised_on_own_website;
}
/**
* @param bool $advertised_on_own_website
* @return $this
*/
public function setAdvertisedOnOwnWebsite(bool $advertised_on_own_website): self
{
$this->advertised_on_own_website = (int)$advertised_on_own_website;
return $this;
}
/**
* @return int
*/
public function getBerths(): int
{
return $this->berths;
}
/**
* @param int $berth
*/
public function setBerths(int $berths): self
{
$this->berths = $berths;
return $this;
}
/**
* @return int
*/
public function getAxles(): int
{
return $this->axles;
}
/**
* @param int $axles
* @return $this
*/
public function setAxles(int $axles): self
{
$this->axles = $axles;
return $this;
}
/**
* @return string
*/
public function getLayoutType(): string
{
return $this->layout_type;
}
/**
* @param string $layout_type
* @return $this
*/
public function setLayoutType(string $layout_type): self
{
$this->layout_type = $layout_type;
return $this;
}
/**
* @return float
*/
public function getWidth(): float
{
return $this->width;
}
/**
* @param float $width
* @return $this
*/
public function setWidth(float $width): self
{
$this->width = $width;
return $this;
}
/**
* @return float
*/
public function getLength(): float
{
return $this->length;
}
/**
* @param float $length
* @return $this
*/
public function setLength(float $length): self
{
$this->length = $length;
return $this;
}
/**
* @return float
*/
public function getHeight(): float
{
return $this->height;
}
/**
* @param float $height
* @return $this
*/
public function setHeight(float $height): self
{
$this->height = $height;
return $this;
}
/**
* @return int
*/
public function getKimberleyUnitId(): int
{
return $this->kimberley_unit_id;
}
/**
* @param int $kimberley_unit_id
* @return $this
*/
public function setKimberleyUnitId(int $kimberley_unit_id): self
{
$this->kimberley_unit_id = $kimberley_unit_id;
return $this;
}
/**
* @return \DateTime
*/
public function getKimberleyDateUpdated(): \DateTime
{
return $this->kimberley_date_updated;
}
/**
* @param \DateTime $kimberley_date_updated
* @return $this
*/
public function setKimberleyDateUpdated(\DateTime $kimberley_date_updated): self
{
$this->kimberley_date_updated = $kimberley_date_updated;
return $this;
}
}
所以我在顶部有 HasOne
部分,以便能够使用模型获取 type
和 category
,但是我将如何执行此操作来替换我的查询上面有 where 子句?
我试过类似的方法:
Caravan::with(['type','category'])->where('type.name','New');
这个不行,就是说type
不存在那么多字
所以我想要返回的是模型,就像我 运行 说 Caravan::all()
然后我可以循环并使用类似 $caravan->type->name
编辑
Wahyu 的以下回答:
Caravan::with(['type' => function ($query) {
$query->where('name', 'New');
}, 'category' => function($query) use ($category) {
$query->where('name',ucwords(strtolower($category)));
}])->orderBy($orderBy,$order);
此查询运行良好,但是,它会在类型为 Used 和 New 的地方返回结果,因此查询无法正常工作。
此外,循环结果,我仍然无法在我的模型中使用 HasOne 方法来做 $caravan->type->name
但是,Caravan::all()
作为测试允许我在循环中使用 $caravan->type->name
并且工作正常,所以 where 查询仍然是一个问题
您可以通过将关系数组传递给 with
方法来完成,其中数组键是关系名称,数组值是向预加载查询添加额外约束的闭包:
Caravan::with(['type' => function ($query) {
$query->where('name', 'New');
}, 'category'])->get();
更新(不同的问题):
i still can't use the
HasOne
method in my model to do$caravan->type->name
public function type() : HasOne
{
return $this->hasOne(Type::class, 'id', 'type_id');
}
因此,您可以从 Caravan
模型访问 Type
模型。您可以使用 belongsTo
方法定义 hasOne
关系的 inverse:
public function type()
{
return $this->belongsTo(Type::class);
}
调用 type
方法时,Eloquent 将尝试查找具有 id
且与 type_id
列匹配的 Type
模型Caravan
型号。
如果您使用的是 Laravel 的最新版本,您可以执行以下操作:
Caravan::query()
->whereRelation('type', 'name', 'New')
->whereRelation('category', 'name', ucwords(strtolower($category)))
->orderBy($orderBy, $order)
->get();
否则,您可以使用eloquent whereHas()方法达到相同的结果。
Caravan::query()
->whereHas('type', fn (Builder $query) => $query->where('name', 'New'))
->whereHas('category', fn (Builder $query) => $query->where('name', ucwords(strtolower($category))))
->orderBy($orderBy, $order)
->get();
看你的模型关系我认为你的关系应该是one to many
而不是one to one
。如果是这样,您的关系应该是:
public function type()
{
return $this->belongsTo(Type::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}