Exemple #1
0
 private function seedTags()
 {
     $faker = \Faker\Factory::create();
     Model::unguard();
     for ($i = 0; $i < 10; $i++) {
         Tag::create(array('title' => $faker->word, 'created_at' => $faker->dateTimeBetween($startDate = '-1 years', $endDate = 'now'), 'updated_at' => $faker->dateTimeBetween($startDate = '-1 years', $endDate = 'now')));
     }
 }
 /**
  * Run the migrations.
  *
  * @return void
  */
 public function up()
 {
     Schema::table('tags', function (Blueprint $table) {
         $table->string('slug', 255);
     });
     $query = Tag::where('slug', '=', '');
     $tags = $query->get();
     if ($tags) {
         foreach ($tags as $tag) {
             $tag->slug = Str::slug($tag->title);
             $tag->save();
         }
     }
 }
 /**
  *   Return a view listing all of the projects
  *
  */
 public function index()
 {
     // Get all the projects
     $projects = Project::with('tags')->orderBy('created_at', 'DESC')->get();
     // Loop through all of the projects and concatenate the tags together as a single string - keeps the template clean
     foreach ($projects as $project) {
         $collectTags = '';
         foreach ($project->tags as $tag) {
             $collectTags .= '-' . str_slug($tag->title) . ' ';
         }
         $project->allTags = $collectTags;
     }
     // Get all the tags
     $tags = Tag::with('projects')->orderBy('created_at', 'DESC')->get();
     // We only need tags that have a relationship with a project
     // Use Eloquent's filter method, allowing only tags with relationships to Projects are be returned
     $tagsFiltered = $tags->filter(function ($tag) {
         if (isset($tag->projects) && count($tag->projects) > 0) {
             return $tag;
         }
     });
     // Return view along with projects and filterestags
     return view('portfolio::index')->with(['projects' => $projects, 'tags' => $tagsFiltered]);
 }
 /**
  * Update existing record of model
  *
  * @param $class Class of model to save data as (Eloquent Model)
  * @param $id ID of record to update
  * @param $request data to save to model (Illuminate Request)
  *
  * @return data collection of newly updated record as JSON response (Http Response)
  */
 public function update($class, $id, $request)
 {
     // Modify some of the input data
     $this->modifyRequestData($request);
     // Return errors as JSON if request does not validate against model rules
     $v = Validator::make($request->all(), $class->rules($id));
     if ($v->fails()) {
         return response()->json($v->errors(), 422);
     }
     // Find the item to update
     $collection = $class::find($id);
     // Check if the item exists
     if (!$collection) {
         // Return an error if not
         return response()->json(['errors' => [$this->messages['not_found']]], 422);
     } else {
         // Update the item with request data
         $collection->fill($request->all());
         // Check if the data saved OK
         if (!$collection->save()) {
             // Fail - Return error as JSON
             return response()->json(['errors' => [$this->messages['error_updating']]], 422);
         } else {
             // Save sections
             if (count($request->sections) > 0) {
                 $sectionsToUpdate = [];
                 foreach ($request->sections as $section) {
                     $newSection = Section::find($section['id']);
                     // if no existing model is found, create a new section
                     if (!$newSection) {
                         $newSection = new Section();
                     }
                     $newSection->fill($section);
                     array_push($sectionsToUpdate, $newSection);
                 }
                 $collection->sections()->saveMany($sectionsToUpdate);
             }
             // Save tags
             if (count($request->tags) > 0) {
                 $tagsToUpdate = [];
                 foreach ($request->tags as $tag) {
                     if (isset($tag['id'])) {
                         // check if a tag can be found by ID
                         $newTag = Tag::find($tag['id']);
                     } else {
                         // Check if the model can be found by slug (prevents duplicate tags)
                         $query = Tag::where('slug', '=', Str::slug($tag['title']));
                         $newTag = $query->first();
                     }
                     // if no existing model is found, create a new tag
                     if (!$newTag) {
                         $newTag = new Tag();
                         $newTag->fill($tag);
                         // Save the tag
                         $newTag = $collection->tags()->save($newTag);
                     }
                     array_push($tagsToUpdate, $newTag->id);
                 }
                 $collection->tags()->sync($tagsToUpdate);
             }
             // Save pages
             if (count($request->pages) > 0) {
                 $pagesToUpdate = [];
                 foreach ($request->pages as $page) {
                     $newPage = Page::find($page['id']);
                     // if no existing model is found, create a new section
                     if (!$newPage) {
                         $newPage = new Page();
                     }
                     $newPage->fill($page);
                     array_push($pagesToUpdate, $newPage);
                 }
                 $collection->pages()->saveMany($pagesToUpdate);
             }
             // Success - Return item ID as JSON
             return response()->json($collection, 200);
         }
     }
 }
 /**
  * Search Projects
  *
  * @returns Illuminate response (JSON list of projects)
  */
 public function search($request)
 {
     $term = '%' . $request->get('query') . '%';
     $collection = Tag::orderBy('updated_at', 'DESC')->where('title', 'LIKE', $term)->get();
     return response()->json($collection);
 }