Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.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 如何从每个类别中获取2种产品_Php_Database_Laravel - Fatal编程技术网

Php 如何从每个类别中获取2种产品

Php 如何从每个类别中获取2种产品,php,database,laravel,Php,Database,Laravel,我想从每个类别中取两种产品 Product.php class Product extends Model { protected $table = 'products'; } class Product extends Model { protected $table = 'products'; public function Category() { return $this->belongsTo(Product::class);

我想从每个类别中取两种产品

Product.php

class Product extends Model
{ 
   protected $table = 'products';
}
class Product extends Model
{ 
   protected $table = 'products';

  public function Category()
    {
        return $this->belongsTo(Product::class);
    }
}
Category.php

class Category extends Model {
    protected $table = 'categories';
}
class Category extends Model {
    protected $table = 'categories';

    public function Product()
    {
        return $this->hasMany(Product::class);
    }
}
控制器

public function index() {
    $products = Product::all();
    return Product::latest()->take(5)->get();
}
public function index() {
   
    return Category::with('Product'=>funcation($obj){
        return $obj->take(2);
    })->latest()->take(5)->get();
}
试试这个

$products = Product::latest()->take(2)->with('categories')->get();
试试这个

$products = Product::latest()->take(2)->with('categories')->get();

这将获取每个类别的前两种产品:

// Get all categories
$categories = Category::all();

//Create an empty array to store our product IDs
$prodIds = array();

// Extract the first 2 product IDs in each category
foreach ($categories as $category) {
    $prodIds[] = $category->products->take(2)->pluck('id');
}

// Fetch products from their IDs
$products = Product::findMany($prodIds);


编辑:我上面的回答也应该有效,如果该类别没有至少两种产品,它将失败。要解决此问题,您需要从$prodIds中删除空值。

这将获取每个类别的前两个产品:

// Get all categories
$categories = Category::all();

//Create an empty array to store our product IDs
$prodIds = array();

// Extract the first 2 product IDs in each category
foreach ($categories as $category) {
    $prodIds[] = $category->products->take(2)->pluck('id');
}

// Fetch products from their IDs
$products = Product::findMany($prodIds);

编辑:我上面的回答也应该有效,如果该类别没有至少两种产品,它将失败。要解决这个问题,您需要从$prodIds中删除空值。

Product.php

class Product extends Model
{ 
   protected $table = 'products';
}
class Product extends Model
{ 
   protected $table = 'products';

  public function Category()
    {
        return $this->belongsTo(Product::class);
    }
}
注意:在类别模型中导入类别模型

Category.php

class Category extends Model {
    protected $table = 'categories';
}
class Category extends Model {
    protected $table = 'categories';

    public function Product()
    {
        return $this->hasMany(Product::class);
    }
}
注意:在产品模型中导入您的类别模型

控制器

public function index() {
    $products = Product::all();
    return Product::latest()->take(5)->get();
}
public function index() {
   
    return Category::with('Product'=>funcation($obj){
        return $obj->take(2);
    })->latest()->take(5)->get();
}
Product.php

class Product extends Model
{ 
   protected $table = 'products';
}
class Product extends Model
{ 
   protected $table = 'products';

  public function Category()
    {
        return $this->belongsTo(Product::class);
    }
}
注意:在类别模型中导入类别模型

Category.php

class Category extends Model {
    protected $table = 'categories';
}
class Category extends Model {
    protected $table = 'categories';

    public function Product()
    {
        return $this->hasMany(Product::class);
    }
}
注意:在产品模型中导入您的类别模型

控制器

public function index() {
    $products = Product::all();
    return Product::latest()->take(5)->get();
}
public function index() {
   
    return Category::with('Product'=>funcation($obj){
        return $obj->take(2);
    })->latest()->take(5)->get();
}


您好,欢迎光临。如果您在问题中使用正确的代码格式,这将有助于像我这样的人回答您的问题。另外,您希望获取哪两种产品?类别中的前2个、后2个?如果您能相应地编辑您的问题,这将有所帮助。干杯定义和使用之后,它变得更容易了。我想从每个类别中获取2个产品。Hello和welcome。如果您在问题中使用正确的代码格式,这将有助于像我这样的人回答您的问题。另外,您希望获取哪两种产品?类别中的前2个、后2个?如果您能相应地编辑您的问题,这将有所帮助。干杯定义和使用之后,它变得非常容易。我想从每个类别中获取2个产品。您将获得2个产品及其所有类别。这个问题是关于从每个类别中获得2种产品。是的,我不理解这个问题。谢谢这并不能回答这个问题。一旦你有足够的钱,你将能够;相反请在您的答案中添加一些解释,以便其他人可以从中学习您将获得2种产品及其所有类别。这个问题是关于从每个类别中获得2种产品。是的,我不理解这个问题。谢谢这并不能回答这个问题。一旦你有足够的钱,你将能够;相反请在您的答案中添加一些解释,以便其他人可以从中了解我需要在关系模型中编写的内容请参见下面的@Abhishek answer了解模型关系。我放置了该关系,但postman给出了“消息”:“SQLSTATE[HY000]:一般错误:2031(SQL:select*from
products
where
products
id
in(?,)”,这个错误我需要在关系的模型中写些什么请参见下面的@Abhishek答案了解模型的关系。我放置了这个关系,但postman给出了“消息”:“SQLSTATE[HY000]:一般错误:2031(SQL:从
产品
中选择*其中
产品
id
在(?,)”中,此错误