Example #1
0
 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];
 }
Example #2
0
 public function create(Project $project)
 {
     $projects = Project::get();
     //sends an array of  'first_name' => 'id'
     $subs = Sub::where('employee', '=', 1)->lists('first_name', 'id');
     //where 'employee refers to if they're a GS employee..not an ID
     //$clients = Client::lists('first_name', 'id'); //sends an array of  'first_name' => 'id'
     return view('hours.create', compact('projects', 'subs'));
 }
Example #3
0
 public function store($subName, $postSlug, Request $request)
 {
     $sub = Sub::where('name', $subName)->firstOrFail();
     $post = Post::where('slug', $postSlug)->where('sub_id', $sub->id)->firstOrFail();
     $comment = Comment::create($request->all());
     $comment->post()->associate($post);
     $comment->user()->associate(auth()->user());
     $comment->save();
     return redirect()->route('sub.post.show', [$sub->name, $post->slug]);
 }
Example #4
0
 public function vote($subName, $slug, $value)
 {
     if (!in_array($value, [-1, 0, 1])) {
         abort(400);
     }
     $sub = Sub::where('name', $subName)->firstOrFail();
     $post = Post::where('slug', $slug)->where('sub_id', $sub->id)->firstOrFail();
     try {
         $vote = $post->votes()->where('user_id', auth()->id())->firstOrFail();
     } catch (ModelNotFoundException $e) {
         $vote = new Vote();
     }
     $vote->value = $value;
     $vote->user()->associate(auth()->user());
     $vote->voteable()->associate($post);
     $vote->save();
     $post->score += $value;
     $post->save();
 }
Example #5
0
 public function show($name)
 {
     $sub = Sub::where('name', $name)->firstOrFail();
     $posts = $sub->posts()->sort(Request::get('sort'))->simplePaginate(self::RESULTS_PER_PAGE);
     return view('sub.show', compact('sub', 'posts'));
 }