【问题标题】:Laravel show random from only one columnLaravel 仅从一列随机显示
【发布时间】: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


【解决方案1】:

根据您的要求,您需要使用whereIn() 而不是whereNotIn()

$produits = Produit::whereIn('categorie_id', $cavelepatrice)->inRandomOrder()->take(4)->get();

【讨论】:

  • 问题是在这种情况下它只返回cavelepatrice类别而不是我试图返回与我选择的产品相同的类别我//$produits = Produit::whereIn('categorie_id')-&gt;inRandomOrder()-&gt;take(4)-&gt;get(); $produits = Produit::inRandomOrder()-&gt;take(4)-&gt;get(); //$produits = Produit::select('categorie_id', 'image','nom')-&gt;inRandomOrder()-&gt;take(4)-&gt;get(); //$produits = Produit::select('categorie_id','image')-&gt;get()-&gt;random(4);
  • @SalamahSalamah 你能告诉我数据库表 Produit Model(列及其与类别模型的关系)
  • 我已将模型添加到问题中,谢谢
【解决方案2】:

谢谢@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);   
    }

【讨论】:

    猜你喜欢
    • 2021-12-23
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多