public static function subscribe($id, Request $request) { $idsToInsert = []; $idsToUpdate = []; $idsToDelete = []; if ($request->has('id')) { $input = $request->all(); $idsToInsert = []; $idsToUpdate = []; foreach ($input['id'] as $key => $value) { if (!$value) { $lunch = Lunch::create(['user_id' => $id, 'name' => $input['name'][$key], 'meal1_id' => $input['meal1'][$key], 'meal2_id' => $input['meal2'][$key], 'garnish_id' => $input['garnish'][$key], 'salad_id' => $input['salad'][$key], 'drink_id' => $input['drink'][$key], 'price' => $input['price'][$key]]); $lunch->additions()->sync($input['additions'][$key] ? explode(',', $input['additions'][$key]) : []); Sub::create(['user_id' => $id, 'lunch_id' => $lunch->id, 'day' => $input['day'][$key], 'quantity' => $input['quantity'][$key]]); $idsToInsert[] = (string) $lunch->id; } else { $lunch = Lunch::findOrFail($value); $lunch->price = $input['price'][$key]; $lunch->save(); $sub = Sub::where('lunch_id', $value)->firstOrFail(); $sub->quantity = $input['quantity'][$key]; $sub->save(); $idsToUpdate[] = $value; } } $idsToDelete = Sub::where('user_id', $id)->whereNotIn('lunch_id', array_merge($idsToUpdate, $idsToInsert))->get()->lists('lunch_id'); Lunch::whereIn('id', $idsToDelete)->delete(); Sub::whereIn('lunch_id', $idsToDelete)->delete(); } else { $lunchToDelete = Sub::where('user_id', $id)->get()->lists('lunch_id'); Lunch::whereIn('id', $lunchToDelete)->delete(); Sub::whereIn('lunch_id', $lunchToDelete)->delete(); } return ['insert' => $idsToInsert, 'update' => $idsToUpdate, 'delete' => $idsToDelete]; }
public function store(StoreSubRequest $request) { $sub = Sub::create($request->all()); $sub->owner()->associate(auth()->user()); $sub->save(); return redirect()->route('sub.show', $sub->name); }
public function store(SubRequest $request) { if ($request->hasFile('file')) { $file = $request->file('file'); $name = time() . 'liability_certificate.' . $file->guessClientExtension(); $file->move('docs/insurance', $name); Sub::create($request->all()); $file = $request->file('file'); $name = time() . '_liability_certificate.' . $file->guessClientExtension(); $url = Sub::latest()->first(); $url->liablity_url = $name; $url->save(); } Sub::create($request->all()); return redirect('clients'); }