/**
  * Store a newly created resource in storage.
  *
  * @param Request $request
  * @return Response
  */
 public function store(AdRequest $request)
 {
     $attributes = ['customer_id' => Auth::customer()->get()->id];
     $category = Category::find($request->get('category_id'));
     if ($category->isDescendantOf(Category::find(1))) {
         // motors
         $motor = Motor::create($request->only('chassis_no', 'model', 'color', 'doors'));
         $ad = $motor->advertisements()->create(array_merge($attributes, $request->only(['name', 'pin', 'address', 'emirate_id', 'phone'])));
     } else {
         $ad = Advertisement::create(array_merge($attributes, $request->only(['name', 'pin', 'address', 'emirate_id', 'phone'])));
     }
     // array_merge = add two arrays together
     $product = Product::create($request->only(['title', 'description', 'brand', 'category_id', 'price']));
     $ad->product()->save($product);
     $source = public_path() . '/uploads/temp/' . Session::getId() . '/';
     $destination = public_path() . '/uploads/ads/' . $ad->id . '/';
     if (!file_exists($destination)) {
         mkdir($destination, 0777, true);
         // create directory if doesn't exists
     }
     if (file_exists($source)) {
         // check if directory exists
         $files = scandir($source);
         // list files in directory
         $delete = [];
         foreach ($files as $file) {
             // in_array() = check for values in array
             if (in_array($file, ['.', '..'])) {
                 continue;
             }
             if (copy($source . $file, $destination . $file)) {
                 $delete[] = $source . $file;
                 //                $ad->images()->create([
                 //                    'customer_id' => Auth::customer()->get()->id,
                 //                    'advertisement_id' => $ad->id,
                 //                    'url' => url('/uploads/ads/' . $ad->id . '/' . $file)
                 //                ]);
                 $product->images()->create(['url' => url('/uploads/ads/' . $ad->id . '/' . $file)]);
             }
         }
         foreach ($delete as $file) {
             unlink($file);
             // delete file
         }
     }
     return response()->json('success');
 }
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store(Request $request)
 {
     //        return response()->json($request->all());
     $category = Category::find($request->get('category_id'));
     if ($category->isDescendantOf(Category::find(1))) {
         // motors
         $motor = Motor::create($request->only('chassis_no', 'model', 'color', 'doors'));
         $globex = Globex::create($request->only('stock'));
         $motor->globexs()->save($globex);
     } else {
         $globex = Globex::create($request->only('stock'));
     }
     $product = Product::create($request->only(['title', 'description', 'brand', 'category_id', 'price']));
     $globex->product()->save($product);
     $files = Input::file('images');
     $dir = public_path() . '/uploads/products/' . $globex->id . '/';
     if (!File::exists($dir)) {
         mkdir($dir, 0777, true);
     }
     foreach ($files as $file) {
         if ($file != null) {
             $filename = sha1($file->getClientOriginalName() . time());
             $extension = $file->getClientOriginalExtension();
             $imgdata = ['product_id' => $product->id, 'url' => url('/uploads/products/' . $globex->id . '/' . $filename . '.' . $extension)];
             $product->images()->create($imgdata);
             $file->move($dir, $filename . '.' . $extension);
         }
     }
     return redirect()->back()->with('message', 'Created');
 }