apl-mhd

hello

Mar 8th, 2019
152
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace App\Http\Controllers\Frontend;
  4.  
  5. use App\Models\Ads;
  6. use App\Models\Brand;
  7. use App\Models\BrandModel;
  8. use App\Models\Category;
  9. use App\Models\FormAttributes;
  10. use App\Models\Package;
  11. use App\Models\SellerProfile;
  12. use App\Models\User;
  13. use Illuminate\Http\Request;
  14. use App\Http\Controllers\Controller;
  15. use Illuminate\Support\Facades\Auth;
  16. use Illuminate\Support\Facades\Validator;
  17.  
  18.  
  19. class AdsController extends Controller
  20. {
  21.     /*helper function for return id name*/
  22.     private $tree = '';
  23.  
  24.     public function parentId($id)
  25.     {
  26.         if (Category::find($id)->parent_id == null)
  27.             return strtolower(Category::find($id)->name);
  28.         return $this->parentId(Category::find($id)->parent_id);
  29.  
  30.  
  31.     }
  32.  
  33.     public function parentTree($id)
  34.     {
  35.         if (Category::find($id)->parent_id == null) {
  36.             $this->tree = Category::find($id)->name . ' > ' . $this->tree;
  37.         } else {
  38.             $this->tree = Category::find($id)->name . ' > ' . $this->tree;
  39.             $this->parentTree(Category::find($id)->parent_id);
  40.  
  41.         }
  42.  
  43.     }
  44.  
  45.     public function homePage()
  46.     {
  47.  
  48.         $adds = Ads::all();
  49.         return view('frontend.layouts.index', compact('adds'));
  50.  
  51.  
  52.     }
  53.  
  54.     public function adCategory()
  55.     {
  56.         $parent_categories = Category::whereNull('parent_id')->get();
  57.  
  58.         return view('frontend.layouts.ads.business_dashboard', compact('parent_categories'));
  59.     }
  60.  
  61.     public function createAd($id)
  62.     {
  63.         $this->parentTree($id);
  64.  
  65.         if (Auth::user()) {
  66.             $user = Auth::user();
  67.             session()->flash('name', $user->name);
  68.             session()->flash('email', $user->email);
  69.         }
  70.  
  71.         //dd($user->id);
  72.         $custom_form = FormAttributes::where('category_id', $id)->get();
  73.         $packages = Package::get();
  74.  
  75.         $parent_category = $this->parentId($id);
  76.         //$brands = Brand::orderby('name', 'asc')->get();
  77.         //$brandModels = BrandModel::orderby('name', 'asc')->get();
  78.         $form_builder = FormAttributes::where('category_id', $id)->with('attributes')->get();
  79. //dd($form_builder);
  80.         $catTree = $this->tree;
  81.         return view('frontend.layouts.ads.create', compact('id', 'custom_form', 'packages', 'parent_category', 'form_builder', 'catTree'));
  82.     }
  83.  
  84.     public function createAdProcess(Request $request)
  85.     {
  86.  
  87.  
  88.         $validator = Validator::make($request->all(), [
  89.             'post-title' => 'required',
  90.             'price' => 'required',
  91.             'example-rd-custom-inline' => 'required',
  92.             'example-rd-custom-primary-lg' => 'required',
  93.             'sell_type' => 'required',
  94.             'condition' => 'required',
  95.             'description' => 'required',
  96.             'img-up' => 'required',
  97.             'img-up.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
  98.             'name' => 'required',
  99.             'password' => 'sometimes|required',
  100.             'email' => 'required',
  101.         ]);
  102.  
  103.         /*If validation failed then redirect back*/
  104.         if ($validator->fails()) {
  105.             return redirect()->back()->withErrors($validator);
  106.         }
  107.  
  108.  
  109.         /* images, specification, packages Convert to Json format*/
  110.         $n = 0;
  111.         foreach ($request->file('img-up') as $i) {
  112.             $input[$n] = rand() . time() . '.' . $i->getClientOriginalExtension();
  113.             $destinationPath = public_path('/images');
  114.             $i->move($destinationPath, $input[$n]);
  115.             $n++;
  116.         }
  117.         $images = json_encode($input, JSON_FORCE_OBJECT);
  118.         $spec = json_encode($request->spec, JSON_FORCE_OBJECT);
  119.         $package = json_encode($request->package, JSON_FORCE_OBJECT);
  120.  
  121.  
  122.         if (!auth()->check()) {
  123.             $user = User::create([
  124.                 'name' => $request->input('name'),
  125.                 'email' => strtolower($request->input('email')),
  126.                 'mobile' => $request->input('mobile'),
  127.                 'password' => bcrypt($request->input('password')),
  128.             ]);
  129.  
  130.             if ($request->input('sell_type') == 'business')
  131.                 SellerProfile::create([
  132.                     'user_id' => $user->id,
  133.                     'contact_person' => $request->input('mobile'),
  134.                 ]);
  135.             $userId = $user->id;
  136.         } else
  137.             $userId = Auth::id();
  138.  
  139.  
  140.         Ads::create([
  141.             'user_id' => $userId,
  142.             'title' => $request->input('post-title'),
  143.             'category_id' => $request->input('category_id'),
  144.             'type' => 1,
  145.             'price' => $request->input('price'),
  146.             'price_type' => 1,
  147.             'images' => $images,
  148.             'description' => $request->input('description'),
  149.             'contact_name' => $request->input('name'),
  150.             'contact_email' => $request->input('email'),
  151.             'contact_phone' => '01719453438',
  152.             'contact_division' => $request->input('division'),
  153.             'contact_district' => $request->input('district'),
  154.             'contact_location' => $request->input('location'),
  155.             'specification' => $spec,
  156.             'package_id' => $package,
  157.         ]);
  158.         return redirect('create_ad/' . $request->input('category_id'))->with("message", "Post added successfully");
  159.  
  160.     }
  161.  
  162.  
  163.     public function viewAd($id)
  164.     {
  165.         //$add = Ads::with('user')->find($id);
  166.         $add = Ads::find($id);
  167.         $profile = SellerProfile::find($add->user_id);
  168.  
  169.         return view('frontend.layouts.ads.ad-view', compact('add', 'profile'));
  170.     }
  171.  
  172.     public function listings()
  173.     {
  174.         return view('frontend.layouts.ads.listings');
  175.     }
  176.  
  177.  
  178.     public function brandModelLoad()
  179.     {
  180.  
  181.  
  182.     }
  183. }
RAW Paste Data