Sum*_*per 5 php laravel laravel-5
我有以下Eloquent查询,大量使用模型上的关系,它的工作原理.
这里是:
$trip = ModelLocale::where("lang",$lang)
                    ->where("slug",$slug)
                    ->where('mainmodel_id',$id)
                    ->with(['mainmodel' => function ($query) use ($id)
                    {
                        $query
                            ->where('id', $id)
                            ->with(['convertedPrices' => function ($nestedQuery) use($id)
                            {
                                $nestedQuery
                                    ->where('model_id', $id)
                                    ->where('currency', Function::getCurrency());
                            }])
                            ->with(['othermodel','othermodel2', /** */, 'othermodel5']);
                    }])
                    ->first();
Run Code Online (Sandbox Code Playgroud)
无论如何,这是有效的,只是一个例子.
我的问题是我得到了很多嵌套关系,当它进入视图时它有点混乱.
我有我团队的另一名成员,我们提出此查询的雄辩关系或具体方式可能会发生变化,但视图或多或少总是需要相同的数据.因此,我想组织/清理此查询并将其标准化,然后再将其传递给我的视图.
这有意义吗?
我理解Eloquent的每一个回报都是这个例子 - Collection.
但是我在处理各种方法时遇到了一些问题,因为当dd()我收藏时,我看到了很多属性和嵌套.
在保持各种关系模型的完整性的同时,操作这个大集合的推荐方法是什么?例如,此查询将返回ModelLocale顶部,然后是一个关系属性(我不需要的其他属性 - 比如perPage和wasRecentlyCreated)与另一个模型,然后是第二个模型上的更多嵌套关系.我想结合前两个模型,只有一个父母的关系.
所以,例如,当dd我得到:
-Model 1:
--Many properties
--Relations:
---Model 2
----Many properties
----Relations:
-----Model 3
-----Model 4
-----etc
Run Code Online (Sandbox Code Playgroud)
当我在视野中时,有一种简单的方法,而不是......
$model->model_relationship->model_relationship->atrribute
Run Code Online (Sandbox Code Playgroud)
...我可以重组这些并将两个第一个模型合并?或者重命名并映射关系和属性,同时忽略无关的属性,并为视图创建一个非常简单的属性数组?
对不起,这不是非常具体,我不能从我的数据库中显示确切的值,关键是它会改变...所以我正在寻找关于这种集合重组的一般指导.
我对JSON比这更舒服,而且我对这个集合的结构感到有点害怕,因为我读到某处的集合"就像类固醇上的数组一样".
以下是该集合的示例:
ModelLocale {#246 ?
  #fillable: array:1 [?]
  #connection: "mysql"
  #table: "model_locales"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:8 [?
    "id" => 580
    "main_model_id" => 290
    "title" => "Dolor dolores et hic."
    "slug" => "dolor-dolores-et-hic"
    "body" => "Enim consequuntur aliquam et iste aut incidunt corporis voluptates. Est sapiente doloribus est inventore delectus. Quia sequi doloremque nesciunt nulla non minu ?"
    "lang" => "fj"
    "created_at" => "2019-01-16 19:17:43"
    "updated_at" => "2019-01-16 19:17:43"
  ]
  #original: array:8 [?]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: array:1 [?
    "MainModel" => MainModel{#255 ?
      #guarded: array:1 [?]
      #hidden: array:2 [?]
      #connection: "mysql"
      #table: "mainmodels"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:16 [?
        "id" => 290
        "model2_id" => 17
        "model3_id" => 0
        "model4_id" => 451
        "price" => "385.00"
        "created_at" => "2019-01-16 19:17:42"
        "updated_at" => "2019-01-16 19:17:43"
      ]
      #original: array:16 [?]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:7 [?
        "model3" => Model 3{#267 ?}
        "model4" => Model 4{#264 ?}
        "model5" => Model 5{#307 ?
          #guarded: array:1 [?]
          #connection: "mysql"
          #table: "model5s"
          #primaryKey: "id"
          #keyType: "int"
          +incrementing: true
          #with: []
          #withCount: []
          #perPage: 15
          +exists: true
          +wasRecentlyCreated: false
          #attributes: array:7 [?]
          #original: array:7 [?]
          #changes: []
          #casts: []
          #dates: []
          #dateFormat: null
          #appends: []
          #dispatchesEvents: []
          #observables: []
          #relations: []
          #touches: []
          +timestamps: true
          #hidden: []
          #visible: []
          #fillable: []
        }
        "model6" => Collection {#311 ?}
        "model7" => Collection {#312 ?}
        "model8" => null
        "model9" => Collection {#368 ?}
      ]
      #touches: []
      +timestamps: true
      #visible: []
      #fillable: []
    }
  ]
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [?]
}
Run Code Online (Sandbox Code Playgroud)
所以我有ModelLocale,与之MainModel有关系,与其他模型有几种关系.但我只想要一个非常简单(尽可能多)我关心的属性数组 - 因为嵌套或关系可能会改变......
我有两点建议。
首先,尽可能将查询限制为仅预先加载特定列。
您可能并不总是需要所检索的关系中的每一列。因此,Eloquent 允许您指定要检索关系的哪些列:
$users = App\Book::with('author:id,name')->get();
Run Code Online (Sandbox Code Playgroud)
其次,使用响应转换器 - 请参阅Eloquent:API 资源- 在发送响应之前调整数据。这保证了您需要的数据以设定的格式提供。
这两件事都将减少响应负载并提供一个健全的、有组织的结构。
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           142 次  |  
        
|   最近记录:  |