Example #1
0
 public function run()
 {
     DB::table('giraffes')->delete();
     $faker = Faker\Factory::create('en_US');
     for ($x = 0; $x < 150; $x++) {
         Giraffe::create(['name' => $faker->name, 'age' => $faker->numberBetween(1, 15), 'height' => $faker->numberBetween(400, 580), 'desc' => $faker->paragraph(3), 'alive' => $faker->boolean(), 'lang' => $faker->randomElement(['fr', 'en']), 'zookeeper_id' => $faker->numberBetween(1, 3)]);
     }
 }
Example #2
0
 /**
  * Execute the entities list command, and return
  * a file name (if command type is "download"),
  * an array of data for a view (case "view"),
  * or nothing.
  *
  * @param \Dvlpp\Sharp\ListView\SharpEntitiesListParams $entitiesListParams
  * @return mixed
  */
 function execute(SharpEntitiesListParams $entitiesListParams)
 {
     $query = Giraffe::with('zookeeper')->where("zookeeper_id", $entitiesListParams->getCurrentSublistId());
     if ($entitiesListParams->getSearch()) {
         foreach ($entitiesListParams->getSearchTerms() as $term) {
             $query->where(function ($query) use($term) {
                 $query->orWhere("name", "like", $term)->orWhere('desc', 'like', $term);
             });
         }
     }
     if ($entitiesListParams->getSortedColumn()) {
         $query->orderBy($entitiesListParams->getSortedColumn(), $entitiesListParams->getSortedDirection());
     }
     $giraffes = $query->get();
     // Code omitted: generate a CSV file with $giraffes
     // ...
     return public_path("tmp/giraffes.csv");
 }
Example #3
0
 /**
  * Paginate instances.
  *
  * @param $count
  * @param \Dvlpp\Sharp\ListView\SharpEntitiesListParams $params
  * @return mixed
  */
 function paginate($count, SharpEntitiesListParams $params)
 {
     $giraffes = Giraffe::with('zookeeper')->where("lang", $this->lang)->where("zookeeper_id", $this->getCurrentSublistId());
     if ($params->getSearch()) {
         if ($params->isAdvancedSearch()) {
             foreach ($params->getSearch() as $field => $value) {
                 switch ($field) {
                     case "age":
                         $ageComp = $params->getAdvancedSearchValue("age_comp");
                         $giraffes->where("age", $ageComp, $value);
                         break;
                     case "name":
                         foreach (explode_search_words($value) as $term) {
                             $giraffes->where("name", "like", $term);
                         }
                         break;
                     case "particularities":
                         foreach ($value as $v) {
                             $giraffes->whereExists(function ($query) use($v) {
                                 $query->select('giraffe_id')->from('giraffes_particularities')->whereRaw('giraffes_particularities.giraffe_id = giraffes.id ' . 'AND giraffes_particularities.particularity_id=' . $v);
                             });
                         }
                 }
             }
         } else {
             // Quicksearch
             foreach (explode_search_words($params->getSearch()) as $term) {
                 $giraffes->where(function ($query) use($term) {
                     $query->orWhere("name", "like", $term)->orWhere('desc', 'like', $term);
                 });
             }
         }
     }
     if ($params->getSortedColumn()) {
         $giraffes->orderBy($params->getSortedColumn(), $params->getSortedDirection());
     }
     return $giraffes->paginate($count);
 }