亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel Eloquent如何根據類別id獲取所有產品

Laravel Eloquent如何根據類別id獲取所有產品

PHP
慕工程0101907 2023-12-15 17:05:05
用戶產品控制器class UserProductsController extends Controller{    public function index()    {        $products = Product::get();        return view ('products')->with(compact('products'));          }          public function product_categories()    {        $categories = Category::all();        return view ('categories')->with(compact('categories'));          }  }產品表 public function up()    {        Schema::create('products', function (Blueprint $table) {            $table->bigIncrements('id');            $table->string('prod_name');            $table->string('prod_brand')->nullable();            $table->unsignedBigInteger('cat_id');            $table->string('prod_image_path')->nullable();            $table->timestamps();            $table->foreign('cat_id')            ->references('id')            ->on('categories')            ->onDelete('cascade');        });    }類別表   public function up()        {            Schema::create('categories', function (Blueprint $table) {                $table->bigIncrements('id');                $table->string('cat_name');                $table->string('cat_image_path')->nullable();                $table->string('cat_description')->nullable();                $table->timestamps();            });        }產品型號class Product extends Model{      public function category()    {        return $this->belongsTo('App\Category','category_id');    }     }類別型號class Category extends Model{   public function category()   {       return $this->hasMany('App\Product');   }}我的路線是Route::get('/All_Products', 'UserProductsController@index')->name('All_Products');Route::get('/product_categories', 'UserProductsController@product_categories')->name('categories');如何獲得同一類別的所有產品?由于這是我的第一個項目,我在這方面花費了更多的時間。但對我來說沒有任何作用。有人可以指導我嗎?
查看完整描述

4 回答

?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

假設你正確設置了你的關系(但事實并非如此)


您可以通過以下幾種方式使用 Eloquent:


$products = Category::findOrFail($categoryId)->products;


$products = Product::where('category_id', $categoryId)->get();


$products = Product::whereHas('category', function ($query) use ($categoryId) {

? ? $q->where('id', $categoryId);

})->get();


查看完整回答
反對 回復 2023-12-15
?
森欄

TA貢獻1810條經驗 獲得超5個贊

首先,你沒有正確定義你們的關系。它應該是這樣的:


class Product extends Model

{

    public function category()

    {

        return $this->belongsTo('App\Category');

    }

}

class Category extends Model

{

   public function products()

   {

       return $this->hasMany('App\Product');

   }

}

然后在您的產品遷移文件中,cat_id 應重命名為category_id。這樣,您就不需要在關系上指定外鍵。


我假設您想列出屬于特定類別的所有產品。您可以使用路由模型綁定輕松地做到這一點。在這種情況下,您的路線應類似于:


Route::get('categories/{category:id}/products', [CategoryController::class, 'products']);

然后在你的控制器中:


use App\Category;


class CategoryController extends Controller

{

    public function products(Category $category)

    {

        $category->load('products');


        return view('products')->withCategory($category);

    } 

}

您可以在刀片視圖中訪問產品列表,如下所示:$category->products


查看完整回答
反對 回復 2023-12-15
?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

您需要對您的Category模型進行調整,因為Category有很多Products。就目前而言,關系的命名并沒有反映出這一點


class Category extends Model

{

   public function products()

   {

       return $this->hasMany('App\Product');

   }

}

然后您可以通過Category模型訪問產品,如下所示。


$categories = Category::with('products')->all();


$categories->each(function($category) {

    $products = $category->products;

    

    // Dump & Die a collection of products

    dd($products);

});

注意: 我已使用 with() 方法預先加載關系,這只是為了防止 n+1 查詢。有關急切加載和延遲加載的更多信息可以在文檔中找到。


查看完整回答
反對 回復 2023-12-15
?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

你可以做類似的事情,


$products = product::with('categories')->get();

foreach($products as $product)

{

    foreach($product->categories as $category)

    {

        echo $category->name;

    }

}

$categories = Category::with('products')->get();


$category = Category::with('products')->find($category_id);


查看完整回答
反對 回復 2023-12-15
  • 4 回答
  • 0 關注
  • 247 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號