コード例 #1
0
 public function get_category($get_category_id, $get_subcategory_id, $search_term)
 {
     $sub_categories = Sub_category::all();
     $arr = array();
     if ($get_category_id == 0 && $get_subcategory_id == 0) {
         foreach ($sub_categories as $sub_category) {
             array_push($arr, $sub_category->id);
         }
     } else {
         if ($get_category_id != 0 && $get_subcategory_id == 0) {
             foreach ($sub_categories as $sub_category) {
                 if ($sub_category->categories_id == $get_category_id) {
                     array_push($arr, $sub_category->id);
                 }
             }
         } else {
             if ($get_category_id != 0 && $get_subcategory_id != 0) {
                 array_push($arr, $get_subcategory_id);
             }
         }
     }
     if ($search_term != null) {
         $items = Items::whereIn('sub_categories_id', $arr)->where('title', 'like', '%' . $search_term . '%')->paginate(12);
     } else {
         $items = Items::whereIn('sub_categories_id', $arr)->paginate(12);
     }
     return $items;
 }
コード例 #2
0
 function checkout(DeliveryFormRequest $request)
 {
     if (isset($_COOKIE['basket'])) {
         $orders = $_COOKIE['basket'];
         $orders = json_decode($orders);
     } else {
         return redirect('/basket');
     }
     $ids = [];
     $amount = [];
     $total_cost = 0;
     $order_id = Orders::latest()->first();
     //получаем последний заказ
     empty($order_id) ? $order_id = 1 : ($order_id = $order_id->order_id + 1);
     //определяемся с новым заказом, увеличивая номер последнего заказа на 1
     foreach ($orders as $order) {
         $ids[] = $order->item_id;
         //создаем массив из id заказанных товаров
         $amount[$order->item_id] = $order->amount;
         //создаем массив с количеством каждого товара ['id товара'=>'количество товара']
     }
     $items = Items::whereIn('id', $ids)->get();
     foreach ($items as $item) {
         $orders = Orders::create(['item_id' => $item->id, 'price' => $item->price, 'order_id' => $order_id, 'amount' => $amount[$item->id], 'name' => $request->name, 'address' => $request->address, 'phone' => $request->phone]);
         //сохраняем заказ в базу
         $total_cost = $total_cost + $item->price * $amount[$item->id];
     }
     setcookie('basket', '');
     $orders = Orders::where('order_id', $orders->order_id)->get();
     return view('finish_order', ['orders' => $orders, 'total' => $total_cost]);
 }