public function update($id, MaterialRequest $request) { $material = Material::findOrFail($id); $material->name = $request->input('name'); $material->type = $request->input('type'); $material->status = $request->input('status'); $material->updated_by = Auth::user()->id; $material->updated_at = time(); $material->update(); Session()->flash('flash_message', 'Material has been updated!'); return redirect('materials'); }
public function store(MaterialRequest $request) { // make new material $material = Material::create(); $material->user_id = Auth::user()->id; $material->private = 0; if (isset($request->title)) { $material->title = trim(ucwords($request->title)); $material->slug = str_slug($request->title, '-'); } if (isset($request->objective)) { $material->objective = trim(ucfirst(strtolower($request->objective))); } if (isset($request->target_language)) { $material->target_language = trim(ucfirst(strtolower($request->target_language))); } if (isset($request->time_needed_prep)) { $material->time_needed_prep = $request->time_needed_prep; } else { $material->time_needed_prep = 0; } if (isset($request->time_needed_class)) { $material->time_needed_class = $request->time_needed_class; } else { $material->time_needed_class = 0; } if (isset($request->materials)) { $material->materials = trim(ucfirst(strtolower($request->materials))); } if (isset($request->procedure_before)) { $material->procedure_before = trim(ucfirst($request->procedure_before)); } if (isset($request->procedure_in)) { $material->procedure_in = trim(ucfirst($request->procedure_in)); } // store files $dir = Auth::user()->username; $path = public_path() . '/users/' . $dir; $empty = count(glob("{$path}/*")) === 0 ? true : false; if (!$empty) { $files = []; foreach (glob("{$path}/*") as $filename) { array_push($files, $filename); } MaterialFile::store($files, $material); } if (isset($request->category_list)) { // add category to categories table foreach ($request->Input('category_list') as $category) { $material_category = Auth::user()->material_category()->firstOrCreate(['category' => title_case($category), 'slug' => str_slug($category, '-')]); // add category to users pivot $material_category->users()->attach($request->input('id')); // add category to materials pivot table category / material //$material_category->materials()->attach($material->id); $material->categories()->attach($material_category->id); } } if (isset($request->level)) { foreach ($request->level as $level) { $material_level = MaterialLevel::firstOrCreate(['level' => title_case($level), 'slug' => str_slug($level, '-')]); // add level to materials pivot $material->levels()->attach($material_level->id); } } if (isset($request->language_focus)) { //add language focuses to material_language_focuses database foreach ($request->language_focus as $lf) { $material_language_focus = MaterialLanguageFocus::firstOrCreate(['language_focus' => title_case($lf), 'slug' => str_slug($lf, '-')]); // add language focus to materials pivot $material->languageFocuses()->attach($material_language_focus->id); } } if (isset($request->activity_use)) { //add activity use to material_activity_uses database foreach ($request->activity_use as $au) { $material_activity_use = MaterialActivityUse::firstOrCreate(['activity_use' => title_case($au), 'slug' => str_slug($au, '-')]); // add activity use to materials pivot $material->activityUses()->attach($material_activity_use->id); } } if (isset($request->pupil_task)) { //add pupil task to material_pupil_tasks database foreach ($request->pupil_task as $pt) { $material_pupil_task = MaterialPupilTask::firstOrCreate(['pupil_task' => title_case($pt), 'slug' => str_slug($pt, '-')]); // add pupil task use to materials pivot $material->pupilTasks()->attach($material_pupil_task->id); } } if (isset($request->book)) { $book = MaterialBook::firstOrCreate(['book' => $request->book[0], 'slug' => str_slug($request->book[0], '-')]); $material->book_id = $book->id; } if (isset($request->page)) { $material->page = trim($request->page); } if (isset($request->follow_up)) { //update material $material->follow_up = trim(ucfirst($request->follow_up)); } if (isset($request->variations)) { $material->variations = trim(ucfirst($request->variations)); } if (isset($request->tips)) { $material->tips = trim(ucfirst($request->tips)); } if (isset($request->notes)) { $material->notes = trim(ucfirst($request->notes)); } $material->update(); return redirect()->action('Admin\\UserController@home'); }