/** * Store a newly created resource in storage. * * @return Response */ public function store(Adtype $adtype, AdRequest $request) { $geo = new GeolocationHelper($request->get('geolocation_data')); $adstatus = Adstatus::whereTitle('pending')->first(); $user = User::findOrFail($request->get('user_id')); $category = Category::findOrFail($request->get('category_id')); $geolocation = new Geolocation(); $geolocation->fill($geo->get()); $content = new Adcontent(); $content->fill($request->get('content')); $ad = new Ad(); $ad->user()->associate($user); $ad->category()->associate($category); $ad->adstatus()->associate($adstatus); $ad->adtype()->associate($adtype); $ad->price = $this->getPrice($ad, $request); event(new AdWillBeCreated($ad, $content, $geolocation, $this->admin)); $ad->save(); $ad->geolocation()->save($geolocation); $ad->content()->save($content); if ($ad->adtype->can_add_pic) { $this->syncAdPhotos($ad, $request); } if ($ad->adtype->can_add_video) { $this->syncAdVideos($ad, $request); } $this->syncAdFields($ad, $request); event(new AdWasCreated($ad, $this->admin)); return redirect()->route('zxadmin.ad.edit', $ad->id); }
/** * Update the specified resource in storage. * * @param int $id * * @return Response */ public function update(Ad $ad, UpdateAdUserRequest $request) { if (!$ad->adtype->can_edit) { return ['adId' => null]; } $oldStatus = $ad->adstatus->title; $geo = new GeolocationHelper($request->get('geolocation_data')); $adstatus = Adstatus::whereTitle('pending')->first(); $category = Category::visible()->findOrFail($request->get('category_id')); $ad->category()->associate($category); $ad->adstatus()->associate($adstatus); $ad->geolocation->fill($geo->get()); $ad->content->fill($request->get('content')); $ad->price = $this->getPrice($ad, $request); event(new AdWillBeUpdated($ad, $this->user)); $ad->save(); $ad->geolocation->save(); $ad->content->save(); if ($ad->adtype->can_update_pic) { $this->syncAdPhotos($ad, $request); } if ($ad->adtype->can_update_video) { $this->syncAdVideos($ad, $request); } $this->syncAdFields($ad, $request); if ($oldStatus != 'pending') { event(new AdWasUpdated($ad, $this->user)); } return ['adId' => $ad->id]; }