Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel雄辩地讲述了如何根据类别id获得所有产品_Php_Laravel_E Commerce - Fatal编程技术网

Php Laravel雄辩地讲述了如何根据类别id获得所有产品

Php Laravel雄辩地讲述了如何根据类别id获得所有产品,php,laravel,e-commerce,Php,Laravel,E Commerce,UserProductsController class UserProductsController extends Controller { public function index() { $products = Product::get(); return view ('products')->with(compact('products')); } public function pro

UserProductsController

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'));
      
    }
  
}
产品\u表

 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();
            });
        }
类别\u表

 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');
   }
}
类别型号

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');

如何获得同一类别的所有产品?由于这是我的第一个项目,我在这方面花费了更多的时间。但对我来说什么都不管用。有谁能指导我吗?

假设你的关系设置正确(事实并非如此)

你有几种方法来表达你的雄辩:

$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();
列举一些

findOrFail


where has

您需要对您的
类别
模型进行调整,因为
类别
有许多
产品
。目前,这种关系的命名并不反映这一点

class Category extends Model
{
   public function products()
   {
       return $this->hasMany('App\Product');
   }
}
然后,您可以像这样通过
类别
模型访问产品

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

$categories->each(function($category) {
    $products = $category->products;
    
    // Dump & Die a collection of products
    dd($products);
});

注意:我已经使用
with()
方法加载了关系,这只是为了防止
n+1
查询。有关快速加载和延迟加载的更多信息,请参阅文档。

您可以执行以下操作:

$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);

首先,你没有正确定义你的关系。应该是这样的:

类产品扩展模型
{
公共职能类别()
{
返回$this->belongsTo('App\Category');
}
}
类类别扩展模型
{
公共功能产品()
{
返回$this->hasMany('App\Product');
}
}
然后在产品迁移文件中,
cat\u id
应重命名为
category\u id
。这样,就不需要在关系上指定外键

我假设您想列出属于特定类别的所有产品。通过路由模型绑定,您可以轻松做到这一点。在这种情况下,您的路线应该如下所示:

Route::get('categories/{category:id}/products',[CategoryController::class,'products']);
然后在控制器中:

使用App\Category;
类CategoryController扩展控制器
{
公共功能产品(类别$类别)
{
$category->load('products');
返回视图(“产品”)->withCategory($category);
} 
}

您可以访问刀片视图中的产品列表,如下所示:
$category->products

名称不正确。它应该是
products
,而不是
category
与您的问题无关,但是产品模型上的
category()
关系方法不正确,它应该是
$this->belongsTo('App\category','cat\u id'))
因为带有外键的列被称为
cat\u id
而不是
category\u id
这些都是可靠的选择。如果已经有可用的类别模型,则第一个选项应该是“转到”。如果您不需要访问类别模型的任何属性(不需要表联接),则第二个选项非常有用。第三个选项不太常见,但100%有效。虽然这些选项在理论上都有效,但要在OP的情况下发挥作用,第二个选项应该是
cat_id
,而不是
category_id
,第三个选项不起作用,因为关系设置不正确,同样是因为
类别id
=>
类别id
。我在blade中添加了href。它抛出错误,因为缺少[Route:categorize][URI:categorize/{category\u id}]的必需参数。
$c->category\u id
应该是
$c->id