public function anyEntry(Request $request, $id = null) { if (isset($id) && $id != null) { $company = Company::find($id); if ($company != null) { session(['current_company' => $company->id]); } else { return redirect('companies'); } } else { $id = session('current_company') !== null ? session('current_company') : 0; $address_id = session('current_address') !== null ? session('current_address') : 0; if ($id) { $company = Company::find($id); session(["current_company" => $company->id]); } else { $company = Company::get()->last(); if ($company) { session(["current_company" => $company->id]); } else { $company = new Company(); $company->save(); session(['current_company' => $company->id]); } } } $address = Address::where('module_id', '=', $company->id)->where('module_type', '=', 'App\\Company')->first(); $address_province = isset($address->province_id) ? $address->province_id : 0; $country_province = Province::addSelect('provinces.name as province_name')->where('provinces.id', '=', $address_province)->addSelect('countries.name as country_name')->leftJoin('countries', 'countries.id', '=', 'provinces.country_id')->first(); if ($country_province) { $country_province->toArray(); $company['province_name'] = $country_province['province_name']; $company['country_name'] = $country_province['country_name']; } else { $company['province_name'] = ''; $company['country_name'] = ''; } $countries = array(); $company_type = array(); $countries = Country::with('provinces')->get()->toArray(); $company_type = CompanyType::get()->toArray(); $company->toArray(); $this->layout->content = view('company.entry', ['company' => $company, 'address' => $address, 'countries' => $countries, 'company_type' => $company_type]); }