Laravel 仅从一列中随机显示
Laravel show random from only one column
大家好,我正在尝试仅从“categorie_id”中随机获取数据,只获取相同类别的产品,但它总是 return 来自其他类别的产品
这是我在控制器中的功能
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);
}
这是 table 产品型号
<?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');
}
}
根据您的要求,您需要使用 whereIn()
而不是 whereNotIn()
$produits = Produit::whereIn('categorie_id', $cavelepatrice)->inRandomOrder()->take(4)->get();
谢谢@leena patela 我找到了解决方案
public function product_detail($id){
$produit = Produit::find($id);
$categorie_id = $produit->categorie_id;
$produits = Produit::where('categorie_id', $categorie_id)->inRandomOrder()->take(4)->get();
return view("store.detail")->with("prod",$produit)->with('produit',$produits);
}
大家好,我正在尝试仅从“categorie_id”中随机获取数据,只获取相同类别的产品,但它总是 return 来自其他类别的产品 这是我在控制器中的功能
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);
}
这是 table 产品型号
<?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');
}
}
根据您的要求,您需要使用 whereIn()
而不是 whereNotIn()
$produits = Produit::whereIn('categorie_id', $cavelepatrice)->inRandomOrder()->take(4)->get();
谢谢@leena patela 我找到了解决方案
public function product_detail($id){
$produit = Produit::find($id);
$categorie_id = $produit->categorie_id;
$produits = Produit::where('categorie_id', $categorie_id)->inRandomOrder()->take(4)->get();
return view("store.detail")->with("prod",$produit)->with('produit',$produits);
}