Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AntonParotikov/039439d3956deccb1951020214736302 to your computer and use it in GitHub Desktop.
Save AntonParotikov/039439d3956deccb1951020214736302 to your computer and use it in GitHub Desktop.
Есть модель в который мы делаем отношения к другим моделям
public function hotels() {
return $this->hasManyThrough(
'Adrus\Travel\Models\OrderHotel',
'Adrus\Travel\Models\OrderCity'
);
}
На выходе
"cities": [
{
"id": 3,
"order_id": 12,
"id_city": 1,
"id_city_from": null,
"id_city_to": null
}
],
"hotels": [
{
"id": 2,
"order_city_id": 3,
"date_from": "2017-06-19 13:48:45",
"date_to": "2017-06-19 13:48:45",
"price": 1500,
"id_hotel": 0,
"order_id": 12
},
А мне же нужно так:
"cities": [
{
"id": 3,
"order_id": 12,
"id_city": 1,
"id_city_from": null,
"id_city_to": null
"hotels": [
{
"id": 2,
"order_city_id": 3,
"date_from": "2017-06-19 13:48:45",
"date_to": "2017-06-19 13:48:45",
"price": 1500,
"id_hotel": 0,
"order_id": 12
},
}
],
вопрос: как сделать именно такую структуру ?
@AntonParotikov
Copy link
Author

Order {#349
+timestamps: false
+hasManyThrough: array:1 [
"posts" => array:2 [
0 => "Adrus\Travel\Models\OrderHotel"
"through" => "Adrus\Travel\Models\OrderCity"
]
]
+rules: []
+table: "orders"
+implement: null
+attributes: array:6 [
"id" => 12
"user_id" => 69
"status" => 0
"adults" => 0
"date_from" => 0
"date_to" => 0
]
#jsonable: []
#dates: []
+duplicateCache: true
+hasMany: []
+hasOne: []
+belongsTo: []
+belongsToMany: []
+morphTo: []
+morphOne: []
+morphMany: []
+morphToMany: []
+morphedByMany: []
+attachOne: []
+attachMany: []
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#original: array:6 [
"id" => 12
"user_id" => 69
"status" => 0
"adults" => 0
"date_from" => 0
"date_to" => 0
]
#relations: array:4 [
"childs" => Collection {#381
#items: array:3 [
0 => Child {#374
+timestamps: false
+rules: []
+table: "persons_child"
+implement: null
+attributes: array:3 [
"id" => 1
"order_id" => 12
"age" => 5
]
#jsonable: []
#dates: []
+duplicateCache: true
+hasMany: []
+hasOne: []
+belongsTo: []
+belongsToMany: []
+morphTo: []
+morphOne: []
+morphMany: []
+morphToMany: []
+morphedByMany: []
+attachOne: []
+attachMany: []
+hasManyThrough: []
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#original: array:3 [
"id" => 1
"order_id" => 12
"age" => 5
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [
0 => ""
]
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
#emitterSingleEventCollection: []
#emitterEventCollection: array:1 [
"model.saveInternal" => array:1 [
500 => array:1 [
0 => Closure {#375
class: "Adrus\Travel\Models\Child"
parameters: array:2 [
"$data" => []
"$options" => []
]
use: array:1 [
"$model" => Child {#374}
]
file: "/home/visitrussia/web/visitrussia.dev.ad-rus.com/public_html/vendor/october/rain/src/Database/Traits/Validation.php"
line: "57 to 75"
}
]
]
]
#emitterEventSorted: []
#extensionData: array:3 [
"extensions" => []
"methods" => []
"dynamicMethods" => []
]
+sessionKey: null
#deferredBindingCache: null
#validationErrors: null
}
1 => Child {#376
+timestamps: false
+rules: []
+table: "persons_child"
+implement: null
+attributes: array:3 [
"id" => 2
"order_id" => 12
"age" => 7
]
#jsonable: []
#dates: []
+duplicateCache: true
+hasMany: []
+hasOne: []
+belongsTo: []
+belongsToMany: []
+morphTo: []
+morphOne: []
+morphMany: []
+morphToMany: []
+morphedByMany: []
+attachOne: []
+attachMany: []
+hasManyThrough: []
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#original: array:3 [
"id" => 2
"order_id" => 12
"age" => 7
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [
0 => "
"
]
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
#emitterSingleEventCollection: []
#emitterEventCollection: array:1 [
"model.saveInternal" => array:1 [
500 => array:1 [
0 => Closure {#377
class: "Adrus\Travel\Models\Child"
parameters: array:2 [
"$data" => []
"$options" => []
]
use: array:1 [
"$model" => Child {#376}
]
file: "/home/visitrussia/web/visitrussia.dev.ad-rus.com/public_html/vendor/october/rain/src/Database/Traits/Validation.php"
line: "57 to 75"
}
]
]
]
#emitterEventSorted: []
#extensionData: array:3 [
"extensions" => []
"methods" => []
"dynamicMethods" => []
]
+sessionKey: null
#deferredBindingCache: null
#validationErrors: null
}
2 => Child {#378
+timestamps: false
+rules: []
+table: "persons_child"
+implement: null
+attributes: array:3 [
"id" => 3
"order_id" => 12
"age" => 11
]
#jsonable: []
#dates: []
+duplicateCache: true
+hasMany: []
+hasOne: []
+belongsTo: []
+belongsToMany: []
+morphTo: []
+morphOne: []
+morphMany: []
+morphToMany: []
+morphedByMany: []
+attachOne: []
+attachMany: []
+hasManyThrough: []
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#original: array:3 [
"id" => 3
"order_id" => 12
"age" => 11
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [
0 => ""
]
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
#emitterSingleEventCollection: []
#emitterEventCollection: array:1 [
"model.saveInternal" => array:1 [
500 => array:1 [
0 => Closure {#379
class: "Adrus\Travel\Models\Child"
parameters: array:2 [
"$data" => []
"$options" => []
]
use: array:1 [
"$model" => Child {#378}
]
file: "/home/visitrussia/web/visitrussia.dev.ad-rus.com/public_html/vendor/october/rain/src/Database/Traits/Validation.php"
line: "57 to 75"
}
]
]
]
#emitterEventSorted: []
#extensionData: array:3 [
"extensions" => []
"methods" => []
"dynamicMethods" => []
]
+sessionKey: null
#deferredBindingCache: null
#validationErrors: null
}
]
}
"cities" => Collection {#407
#items: array:3 [
0 => OrderCity {#400
+timestamps: false
+rules: []
+table: "orders_cities"
+implement: null
+attributes: array:5 [
"id" => 3
"order_id" => 12
"id_city" => 1
"id_city_from" => null
"id_city_to" => null
]
#jsonable: []
#dates: []
+duplicateCache: true
+hasMany: []
+hasOne: []
+belongsTo: []
+belongsToMany: []
+morphTo: []
+morphOne: []
+morphMany: []
+morphToMany: []
+morphedByMany: []
+attachOne: []
+attachMany: []
+hasManyThrough: []
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#original: array:5 [
"id" => 3
"order_id" => 12
"id_city" => 1
"id_city_from" => null
"id_city_to" => null
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [
0 => "
"
]
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
#emitterSingleEventCollection: []
#emitterEventCollection: array:1 [
"model.saveInternal" => array:1 [
500 => array:1 [
0 => Closure {#401
class: "Adrus\Travel\Models\OrderCity"
parameters: array:2 [
"$data" => []
"$options" => []
]
use: array:1 [
"$model" => OrderCity {#400}
]
file: "/home/visitrussia/web/visitrussia.dev.ad-rus.com/public_html/vendor/october/rain/src/Database/Traits/Validation.php"
line: "57 to 75"
}
]
]
]
#emitterEventSorted: []
#extensionData: array:3 [
"extensions" => []
"methods" => []
"dynamicMethods" => []
]
+sessionKey: null
#deferredBindingCache: null
#validationErrors: null
}
1 => OrderCity {#402
+timestamps: false
+rules: []
+table: "orders_cities"
+implement: null
+attributes: array:5 [
"id" => 4
"order_id" => 12
"id_city" => 2
"id_city_from" => null
"id_city_to" => null
]
#jsonable: []
#dates: []
+duplicateCache: true
+hasMany: []
+hasOne: []
+belongsTo: []
+belongsToMany: []
+morphTo: []
+morphOne: []
+morphMany: []
+morphToMany: []
+morphedByMany: []
+attachOne: []
+attachMany: []
+hasManyThrough: []
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#original: array:5 [
"id" => 4
"order_id" => 12
"id_city" => 2
"id_city_from" => null
"id_city_to" => null
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [
0 => ""
]
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
#emitterSingleEventCollection: []
#emitterEventCollection: array:1 [
"model.saveInternal" => array:1 [
500 => array:1 [
0 => Closure {#403
class: "Adrus\Travel\Models\OrderCity"
parameters: array:2 [
"$data" => []
"$options" => []
]
use: array:1 [
"$model" => OrderCity {#402}
]
file: "/home/visitrussia/web/visitrussia.dev.ad-rus.com/public_html/vendor/october/rain/src/Database/Traits/Validation.php"
line: "57 to 75"
}
]
]
]
#emitterEventSorted: []
#extensionData: array:3 [
"extensions" => []
"methods" => []
"dynamicMethods" => []
]
+sessionKey: null
#deferredBindingCache: null
#validationErrors: null
}
2 => OrderCity {#404
+timestamps: false
+rules: []
+table: "orders_cities"
+implement: null
+attributes: array:5 [
"id" => 5
"order_id" => 12
"id_city" => 4
"id_city_from" => null
"id_city_to" => null
]
#jsonable: []
#dates: []
+duplicateCache: true
+hasMany: []
+hasOne: []
+belongsTo: []
+belongsToMany: []
+morphTo: []
+morphOne: []
+morphMany: []
+morphToMany: []
+morphedByMany: []
+attachOne: []
+attachMany: []
+hasManyThrough: []
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#original: array:5 [
"id" => 5
"order_id" => 12
"id_city" => 4
"id_city_from" => null
"id_city_to" => null
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [
0 => "
"
]
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
#emitterSingleEventCollection: []
#emitterEventCollection: array:1 [
"model.saveInternal" => array:1 [
500 => array:1 [
0 => Closure {#405
class: "Adrus\Travel\Models\OrderCity"
parameters: array:2 [
"$data" => []
"$options" => []
]
use: array:1 [
"$model" => OrderCity {#404}
]
file: "/home/visitrussia/web/visitrussia.dev.ad-rus.com/public_html/vendor/october/rain/src/Database/Traits/Validation.php"
line: "57 to 75"
}
]
]
]
#emitterEventSorted: []
#extensionData: array:3 [
"extensions" => []
"methods" => []
"dynamicMethods" => []
]
+sessionKey: null
#deferredBindingCache: null
#validationErrors: null
}
]
}
"hotels" => Collection {#433
#items: array:2 [
0 => OrderHotel {#428
+timestamps: false
+rules: []
+table: "orders_hotels"
+implement: null
+attributes: array:7 [
"id" => 2
"order_city_id" => 3
"date_from" => "2017-06-19 13:48:45"
"date_to" => "2017-06-19 13:48:45"
"price" => 1500
"id_hotel" => 0
"order_id" => 12
]
#jsonable: []
#dates: []
+duplicateCache: true
+hasMany: []
+hasOne: []
+belongsTo: []
+belongsToMany: []
+morphTo: []
+morphOne: []
+morphMany: []
+morphToMany: []
+morphedByMany: []
+attachOne: []
+attachMany: []
+hasManyThrough: []
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#original: array:7 [
"id" => 2
"order_city_id" => 3
"date_from" => "2017-06-19 13:48:45"
"date_to" => "2017-06-19 13:48:45"
"price" => 1500
"id_hotel" => 0
"order_id" => 12
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [
0 => ""
]
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
#emitterSingleEventCollection: []
#emitterEventCollection: array:1 [
"model.saveInternal" => array:1 [
500 => array:1 [
0 => Closure {#429
class: "Adrus\Travel\Models\OrderHotel"
parameters: array:2 [
"$data" => []
"$options" => []
]
use: array:1 [
"$model" => OrderHotel {#428}
]
file: "/home/visitrussia/web/visitrussia.dev.ad-rus.com/public_html/vendor/october/rain/src/Database/Traits/Validation.php"
line: "57 to 75"
}
]
]
]
#emitterEventSorted: []
#extensionData: array:3 [
"extensions" => []
"methods" => []
"dynamicMethods" => []
]
+sessionKey: null
#deferredBindingCache: null
#validationErrors: null
}
1 => OrderHotel {#430
+timestamps: false
+rules: []
+table: "orders_hotels"
+implement: null
+attributes: array:7 [
"id" => 7
"order_city_id" => 3
"date_from" => "0000-00-00 00:00:00"
"date_to" => "0000-00-00 00:00:00"
"price" => 33
"id_hotel" => 2
"order_id" => 12
]
#jsonable: []
#dates: []
+duplicateCache: true
+hasMany: []
+hasOne: []
+belongsTo: []
+belongsToMany: []
+morphTo: []
+morphOne: []
+morphMany: []
+morphToMany: []
+morphedByMany: []
+attachOne: []
+attachMany: []
+hasManyThrough: []
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#original: array:7 [
"id" => 7
"order_city_id" => 3
"date_from" => "0000-00-00 00:00:00"
"date_to" => "0000-00-00 00:00:00"
"price" => 33
"id_hotel" => 2
"order_id" => 12
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [
0 => "
"
]
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
#emitterSingleEventCollection: []
#emitterEventCollection: array:1 [
"model.saveInternal" => array:1 [
500 => array:1 [
0 => Closure {#431
class: "Adrus\Travel\Models\OrderHotel"
parameters: array:2 [
"$data" => []
"$options" => []
]
use: array:1 [
"$model" => OrderHotel {#430}
]
file: "/home/visitrussia/web/visitrussia.dev.ad-rus.com/public_html/vendor/october/rain/src/Database/Traits/Validation.php"
line: "57 to 75"
}
]
]
]
#emitterEventSorted: []
#extensionData: array:3 [
"extensions" => []
"methods" => []
"dynamicMethods" => []
]
+sessionKey: null
#deferredBindingCache: null
#validationErrors: null
}
]
}
"transports" => Collection {#452
#items: array:1 [
0 => OrderTransport {#449
+timestamps: false
+rules: []
+table: "orders_transports"
+implement: null
+attributes: array:6 [
"id" => 1
"order_id" => 12
"id_city_from" => 1
"id_city_to" => 2
"time" => null
"date" => "2017-06-19 13:49:08"
]
#jsonable: []
#dates: []
+duplicateCache: true
+hasMany: []
+hasOne: []
+belongsTo: []
+belongsToMany: []
+morphTo: []
+morphOne: []
+morphMany: []
+morphToMany: []
+morphedByMany: []
+attachOne: []
+attachMany: []
+hasManyThrough: []
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#original: array:6 [
"id" => 1
"order_id" => 12
"id_city_from" => 1
"id_city_to" => 2
"time" => null
"date" => "2017-06-19 13:49:08"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [
0 => ""
]
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
#emitterSingleEventCollection: []
#emitterEventCollection: array:1 [
"model.saveInternal" => array:1 [
500 => array:1 [
0 => Closure {#450
class: "Adrus\Travel\Models\OrderTransport"
parameters: array:2 [
"$data" => []
"$options" => []
]
use: array:1 [
"$model" => OrderTransport {#449}
]
file: "/home/visitrussia/web/visitrussia.dev.ad-rus.com/public_html/vendor/october/rain/src/Database/Traits/Validation.php"
line: "57 to 75"
}
]
]
]
#emitterEventSorted: []
#extensionData: array:3 [
"extensions" => []
"methods" => []
"dynamicMethods" => []
]
+sessionKey: null
#deferredBindingCache: null
#validationErrors: null
}
]
}
]
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [
0 => "
"
]
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
#emitterSingleEventCollection: []
#emitterEventCollection: array:1 [
"model.saveInternal" => array:1 [
500 => array:1 [
0 => Closure {#350
class: "Adrus\Travel\Models\Order"
parameters: array:2 [
"$data" => []
"$options" => []
]
use: array:1 [
"$model" => Order {#349}
]
file: "/home/visitrussia/web/visitrussia.dev.ad-rus.com/public_html/vendor/october/rain/src/Database/Traits/Validation.php"
line: "57 to 75"
}
]
]
]
#emitterEventSorted: []
#extensionData: array:3 [
"extensions" => []
"methods" => []
"dynamicMethods" => []
]
+sessionKey: null
#deferredBindingCache: null
#validationErrors: null
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment