- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我没有太多运气用 Laravel 的方式解决这个问题。所以我提出两个问题。
鉴于我有一辆汽车,那辆汽车可以有很多特征,但这些特征也按特征类型分开,我如何返回所述汽车的所有特征,分开特征类型?
我有四个表,listings_features 是数据透视表:
我有以下代码,它产生了我需要的东西,但是当我使用它时,我得到一个 Laravel 错误......“调用字符串上的成员函数 addEagerConstraints()”......因为我这样调用它:
Listing::with(
'features',
)->get();
我用来以我想要的格式(不是固定的)生成数据的代码是
public function features()
{
$out = array();
$features = $this->hasMany('App\Models\ListingFeature', 'listing_id', 'id')->select('feature_id')->get();
$types = ListingFeatureType::all();
foreach($types as $key => $obj){
$out[$key]['listing_feature_type_id'] = $obj->id;
$out[$key]['name'] = $obj->listing_feature_type;
$out[$key]['features'] = ListingFeatureValue::whereIn('id', $features->toArray())->where('listing_feature_type_id', '=', $obj->id)->get()->toArray();
}
return json_encode($out);
}
哪个返回:
[
{
"listing_feature_type_id": "0f40888c-3b09-40ed-aef6-0fddc1a155b6",
"name": "Safety",
"features": [
{
"id": "0ed0ad63-a6ed-4818-8c6f-ee048694dcd9",
"listing_feature_type_id": "0f40888c-3b09-40ed-aef6-0fddc1a155b6",
"listing_feature_text": "Anti-Lock Brakes"
},
{
"id": "37abeef2-dc22-4995-8503-f89962242ea6",
"listing_feature_type_id": "0f40888c-3b09-40ed-aef6-0fddc1a155b6",
"listing_feature_text": "Collision Detection"
},
{
"id": "3c0728e1-91f7-4f44-ac0b-429eda816692",
"listing_feature_type_id": "0f40888c-3b09-40ed-aef6-0fddc1a155b6",
"listing_feature_text": "Dual Airbags"
},
{
"id": "4255b8b4-e71c-4059-8a22-1b9894512564",
"listing_feature_type_id": "0f40888c-3b09-40ed-aef6-0fddc1a155b6",
"listing_feature_text": "Side Airbags"
}
]
},
{
"listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"name": "Interior",
"features": [
{
"id": "1b89581e-1a30-4dce-9455-ab0ad4c49bcf",
"listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_text": "Privacy Glass"
},
{
"id": "59e3628f-3cef-4447-9cb2-71be4a3046a4",
"listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_text": "Onboard GPS"
},
{
"id": "66fe416b-98dc-45c8-979d-78f2ea7fe876",
"listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_text": "In-dash Navigation"
},
{
"id": "8fe836a3-5596-4306-aac1-bae4cb596e20",
"listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_text": "Power Windows"
},
{
"id": "e4addb5a-1b26-4ae3-b0ee-3b8bce892fb9",
"listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_text": "Tinted Windows"
},
{
"id": "f95b8253-a2b8-4bfc-90c0-0fc656c3f200",
"listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_text": "CD Player"
}
]
},
{
"listing_feature_type_id": "8d16e8ea-3d38-48dc-8e56-00d1cc736f0d",
"name": "Exterior",
"features": [
{
"id": "3aa6dd05-dd3a-4e93-ad06-295687a8dda1",
"listing_feature_type_id": "8d16e8ea-3d38-48dc-8e56-00d1cc736f0d",
"listing_feature_text": "Spoiler"
}
]
}
]
以下是模型(非常基础,刚刚开始):
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ListingFeatureValue extends Model
{
protected $table = 'listings_features_values';
public $timestamps = false;
public $incrementing = false;
public function type() {
return $this->belongsTo('App\Models\ListingFeatureType', 'listing_feature_type_id', 'id');
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ListingFeatureType extends Model
{
protected $table = 'listings_features_types';
public $timestamps = false;
public $incrementing = false;
public function values() {
return $this->hasMany('App\Models\ListingFeatureValue', 'listing_feature_type_id', 'id');
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ListingFeature extends Model
{
protected $table = 'listings_features';
public $timestamps = false;
public $incrementing = false;
}
如何创建 Laravel 模型关系来实现相同的结果集但如上所述调用它?或者,我怎样才能像上面那样调用它但阻止错误发生?
如果你做到了这一点,谢谢!
更新:
我无法让它按原样工作,我遇到了一个 SQL 错误,这让我觉得我需要一个 belongsToManyThrough 以便我可以指定表名:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ad_l5.listing_listing_feature' doesn't exist (SQL: select `listings_features`.*, `listing_listing_feature`.`listing_id` as `pivot_listing_id`, `listing_listing_feature`.`listing_feature_id` as `pivot_listing_feature_id` from `listings_features` inner join `listing_listing_feature` on `listings_features`.`id` = `listing_listing_feature`.`listing_feature_id` where `listing_listing_feature`.`listing_id` in (b266c874-1cef-4f49-b65f-f91ddaaf6aee, e93674ca-3f82-45d8-9961-e8569cac164b))
但是使用下面@Carter Fort 的答案发布的确切代码并将 features() 方法更改为
return $this->belongsToMany(ListingFeatureValue::class, 'listings_features', 'listing_id', 'feature_id');
同时添加到模型 ListingFeatureValue
public function type()
{
return $this->belongsTo(ListingFeatureType::class, 'listing_feature_type_id', 'id');
}
我得到的输出是:
"featuresByType": {
"": [
{
"id": "3aa6dd05-dd3a-4e93-ad06-295687a8dda1",
"listing_feature_type_id": "8d16e8ea-3d38-48dc-8e56-00d1cc736f0d",
"listing_feature_text": "Spoiler",
"pivot": {
"listing_id": "e93674ca-3f82-45d8-9961-e8569cac164b",
"feature_id": "3aa6dd05-dd3a-4e93-ad06-295687a8dda1"
},
"type": {
"id": "8d16e8ea-3d38-48dc-8e56-00d1cc736f0d",
"listing_feature_type": "Exterior"
}
},
{
"id": "f95b8253-a2b8-4bfc-90c0-0fc656c3f200",
"listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_text": "CD Player",
"pivot": {
"listing_id": "e93674ca-3f82-45d8-9961-e8569cac164b",
"feature_id": "f95b8253-a2b8-4bfc-90c0-0fc656c3f200"
},
"type": {
"id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_type": "Interior"
}
},
{
"id": "66fe416b-98dc-45c8-979d-78f2ea7fe876",
"listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_text": "In-dash Navigation",
"pivot": {
"listing_id": "e93674ca-3f82-45d8-9961-e8569cac164b",
"feature_id": "66fe416b-98dc-45c8-979d-78f2ea7fe876"
},
"type": {
"id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_type": "Interior"
}
},
{
"id": "0ed0ad63-a6ed-4818-8c6f-ee048694dcd9",
"listing_feature_type_id": "0f40888c-3b09-40ed-aef6-0fddc1a155b6",
"listing_feature_text": "Anti-Lock Brakes",
"pivot": {
"listing_id": "e93674ca-3f82-45d8-9961-e8569cac164b",
"feature_id": "0ed0ad63-a6ed-4818-8c6f-ee048694dcd9"
},
"type": {
"id": "0f40888c-3b09-40ed-aef6-0fddc1a155b6",
"listing_feature_type": "Safety"
}
},
...
这比我当时的情况要好,但如果结果按类型分组然后按其中的特征分组,而不是每个特征都附加一个类型,那就更好了。使其更难解析显示。感谢迄今为止的帮助!
更新 2
这些将是我的架构文件:
// listings
Schema::create('listings', function (Blueprint $table) {
$table->string('id');
$table->string('title');
});
// listings to features pivot
Schema::create('listings_features', function (Blueprint $table) {
$table->string('listing_id');
$table->string('feature_id');
});
// feature types (i.e. Safety)
Schema::create('listings_features_types', function(Blueprint $table){
$table->string('id');
$table->string('listing_feature_type');
});
// feature values (i.e. Anti-Lock Brakes)
Schema::create('listings_features_values', function(Blueprint $table){
$table->string('id');
$table->string('listing_feature_type_id'); // links to listings_features_types
$table->string('listing_feature_text');
});
更新 3根据下面的答案进行更改(我将在工作后发布所有代码)我接近我想要的。
"features": [
{
"id": "3aa6dd05-dd3a-4e93-ad06-295687a8dda1",
"listing_feature_type_id": "8d16e8ea-3d38-48dc-8e56-00d1cc736f0d",
"listing_feature_text": "Spoiler",
"pivot": {
"listing_id": "e93674ca-3f82-45d8-9961-e8569cac164b",
"feature_id": "3aa6dd05-dd3a-4e93-ad06-295687a8dda1"
},
"type": {
"id": "8d16e8ea-3d38-48dc-8e56-00d1cc736f0d",
"listing_feature_type": "Exterior"
}
},
{
"id": "f95b8253-a2b8-4bfc-90c0-0fc656c3f200",
"listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_text": "CD Player",
"pivot": {
"listing_id": "e93674ca-3f82-45d8-9961-e8569cac164b",
"feature_id": "f95b8253-a2b8-4bfc-90c0-0fc656c3f200"
},
"type": {
"id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_type": "Interior"
}
},
{
"id": "66fe416b-98dc-45c8-979d-78f2ea7fe876",
"listing_feature_type_id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_text": "In-dash Navigation",
"pivot": {
"listing_id": "e93674ca-3f82-45d8-9961-e8569cac164b",
"feature_id": "66fe416b-98dc-45c8-979d-78f2ea7fe876"
},
"type": {
"id": "84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_type": "Interior"
}
//.... ]
我想要的是:
{
"feature_types":[
{
"type":{
"id":"8d16e8ea-3d38-48dc-8e56-00d1cc736f0d",
"listing_feature_type":"Interior",
"features":[
{
"id":"3aa6dd05-dd3a-4e93-ad06-295687a8dda1",
"listing_feature_type_id":"8d16e8ea-3d38-48dc-8e56-00d1cc736f0d",
"listing_feature_text":"GPS"
},
{
"id":"66fe416b-98dc-45c8-979d-78f2ea7fe876",
"listing_feature_type_id":"84dcce5c-34b9-4c8b-9066-5b20a42cf4cd",
"listing_feature_text":"In-dash Navigation"
},
] "pivot":{
"listing_id":"e93674ca-3f82-45d8-9961-e8569cac164b",
"feature_id":"f95b8253-a2b8-4bfc-90c0-0fc656c3f200"
}
}
}
]
}
最佳答案
您可以在 Listing
和 ListingFeatureValue
模型之间使用多对多关系,然后使用groupBy
集合方法。
列表模型:
class Listing extends Model {
protected $hidden = [
'features'
];
protected $appends = [
'feature_types'
];
public function features(){
return $this->belongsToMany(ListingFeatureValue::class, 'listings_features', 'listing_id', 'feature_id');
}
public function getFeatureTypesAttribute()
{
return $this->features->groupBy(function ($feature, $key) {
return $feature->type->id;
})->map(function($features, $key){
$type = ListingFeatureType::find($key);
$type->features = $features;
return $type;
})->values();
}
}
getFeatureTypesAttribute()
是这里的亮点,因为您可以将它与 appends
数组结合使用,以强制 Eloquent 模型将其附加到任何 toArray()
调用模型实例,这是 toJson()
在将模型转换为 JSON 时使用的方法。
首先获取所有列表值然后使用 groupBy
和 map
集合方法划分它们似乎有点令人费解,但是没有原生的 Eloquent 机制来使用hasManyThrough
通过多对多关系。有一个 different approach here如果你不喜欢这个。
ListingFeatureValue 模型:
class ListingFeatureValue extends Model
{
public $table = 'listings_features_values';
public function type()
{
return $this->belongsTo(ListingFeatureType::class, 'feature_type_id');
}
}
我在这里展示这个模型是因为 type()
关系在上面的 getFeaturesByTypeAttribute()
方法中被调用并且不希望有任何混淆。
并且,为了完整起见,ListingFeatureType 模型:
class ListingFeatureType extends Model
{
public $table = "listings_features_types";
public function listings()
{
return $this->hasMany(ListingFeatureValue::class, 'listing_feature_type_id');
}
}
如果您想预先加载列表及其功能和类型以获得所有列表的完整输出,您可以这样做:
App\Listing::with('features.type')->get()->toJson();
我的迁移文件如下所示:
//create_listings_table
Schema::create('listings', function (Blueprint $table) {
$table->increments('id');
$table->string('uuid');
$table->timestamps();
});
//create_listings_features_values_table
Schema::create('listings_features_values', function (Blueprint $table) {
$table->increments('id');
$table->string('listing_feature_text');
$table->integer('listing_feature_type_id')->unsigned();
$table->timestamps();
});
//create_listings_features_types_table
Schema::create('listings_features_types', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
//create_listings_features_table
Schema::create('listings_features', function(Blueprint $table){
$table->integer('listing_id')->unsigned();
$table->integer('listing_feature_id')->unsigned();
});
您可以在此处了解有关收集方法的更多信息:
https://laravel.com/docs/5.3/collections#available-methods
...并在此处预加载:
https://laravel.com/docs/5.3/eloquent-relationships#eager-loading
...以及此处的多对多关系:
https://laravel.com/docs/5.3/eloquent-relationships#many-to-many
关于php - Laravel 拥有多对多对一的 Eloquent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41133533/
我是 Laravel 的新手,我正在处理我的数据库迁移。对于一个表,我在表定义中包含了 $table->timestamps() 快捷方式。令我沮丧的是,我发现在我为表设置种子后,created_at
我有一个任务需要显示每种类型的假期,其中 id 是奇数。当我尝试在 Eloquent 中执行它时,它总是给我一个错误。 查询 Select holiday_type.id, holiday_type.
我有一个任务需要显示每种类型的假期,其中 id 是奇数。当我尝试在 Eloquent 中执行它时,它总是给我一个错误。 查询 Select holiday_type.id, holiday_type.
好的,我有型号 A链接到许多模型 B , 所以型号 A有一个 hasMany与模型的关系 B和型号B有一个 belongsTo与模型的关系 A . 但是,在所有这些中B有一个特定的,例如具有较高值的
如何应用 Laravel 的 Eloquent whereIn() 以便它包含 null? 我已经尝试过: User::whereIn("column", [null, 1, 2]) -> get()
我有一张库存变动表,其中显示了产品的历史记录(接收、移动位置等) 我有两个计算(通过总和)的查询: 原收到数量。 当前的实时数量(在事物被移动之后)。 这两个查询都返回相同模型的集合。它们将始终具有相
我一直在研究 Slim 2,最近才开始部署到生产服务器。到目前为止,一切似乎都运行正常。我能够登录,所以我知道我正在连接到数据库就好了。它识别出我登录的人,识别出我作为该用户拥有的权限。我有另一个表有
我在 Eloquent 中使用 orWhere 时遇到问题。 我有一个团队,这个团队有一些资料。我想获取状态 = 1 或状态 = 2 的所有配置文件。但我无法让它工作。 我的代码是这样的: $prof
在 Zizaco/entrust的 Laravel Entrust,如何设置与 Eloquent 的 Entrust 类关系,以便获得角色拥有的权限列表,如下所示: // get admin role
我想为我的两个表创建一个汇总报告,一个是员工表,另一个是 sims 表。这些表具有一对多的关系。我知道我们可以通过使用导出模型的数据->fromModel($model)但是有没有办法让我可以根据这两
我有两个模型,一个是扩展 Jenssegers\Model 的 mongo 模型,另一个是扩展 Illuminate\Model 的 sql 模型。这个 sql 模型没有定义连接名称,因为我们有多个数
我有两个多对一关系的模型: class Meal extends \Eloquent { /** * public Integer $id; - primary key
到目前为止,Vue2 对我来说读起来非常好,但除了这一点我很难调试。 Shop-show.vue ..... ..... {{shop.user.id}} .....
关闭。这个问题需要更多 focused .它目前不接受答案。 想要改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 2 年前。 Improve this q
我需要获取第二个表的 ID。我无法避免使用 INNER JOIN,因为我必须按第二个表中的列对结果进行排序。 这是我的两张 table events id name .... date
我正在尝试为我的用户提供 Laravel hiscores 分页表的排名数字。 这是我发现有效的 MySQL 查询。我正在尝试将其作为 Laravel Eloquent 查询来使用。 select @
我的产品模型上有这种关系。 public function productInventory(){ return $this->hasOne('App\Models\Ecommerce
我正在尝试在 Laravel 5.5 应用程序中为一个包含关系数据的表添加种子。 我有这两个表/模型: 用户 问题 在 app\User.php 模型文件上;我有以下 hasMany 关系: publ
我重写了这个帮助请求,并简化了我试图尽量减少移动部件数量的内容。 我是一名经验丰富的 OOP 程序员,但这是我使用 LAMP、Laravel 或 Eloquent 的第一个项目。 我正在 Larave
我正在使用 Laravel 和 MongoDB (jenssegers/laravel-mongodb) 开发一个网络应用程序。 在使用 php artisan make:model 创建新模型时,命
我是一名优秀的程序员,十分优秀!