仅显示 Laravel 5.8 中的最新产品类别
Show only the latest category of product in Laravel 5.8
我有三个模型:产品、类别、product_category。一个产品可能有很多类别,有些类别可能有一个父类别。您可以在下面找到相关关系。我可以用 foreach()
循环显示所有类别,但我不想那样做。我只想显示类别,而且必须是最新的。有没有办法只显示当前产品父类别的最后一个类别?
<div class="row">
@foreach ($products as $product)
@foreach ($product->categories as $cat)
{{ $cat->title }}
@endforeach
@endforeach
</div>
产品型号
public function categories()
{
return $this->belongsToMany(Category::class);
}
类别模型
public function products()
{
return $this->belongsToMany(Product::class);
}
public function subcategories()
{
return $this->hasMany('\App\Category', 'parent_id');
}
public function parent()
{
return $this->belongsTo('\App\Category', 'parent_id');
}
类别架构
$table->increments('id');
$table->string('title');
$table->integer('parent_id')->nullable();
Category_product 架构
$table->increments('id');
$table->integer('category_id')->unsigned();
$table->integer('product_id')->unsigned();
您可以像这样在关系上使用查询生成器:
$product->categories->orderBy('id', 'desc')->first();
这将从最近到最旧的顺序排列,您可以只取第一条记录。
您可以简单地使用
$product->categories()->orderBy('id', 'DESC')->first();
或者你也可以试试
public function categories()
{
return $this->belongsToMany(Category::class)->orderBy('id','DESC');
}
然后你可以使用
$product->categories()->first();
可以使用latest()
方法
$product->categories->latest()->first();
或者,
$product->categories->orderBy('id', 'desc')->first();
在您的模型上使用特定方法
您的类别定义应该是这样的,对吗?
public function categories() {
return $this->belongsToMany(Category::class);
}
所以,只需创建另一种方法来仅检索最新的。
public function latestCategory() {
return $this->categories()->latest();
}
如果您需要最旧的类别,则应用相同的方法。
public function oldestCategory() {
return $this->categories()->oldest();
}
仅使用类别关系定义
如果您不打算创建新方法,您也可以采用不同的方法:
$this->categories()->latest();
或
$this->categories()->oldest();
希望对您有所帮助。
祝你有美好的一天。
我有三个模型:产品、类别、product_category。一个产品可能有很多类别,有些类别可能有一个父类别。您可以在下面找到相关关系。我可以用 foreach()
循环显示所有类别,但我不想那样做。我只想显示类别,而且必须是最新的。有没有办法只显示当前产品父类别的最后一个类别?
<div class="row">
@foreach ($products as $product)
@foreach ($product->categories as $cat)
{{ $cat->title }}
@endforeach
@endforeach
</div>
产品型号
public function categories()
{
return $this->belongsToMany(Category::class);
}
类别模型
public function products()
{
return $this->belongsToMany(Product::class);
}
public function subcategories()
{
return $this->hasMany('\App\Category', 'parent_id');
}
public function parent()
{
return $this->belongsTo('\App\Category', 'parent_id');
}
类别架构
$table->increments('id');
$table->string('title');
$table->integer('parent_id')->nullable();
Category_product 架构
$table->increments('id');
$table->integer('category_id')->unsigned();
$table->integer('product_id')->unsigned();
您可以像这样在关系上使用查询生成器:
$product->categories->orderBy('id', 'desc')->first();
这将从最近到最旧的顺序排列,您可以只取第一条记录。
您可以简单地使用
$product->categories()->orderBy('id', 'DESC')->first();
或者你也可以试试
public function categories()
{
return $this->belongsToMany(Category::class)->orderBy('id','DESC');
}
然后你可以使用
$product->categories()->first();
可以使用latest()
方法
$product->categories->latest()->first();
或者,
$product->categories->orderBy('id', 'desc')->first();
在您的模型上使用特定方法
您的类别定义应该是这样的,对吗?
public function categories() {
return $this->belongsToMany(Category::class);
}
所以,只需创建另一种方法来仅检索最新的。
public function latestCategory() {
return $this->categories()->latest();
}
如果您需要最旧的类别,则应用相同的方法。
public function oldestCategory() {
return $this->categories()->oldest();
}
仅使用类别关系定义
如果您不打算创建新方法,您也可以采用不同的方法:
$this->categories()->latest();
或
$this->categories()->oldest();
希望对您有所帮助。 祝你有美好的一天。