【发布时间】:2022-01-02 18:31:51
【问题描述】:
大家好,我正在尝试仅从“categorie_id”中随机获取数据,仅获取相同类别的产品,但它总是返回其他类别的产品 这是我在控制器中的功能
public function product_detail($id){
$produit = Produit::find($id);
$cavelepatricecat = Categorie::where("libelle","Cave le patrice")->get();
$cavelepatrice = [];
if($cavelepatricecat){
array_push($cavelepatrice,$cavelepatricecat[0]->id);
$cavelepatricesouscats = $cavelepatricecat[0]->sous_categories;
foreach($cavelepatricesouscats as $s_categories){
array_push($cavelepatrice,$s_categories->id);
}
}
$produits = Produit::whereNotIn('categorie_id', $cavelepatrice)->inRandomOrder()->take(4)->get();
//$produits = Produit::select('categorie_id', 'image','nom')->inRandomOrder()->take(4)->get();
//$produits = Produit::select('categorie_id','image')->get()->random(4);
return view("store.detail")->with("prod",$produit)->with("produit",$produits);
}
这是餐桌产品型号
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Produit extends Model
{
use HasFactory;
protected $fillable = [
'nom', 'prix', 'prixpromo', 'detals', 'image','categorie_id','user_id'
];
public function favories()
{
return $this->belongsToMany('App\Models\Favorie');
}
public function paniers()
{
return $this->belongsToMany('App\Models\Panier');
}
public function commandes()
{
return $this->belongsToMany('App\Models\Commande');
}
public function categories(){
return $this->belongsTo('App\Models\Categorie','categorie_id');
}
}
【问题讨论】:
-
您想获得相同类别的产品并且您使用了
whereNotIn()。你需要使用whereIn()
标签: php laravel eloquent laravel-8