Created
May 24, 2019 04:27
-
-
Save kurtsk/7f5f910f4e4d5b64aa5951a36fa07062 to your computer and use it in GitHub Desktop.
Convenciones de Eloquent Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
La tabla de un Modelo se le agrega la s, en plural. | |
La tabla de la clase Post sería posts. | |
La tabla de la clase ProductImage sería product_images (ProductType - product_types). | |
El campo primary key de una tabla debe tener el nombre de id, con el atributo AUTO_INCREMENT. | |
//. Relaciones anidadas Laravel | |
return Event::with('city.companies.persons')->get(); | |
//. Si solo desea seleccionar ciertos campos de la tabla de persons , use esto: | |
return Event::with(['city.companies.persons' => function ($query) | |
{ | |
$query->select('id', '...'); | |
}]) | |
->get(); | |
//. Para los campos específicos de ciudades y empresas, debe distribuirlos con elocuencia. P.ej: | |
return Event::with([ 'city' => function ($query) | |
{ | |
$query->select('id', '...'); | |
}, | |
'city.companies' => function ($query) | |
{ | |
$query->select('id', '...'); | |
}, | |
'city.companies.persons' => function ($query) | |
{ | |
$query->select('id', '...'); | |
} | |
]) | |
->get(); | |
//. Relaciones anidadas bkingc | |
$opendayactives = Establishment::where('id', $establishment->id) | |
->with(['areas', 'areas.opendayactives', 'areas.opendayactives.openday']) | |
->orderBy('id', 'desc') | |
->get() | |
->pluck('areas') | |
->collapse() | |
// ->pluck('opendayactives') | |
// ->collapse() | |
// ->unique('id') | |
->values(); | |
return ['areas' => $opendayactives]; | |
//. Relaviones anidadas con with... | |
$opendayactives = Establishment::where('id', $establishment->id) | |
->with(['areas', 'areas.opendayactives', 'areas.opendayactives.openday']) | |
->orderBy('id', 'desc') | |
->get() | |
->pluck('areas') | |
->collapse() | |
->values(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment