Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers\Frontend;
- use App\Models\Ads;
- use App\Models\Brand;
- use App\Models\BrandModel;
- use App\Models\Category;
- use App\Models\FormAttributes;
- use App\Models\Package;
- use App\Models\SellerProfile;
- use App\Models\User;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Validator;
- class AdsController extends Controller
- {
- /*helper function for return id name*/
- private $tree = '';
- public function parentId($id)
- {
- if (Category::find($id)->parent_id == null)
- return strtolower(Category::find($id)->name);
- return $this->parentId(Category::find($id)->parent_id);
- }
- public function parentTree($id)
- {
- if (Category::find($id)->parent_id == null) {
- $this->tree = Category::find($id)->name . ' > ' . $this->tree;
- } else {
- $this->tree = Category::find($id)->name . ' > ' . $this->tree;
- $this->parentTree(Category::find($id)->parent_id);
- }
- }
- public function homePage()
- {
- $adds = Ads::all();
- return view('frontend.layouts.index', compact('adds'));
- }
- public function adCategory()
- {
- $parent_categories = Category::whereNull('parent_id')->get();
- return view('frontend.layouts.ads.business_dashboard', compact('parent_categories'));
- }
- public function createAd($id)
- {
- $this->parentTree($id);
- if (Auth::user()) {
- $user = Auth::user();
- session()->flash('name', $user->name);
- session()->flash('email', $user->email);
- }
- //dd($user->id);
- $custom_form = FormAttributes::where('category_id', $id)->get();
- $packages = Package::get();
- $parent_category = $this->parentId($id);
- //$brands = Brand::orderby('name', 'asc')->get();
- //$brandModels = BrandModel::orderby('name', 'asc')->get();
- $form_builder = FormAttributes::where('category_id', $id)->with('attributes')->get();
- //dd($form_builder);
- $catTree = $this->tree;
- return view('frontend.layouts.ads.create', compact('id', 'custom_form', 'packages', 'parent_category', 'form_builder', 'catTree'));
- }
- public function createAdProcess(Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'post-title' => 'required',
- 'price' => 'required',
- 'example-rd-custom-inline' => 'required',
- 'example-rd-custom-primary-lg' => 'required',
- 'sell_type' => 'required',
- 'condition' => 'required',
- 'description' => 'required',
- 'img-up' => 'required',
- 'img-up.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
- 'name' => 'required',
- 'password' => 'sometimes|required',
- 'email' => 'required',
- ]);
- /*If validation failed then redirect back*/
- if ($validator->fails()) {
- return redirect()->back()->withErrors($validator);
- }
- /* images, specification, packages Convert to Json format*/
- $n = 0;
- foreach ($request->file('img-up') as $i) {
- $input[$n] = rand() . time() . '.' . $i->getClientOriginalExtension();
- $destinationPath = public_path('/images');
- $i->move($destinationPath, $input[$n]);
- $n++;
- }
- $images = json_encode($input, JSON_FORCE_OBJECT);
- $spec = json_encode($request->spec, JSON_FORCE_OBJECT);
- $package = json_encode($request->package, JSON_FORCE_OBJECT);
- if (!auth()->check()) {
- $user = User::create([
- 'name' => $request->input('name'),
- 'email' => strtolower($request->input('email')),
- 'mobile' => $request->input('mobile'),
- 'password' => bcrypt($request->input('password')),
- ]);
- if ($request->input('sell_type') == 'business')
- SellerProfile::create([
- 'user_id' => $user->id,
- 'contact_person' => $request->input('mobile'),
- ]);
- $userId = $user->id;
- } else
- $userId = Auth::id();
- Ads::create([
- 'user_id' => $userId,
- 'title' => $request->input('post-title'),
- 'category_id' => $request->input('category_id'),
- 'type' => 1,
- 'price' => $request->input('price'),
- 'price_type' => 1,
- 'images' => $images,
- 'description' => $request->input('description'),
- 'contact_name' => $request->input('name'),
- 'contact_email' => $request->input('email'),
- 'contact_phone' => '01719453438',
- 'contact_division' => $request->input('division'),
- 'contact_district' => $request->input('district'),
- 'contact_location' => $request->input('location'),
- 'specification' => $spec,
- 'package_id' => $package,
- ]);
- return redirect('create_ad/' . $request->input('category_id'))->with("message", "Post added successfully");
- }
- public function viewAd($id)
- {
- //$add = Ads::with('user')->find($id);
- $add = Ads::find($id);
- $profile = SellerProfile::find($add->user_id);
- return view('frontend.layouts.ads.ad-view', compact('add', 'profile'));
- }
- public function listings()
- {
- return view('frontend.layouts.ads.listings');
- }
- public function brandModelLoad()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement