Created
August 30, 2014 17:38
-
-
Save aitorjs/b89f8a88c625b534399e to your computer and use it in GitHub Desktop.
Funcion e uso de un "metodo" que busca los datos de una galeria junto con sus imagenes y se le puede añadir parametros de filtrado.
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
Route::get('prueba', function() { | |
/* call */ | |
$values = array( | |
'orderBy' => 'name', | |
'orderByDirection' => 'asc', | |
'search' => ''); | |
$gallery = busqueda($values); | |
/* presentation */ | |
echo "<pre>"; | |
var_dump($gallery->name); | |
echo "</pre>"; | |
echo "<pre>"; | |
foreach ($gallery->multimedia as $image) { | |
echo($image->id.' - '); | |
echo($image->src.' - '); | |
echo($image->name.'<br/>'); | |
} | |
echo "</pre>"; | |
}); | |
// Esta function va al modelo de Gallery | |
// En los controladores se llamara Gallery::busqueda($values) | |
// Hace una select inteligente y saca la galeria con sus imagenes junto | |
// con sus especificaciones. | |
function busqueda($values) { | |
$gallery = Gallery::with(['multimedia' => function ($q) use ($values) { | |
$defaultValues = array( | |
'orderBy' => 'name', | |
'orderByDirection' => 'asc', | |
'search' => '', | |
'skip' => '', | |
'take' => '', | |
); | |
extract(array_merge($defaultValues, $values)); | |
if($orderBy || $orderByDirection) { | |
$q->orderBy($orderBy, $orderByDirection); | |
} | |
if($search) { | |
$q->where('name', 'LIKE', '%'.$search.'%' ); | |
} | |
if ($skip && $take) { | |
$q->skip($skip)->take($take); | |
} | |
}])->find(91); | |
return $gallery; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment