Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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
如何使用laravel关系从数据库中获取数据_Laravel_Eloquent - Fatal编程技术网

如何使用laravel关系从数据库中获取数据

如何使用laravel关系从数据库中获取数据,laravel,eloquent,Laravel,Eloquent,我在一个主页上工作,我必须在其中显示功能类别供应商滑块。 我想听到的是: 我想从分类表中随机获得3个特色类别,每个类别中有6个供应商,我也想显示这些供应商 数据库结构: 类别表包含is_特色标志。 我有一个供应商类别表,其中我映射了供应商和三个类别 供应商分类表 和供应商表,其中我有供应商详细信息 供应商表 到目前为止我都试过了。 我有trid给有很多关系,但是得到的是空的项目数组。我认为类别有很多供应商,所以我添加了很多 public function supplierList() {

我在一个主页上工作,我必须在其中显示功能类别供应商滑块。 我想听到的是:

我想从分类表中随机获得3个特色类别,每个类别中有6个供应商,我也想显示这些供应商

数据库结构:

类别表包含
is_特色
标志。 我有一个供应商类别表,其中我映射了供应商和三个类别

供应商分类表 和供应商表,其中我有供应商详细信息

供应商表 到目前为止我都试过了。 我有trid给
有很多关系,但是得到的是空的项目数组。我认为类别有很多供应商,所以我添加了很多

public function supplierList() 
{
    return $this->hasMany('App\DB\Supplier\SupplierCategoryDetail', 'cat_id', 'id');        
}
我是如何计算出随机类别的。但是在将
与()一起使用后,
会给出空结果

$data['featured_category'] = Category::with('supplierList')->where('status','1')->inRandomOrder()->limit(3)->get();`

我是全新的拉雷维尔和它的关系。我用拉雷维尔的雄辩来做这件事

请参阅本文档:

您应该在相互关联的两个模型上编写关系函数

请这样做:

Category.php

Supplier.php

SupplierCategory.php

Controller.php

index.blade.php


请参阅本文档:

您应该在相互关联的两个模型上编写关系函数

请这样做:

Category.php

Supplier.php

SupplierCategory.php

Controller.php

index.blade.php


由于您使用了轴,因此“多对多”关系是可能的。在这种情况下,您需要使用
belongstomy

你的亲戚看起来像:

return $this->belongsToMany(
    'App\DB\Supplier\SupplierCategoryDetail',
    'SupplierCategoryMappingTable', // or what the name is of the table
    'cat_id',
    'sup_id'
);
Category::with('supplierList')
    ->where('is_featured', '1')
    ->inRandomOrder()
    ->limit(3)
    ->get();
此外,我在您的分类表中没有看到
状态
列。看起来这应该是
的特色

如果不进行测试,我认为您的最终查询可能如下所示:

return $this->belongsToMany(
    'App\DB\Supplier\SupplierCategoryDetail',
    'SupplierCategoryMappingTable', // or what the name is of the table
    'cat_id',
    'sup_id'
);
Category::with('supplierList')
    ->where('is_featured', '1')
    ->inRandomOrder()
    ->limit(3)
    ->get();

由于您使用了轴,因此“多对多”关系是可能的。在这种情况下,您需要使用
belongstomy

你的亲戚看起来像:

return $this->belongsToMany(
    'App\DB\Supplier\SupplierCategoryDetail',
    'SupplierCategoryMappingTable', // or what the name is of the table
    'cat_id',
    'sup_id'
);
Category::with('supplierList')
    ->where('is_featured', '1')
    ->inRandomOrder()
    ->limit(3)
    ->get();
此外,我在您的分类表中没有看到
状态
列。看起来这应该是
的特色

如果不进行测试,我认为您的最终查询可能如下所示:

return $this->belongsToMany(
    'App\DB\Supplier\SupplierCategoryDetail',
    'SupplierCategoryMappingTable', // or what the name is of the table
    'cat_id',
    'sup_id'
);
Category::with('supplierList')
    ->where('is_featured', '1')
    ->inRandomOrder()
    ->limit(3)
    ->get();
return $this->belongsToMany(
    'App\DB\Supplier\SupplierCategoryDetail',
    'SupplierCategoryMappingTable', // or what the name is of the table
    'cat_id',
    'sup_id'
);
Category::with('supplierList')
    ->where('is_featured', '1')
    ->inRandomOrder()
    ->limit(3)
    ->get();