コード例 #1
0
 /**
  * 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
             $sectionsToUpdate = [];
             if ($request->sections) {
                 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);
             }
             // Success - Return item ID as JSON
             return response()->json($collection, 200);
         }
     }
 }
コード例 #2
0
 /**
  * Save a new record of model
  *
  * @param $class Class of model to save data as (Eloquent Model)
  * @param $request data to save to model (Illuminate Request)
  *
  * @return data collection of newly saved record as JSON response (Http Response)
  */
 public function storeSection($class, $id, $request)
 {
     // Modify some of the input data
     $this->modifyRequestData($request);
     $section = new Section();
     // Return errors as JSON if request does not validate against model rules
     $v = Validator::make($request->all(), $section->rules());
     if ($v->fails()) {
         return response()->json($v->errors(), 422);
     }
     $collection = $class::find($id);
     // Update the item with request data
     $section->fill($request->all());
     // Check if the data saved OK
     if (!$collection->sections()->save($section)) {
         // Fail - Return error as JSON
         return response()->json(['errors' => [$this->messages['error_updating']]], 422);
     } else {
         // Success - Return item ID as JSON
         return response()->json($section, 200);
     }
 }
コード例 #3
0
ファイル: Seed.php プロジェクト: dan-powell/portfolio
 private function seedProjects()
 {
     $faker = \Faker\Factory::create();
     Model::unguard();
     // create some Portfolio Items
     for ($i = 0; $i < 20; $i++) {
         Project::create(array('created_at' => $faker->dateTimeBetween($startDate = '-1 years', $endDate = 'now'), 'updated_at' => $faker->dateTimeBetween($startDate = '-1 years', $endDate = 'now'), 'title' => $faker->sentence(rand(2, 5)), 'slug' => $faker->slug, 'seo_title' => $faker->sentence(rand(1, 4)), 'seo_description' => $faker->paragraph(1), 'markup' => $faker->paragraph(rand(3, 8)), 'featured' => $faker->randomElement([0, 1])));
     }
     // Add some Sections to projects
     for ($i = 0; $i < 40; $i++) {
         Section::create(array('markup' => $faker->paragraph(rand(3, 8)), 'attachment_id' => $faker->numberBetween(1, 20), 'attachment_type' => 'DanPowell\\Portfolio\\Models\\Project'));
     }
     // Assign some tags to projects
     for ($i = 0; $i < 16; $i++) {
         DB::table('taggables')->insert(array('tag_id' => $faker->numberBetween(1, 10), 'taggable_id' => $faker->numberBetween(1, 20), 'taggable_type' => 'DanPowell\\Portfolio\\Models\\Project'));
     }
 }
コード例 #4
0
 /**
  * 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);
         }
     }
 }