ขอคำแนะนำ และวิธิการแก้ไขในกรณี query ข้อมูลเยอะ ๆ
ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา ขอคำแนะนำ และวิธิการแก้ไขในกรณี query ข้อมูลเยอะ ๆ
ขอคำแนะนำ และวิธิการแก้ไขในกรณี query ข้อมูลเยอะ ๆ
Copy
กรณี ที่ผมคลิ๊กดู subitem มันโหลดข้อมูลนานมากครับ จนค้างไปเลยก็มี ข้อวิธีแก้ไขหน่อยครับ Laravel ครับ
controller
view
controller
<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppModelsUser; use AppModelsRole; use AppModelsRole_Log; use AppModelsLog; use AppModelsTeam; use AppModelssub_team; use AppModelsCompany; use AppModelsCommission; use AppModelsFile; use AppModelsSetting; use AppImportsUsersImport; use MaatwebsiteExcelFacadesExcel; use Session; // use File; use Auth; use DB; use IlluminateSupportArr; use Validator; class UserController extends Controller { public function __construct() { $this->middleware('auth'); } /** * Display a listing of the resource. * * @return IlluminateHttpResponse */ public function index() { $count = 0; if ( Auth::user()->role()->name == 'Admin' || Auth::user()->role()->name == 'Authorizer' || Auth::user()->role()->name == 'Support' ) { $data = []; $teams = Team::get(); foreach ($teams as $key => $team) { $subteams = User::where('team_id', $team->id)->where('department_id', 14)->count(); if ($subteams == 0 || $subteams == null) { continue; } // $subteams = DB::connection('user')->table('users')->where('team_id', $team->id)->distinct("sub_team_id")->get(); $subteams = DB::connection('user')->select(" SELECT DISTINCT(sub_team_id) FROM users WHERE team_id = $team->id AND department_id = 14 "); $data[$key]['teams'] = $team; foreach ($subteams as $sub_key => $subteam) { if ($subteam->sub_team_id == 0) { continue; } $users = User::where('sub_team_id', $subteam->sub_team_id) ->where('team_id', $team->id) ->where('department_id', 14) ->get(); $data[$key]['subteams'][$sub_key]['sub'] = User::where('department_id', 14) ->find($subteam->sub_team_id); if (empty($data[$key]['subteams'][$sub_key]['sub'])) { $data[$key]['subteams'][$sub_key]['sub'] = User::where('department_id', 14)->where('team_id', $team->id)->where('sub_team_id', $subteam->sub_team_id)->first(); } $data[$key]['subteams'][$sub_key]['users'] = $users; // $data[$key]['subteams'][$sub_key]['users']->roles = $users->role(); $count += count($users); } } // dd([$data, $count]); } else { $user = Auth::user(); $data = []; $sub = User::where('sub_team_id', $user->id)->where('team_id', $user->team_id)->where('department_id', 14)->count(); if ($sub == 0) { return $this->show($user->id); } $users = User::with('roles')->where('sub_team_id', $user->id)->where('team_id', $user->team_id)->where('department_id', 14)->get(); foreach ($users as $key => $value) { $s = User::with('roles')->where('sub_team_id', $value->id)->where('team_id', $user->team_id)->where('department_id', 14)->count(); if ($s == 0) { $count++; $data['users'][] = $value; continue; } $data['subteams'][$key]['sub'] = $value; $data['subteams'][$key]['users'] = User::with('roles')->where('sub_team_id', $value->id)->where('team_id', $user->team_id)->where('department_id', 14)->get(); $count++; } $data['leader'] = User::find($user->id); } return view('pages.sales.index')->withAgents($data)->withCount($count); } public function findSubTeam($sub_id) { $data = []; $data[$key]['subteams'][$sub_key]['sub'] = User::with('roles') ->where('department_id', 14) ->find($sub_id); if (empty($data[$key]['subteams'][$sub_key]['sub'])) { $data[$key]['subteams'][$sub_key]['sub'] = User::with('roles')->where('department_id', 14)->where('team_id', $team->id)->first(); } $data[$key]['subteams'][$sub_key]['users'] = $users; return $data; } /** * Show the form for creating a new resource. * * @return IlluminateHttpResponse */ public function create() { $roles = Role::where('name', '!=', 'Admin')->where('department_id', 14)->get(); $users = []; // $users = User::where('team_id', 2)->get(); // $users = Arr::pluck(sub_team::get(), 'thai_name', 'id'); $teams = Team::get(); $subteam = []; foreach ($teams as $key => $team) { $name = []; foreach ($roles as $role) { $users = User::where('team_id', $team->id)->where('department_id', 14)->where('position_id', $role->id)->get(); foreach ($users as $i => $user) { if ($i == 0) { $name[$i] = 'ไม่มีหัว'; } $name[$user->id] = $user->name_th; // if ($user->roles) { $name[$user->id] = $role->short_code . ' - ' . $user->name_th; // } } } $subteam[$key] = $name; } return view('pages.sales.create') ->withRoles(Arr::pluck($roles, 'name', 'id')) ->withSubTeam($subteam) ->withTeams(Arr::pluck($teams, 'name', 'id')) ->withUser(Arr::pluck(User::get(), 'name_th', 'id')) ->withCompanies(Arr::pluck(Company::get(), 'company_th', 'id')); } /** * Store a newly created resource in storage. * * @param IlluminateHttpRequest $request * @return IlluminateHttpResponse */ public function store(Request $request) { //dd($request); $user = User::where('idcard', $request->idcard)->count(); if ($user > 0) { Session::flash('status', 'เลขบัตรประชาชนซ้ำ'); return redirect()->back(); } $sale = new User; $sale->name_th = $request->name_th; $sale->name_eng = $request->name_eng; $sale->email = $request->email; $sale->idcard = str_replace('-', '', $request->idcard); $sale->lineid = $request->lineid; $sale->address = $request->address; // $sale->soi = $request->soi; $sale->district_id = $request->district_id; $sale->amphur_id = $request->amphur_id; $sale->province_id = $request->province_id; // $sale->zipcode = $request->zipcode; $sale->bank_name = $request->bank_name; $sale->bank_account = $request->bank_account; $sale->phone = str_replace('-', '', $request->phone); $sale->department_id = 14; $sale->position_id = $request->position_id; // $sale->created_at = date('Y-m-d', strtotime(str_replace('/', '-', $request->created_at))); $year = date('Y', strtotime(str_replace('/', '-', $request->created_at))); $sale->team_id = $request->team_id; $sale->company_id = $request->company_id; $sale->password = bcrypt('123456'); $sale->sub_team_id = $request->sub_id; // $year = date('y', strtotime($request->join_date)); $year = $year[2] . $year[3]; // $year = '62'; $company = str_pad($request->company_id, 2, '0', STR_PAD_LEFT); // code = ปีที่เข้างาน บริษํท Team รหัสพนังงาน $sale->code = $year . $company . $request->team_id; $sale->active = 1; $sale->save(); // $sale->roles()->sync($request->position_id); $emp = str_pad($sale->id, 5, '0', STR_PAD_LEFT); $sale->code = $year . $company . $request->team_id . $emp; DB::table('role_user')->insert([ 'role_id' => $request->position_id, 'user_id' => $sale->id ]); //dd($sale); $sale->save(); $log = new Log; $log->action = 'Store'; $log->new = $sale; $log->user_id = Auth::id(); $log->save(); if ($request->hasFile('files')) { foreach ($request->file('files') as $key => $value) { $path = $value->store('public/file'); $file = new File; $file->name = $value; $path = str_replace('public', 'storage', $path); $file->url = config('app.url') . '/' . $path; $file->user_id = $sale->id; $file->save(); } } Session::flash('status', 'Success'); // DB::connection('report')->insert("INSERT INTO sub_sale SET // sale_code = $sale->code , // sale_nameth = '$sale->thai_name' , // sale_position = 'Agent' , // startdate = '$sale->join_date', // resign = '$sale->join_date', // team = 'VP1', // team_leader = '', // sid = '36', // sale_level = 3 // "); // $this->checkPromote($request->sub_team_id); return redirect()->route('users.edit', $sale->id); } public function checkPromote($sub_team_id) { $sub_team = sub_team::find($sub_team_id); $user = User::find($sub_team->user_id); $users = User::where('sub_team_id', $sub_team->id)->get(); $sale = 0; $tl = 0; $mg = 0; $sm = 0; $avp = 0; $vp = 0; $md = 0; $promote = false; foreach ($users as $value) { $role = $value->role()->first(); if ($role->short_code == 'AGENT') { $sale++; } elseif ($role->short_code == 'TL') { $tl++; } elseif ($role->short_code == 'MG') { $mg++; } elseif ($role->short_code == 'SM') { $sm++; } elseif ($role->short_code == 'AVP') { $avp++; } elseif ($role->short_code == 'VP') { $vp++; } elseif ($role->short_code == 'MD') { $md++; } } if ( $sale >= $user->role()->SALE && $tl >= $user->role()->TL && $mg >= $user->role()->MG && $sm >= $user->role()->SM && $avp >= $user->role()->AVP && $vp >= $user->role()->VP ) { $promote = true; } if ($promote) { $role_before_id = $user->role()->id; if ($user->role()->short_code == 'AGENT') { $user->roles()->sync(Role::where('name', 'Team Leader')->first()->id); } elseif ($user->role()->short_code == 'TL') { $user->roles()->sync(Role::where('name', 'Manager')->first()->id); } elseif ($user->role()->short_code == 'MG') { $user->roles()->sync(Role::where('name', 'Senior Manager')->first()->id); } elseif ($user->role()->short_code == 'SM') { $user->roles()->sync(Role::where('name', 'Assistant Vice President')->first()->id); } elseif ($user->role()->short_code == 'AVP') { $user->roles()->sync(Role::where('name', 'Vice President')->first()->id); } elseif ($user->role()->short_code == 'VP') { $user->roles()->sync(Role::where('name', 'Managing Director')->first()->id); } DB::table('role_log')->insert([ 'user_id' => $user->id, 'role_before_id' => $role_before_id, 'role_after_id' => $user->role()->id, 'action' => 'up', 'updated_at' => date('Y-m-d H:i') ]); } } /** * Display the specified resource. * * @param int $id * @return IlluminateHttpResponse */ public function show($id) { $user = User::find($id); return view('pages.sales.show') ->withSale($user) // ->withCommissions(Commission::where('user_id', $id)->where('approved', 1)->get()) ->withCommissions(Commission::where('user_id', $id)->get()) ->withRole($user->role()); } /** * Show the form for editing the specified resource. * * @param int $id * @return IlluminateHttpResponse */ public function edit($id) { $user = User::findOrfail($id); if (Auth::user()->role()->name == 'Admin' || Auth::user()->role()->name == 'Support') { $roles = Role::where('name', '!=', 'Admin')->where('department_id', 14)->get(); $users = []; $roles = $roles->toArray(); $subteam = []; $teams = Team::get(); foreach ($teams as $key => $value) { $users = User::where('department_id', 14) ->where('team_id', $value->id)->get(); foreach ($users as $i => $user) { if ($i == 0) { $subteam[$value->id][$i] = 'ไม่มีหัว'; } $key = array_search($user->position_id, array_column($roles, 'id')); if ($key >= 0) { $subteam[$value->id][$user->id] = $roles[$key]['short_code'] . ' - ' . $user->name_th; } else { } } } $files = File::where('user_id', $id)->get(); $userabc = []; $userInTeam = User::where('sub_team_id', $id)->where('department_id', 14)->get(); foreach ($userInTeam as $userSub) { $userabc[] = $userSub; $a = User::where('sub_team_id', $userSub->id)->where('department_id', 14)->get(); foreach ($a as $b) { $userabc[] = $b; } } return view('pages.sales.edit') ->withSale(User::find($id)) ->withRoles(Arr::pluck($roles, 'name', 'id')) ->withSubTeam($subteam) ->withTeams(Arr::pluck($teams, 'name', 'id')) ->withUser(Arr::pluck(User::get(), 'name_th', 'id')) ->withFiles($files) ->withUserInTeam($userabc) ->withCompanies(Arr::pluck(company::get(), 'company_th', 'id')); } else { if (Auth::id() == $id) { $files = File::where('user_id', Auth::id())->get(); return view('pages.users.edit')->withSale(User::find($id))->withFiles($files); } else { return abort(404); } } } /** * Update the specified resource in storage. * * @param IlluminateHttpRequest $request * @param int $id * @return IlluminateHttpResponse */ public function password(Request $request, $id) { $validate = Validator::make($request->all(), [ 'password' => 'required', 'c_password' => 'required|same:password', ]); if ($validate->fails()) { Session::flash('status', 'รหัสผ่านไม่เหมือนกัน'); return redirect()->back(); } $sale = User::find($id); $sale->password = bcrypt($request->password); $sale->save(); Session::flash('status', 'Success'); return redirect()->route('users.index'); } public function update(Request $request, $id) { $sale = User::find($id); $log = new Log; $log->action = 'Update'; $log->old = $sale; // $sale->update($request->all()); $sale->name_th = $request->name_th; $sale->name_eng = $request->name_eng; $sale->email = $request->email; $sale->idcard = str_replace('-', '', $request->idcard); $sale->lineid = $request->lineid; $sale->address = $request->address; $sale->district_id = $request->district_id; $sale->amphur_id = $request->amphur_id; $sale->province_id = $request->province_id; $sale->bank_name = $request->bank_name; $sale->bank_account = $request->bank_account; $sale->phone = str_replace('-', '', $request->phone); $sale->department_id = 14; $sale->position_id = $request->position_id; $sale->phone = str_replace('-', '', $request->phone); $sale->team_id = $request->team_id; $sale->created_at = date('Y-m-d', strtotime(str_replace('/', '-', $request->created_at))); // $sale->sub_team_id = $request->sub_team_id; $sale->sub_team_id = $request->sub_id; DB::table('role_user') ->where('user_id', $sale->id) ->update([ 'role_id' => $request->position_id, ]); $sale->save(); $log->new = $sale; $log->user_id = Auth::id(); $log->save(); if (Auth::user()->role()->name != 'Support' && Auth::id() != $sale->id) { // $sale->join_date = isset($request->join_date) ? date('Y-m-d', strtotime(str_replace('/', '-', $request->join_date))) : $sale->join_date; // $sale->team_id = isset($request->team_id) ? $request->team_id : $sale->team_id; // $sale->company_id = isset($request->company_id) ? $request->company_id : $sale->company_id; // $sale->sub_team_id = isset($request->sub_team_id) ? $request->sub_team_id : $sale->sub_team_id; } // $sale->save(); // $sale->roles()->sync($request->role_id); if ($request->hasFile('files')) { foreach ($request->file('files') as $key => $value) { $path = $value->store('public/file'); $file = new File; $file->name = $value; $path = str_replace('public', 'storage', $path); $file->url = config('app.url') . '/' . $path; $file->user_id = $sale->id; $file->save(); } } if ($request->hasFile('image_profile')) { $path = $request->file('image_profile')->store('public/agents'); $path = str_replace('public', 'storage', $path); $sale->img_user = config('app.url') . '/' . $path; $sale->save(); } if ($request->sub_team_id) { $sale->sub_team_id = $request->sub_team_id; $sale->save(); } Session::flash('status', 'Success'); return redirect()->route('users.index'); } /** * Remove the specified resource from storage. * * @param int $id * @return IlluminateHttpResponse */ public function destroy($id) { $user = User::find($id); $user->files()->detach(); // $user->sub_team()->detach(); DB::table('sub_teams')->where('user_id', $id)->delete(); $log = new Log; $log->action = 'delete'; $log->old = $user; $log->save(); $user->delete(); Session::flash('status', 'Success'); return redirect()->back(); } public function reject(Request $request, $id) { $role_log = Role_Log::find($id); $role_log->reject = 1; $role_log->save(); Session::flash('status', 'Success'); return redirect()->back(); } public function approve(Request $request, $id) { $role_log = Role_Log::find($id); $setting = Setting::where('name', 'approve')->first(); $user = User::find($role_log->user_id); $role_log->status = $role_log->status + 1; if ($setting->value == $role_log->status) { $subteam = sub_team::find($user->sub_team_id); if ($user->sub_team_id != null) { $subteam->user_id = sub_team::find($user->sub_team_id); $leader = User::find($subteam->user_id)->first(); if ($leader->role()->id == $role_log->role_after_id) { $leader->commission = 10; $leader->save(); } } $role_log->approve = 1; // $user->roles()->sync($role_log->role_after_id); } $role_log->save(); Session::flash('status', 'Success'); return redirect()->back(); } public function importExcel(Request $request) { Excel::import(new UsersImport, $request->file('files')); return redirect()->back(); } public function getimportExcel() { return view('pages.sales.import'); } public function createOther() { $roles = Role::where('department_id', 0)->get(); return view('pages.sales.other.create') ->withRoles(Arr::pluck($roles, 'name', 'id')); } public function storeOther(Request $request) { $user = User::where('code', $request->user_name)->count(); if ($user > 0) { Session::flash('status', 'User_name ซ้ำ'); return redirect()->back(); } $user = new User; $user->name_th = $request->name_th; $user->name_eng = $request->name_eng; $user->code = $request->user_name; $user->department_id = 0; $user->position_id = $request->position_id; $user->password = bcrypt('123456'); $user->active = 1; $user->save(); Session::flash('status', 'Success'); return redirect('/dashboard'); } public function search(Request $request) { $users = DB::connection('user') ->table('users') ->join('tb_position', 'users.position_id', 'tb_position.id') ->select(['users.*', 'tb_position.name as role_name']) ->where('users.department_id', 14); if ($request->name) { $users->where('name_th', 'LIKE', '%' . $request->name . '%'); } if ($request->code) { $users->where('code', 'LIKE', '%' . $request->code . '%'); } if ($request->idcard) { $users->where('idcard', 'LIKE', '%' . $request->idcard . '%'); } if ($request->phone) { $users->where('phone', 'LIKE', '%' . $request->phone . '%'); } $users = $users->get(); return view('pages.sales.result')->withAgents($users)->withCount(count($users)); if (count($users) == 1) { $user = Arr::first($users); return $this->show(Arr::first($user)); } if (count($users) == 0) { return view('pages.sales.index')->withAgents([])->withCount(0); } // foreach ($users as $key => $value) { // $s = User::with('roles')->where('sub_team_id', $value->id)->where('team_id', $user->team_id)->where('department_id', 14)->count(); // if ($s == 0) { // $count++; // $data['users'][] = $value; // continue; // } // $data['subteams'][$key]['sub'] = $value; // $data['subteams'][$key]['users'] = User::with('roles')->where('sub_team_id', $value->id)->where('team_id', $user->team_id)->where('department_id', 14)->get(); // $count++; // } // return view('pages.sales.index')->withAgents($users)->withCount(count($users)); } public function getListAgents(Request $request) { if (($request->team_id)) { $team = Team::find($request->team_id); $users = DB::connection('user') ->table('users') ->join('tb_position', 'users.position_id', 'tb_position.id') ->select(['users.*', 'tb_position.name as role_name']) ->where('users.department_id', 14) ->where('team_id', $team->id) ->where('sub_team_id', 0); return view('pages.sales.sub')->withSubs($users->get())->withCount(count($users)); } elseif (isset($request->sub_team_id)) { $users = DB::connection('user') ->table('users') ->join('tb_position', 'users.position_id', 'tb_position.id') ->select(['users.*', 'tb_position.name as role_name']) ->where('users.department_id', 14) ->where('sub_team_id', $request->sub_team_id); return view('pages.sales.sub')->withSubs($users->get())->withCount(count($users)); } } }
view
@php $role = Auth::user()->role()->name; @endphp <h2>Total {{ $count }}</h2> <table class="table table-responsive table-hover"> <thead> <tr> <th>No.</th> <th>Code</th> <th width="50%" class="text-left">Sale Name</th> <th width="10%">Potision</th> @if($role == 'Admin' || $role == 'Authorizer' || $role == 'Support' ) <th width="1%">Actions</th> @endif </tr> </thead> @php $z = 0; @endphp @foreach ($agents as $key => $item) <tbody> <tr class="clickable " data-toggle="collapse" data-target=".group-of-rows-{{$key}}" aria-expanded="false" aria-controls="group-of-rows-{{$key}}"> <td><i class="fas fa-plus"></i></td> <td colspan="4">{{$item['teams']->name}}</td> </tr> </tbody> @isset($item['subteams']) @foreach ($item['subteams'] as $subteams) @isset($subteams['sub']) <tbody class="collapse group-of-rows-{{$key}}"> <tr class="clickable leader " data-toggle="collapse" data-target=".list-of-rows-{{$z}}" aria-expanded="false" aria-controls="list-of-rows-{{$z}}"> <td><i class="fas fa-plus"></i></td> <td>{{$subteams['sub']->code}}</td> <td>{{$subteams['sub']->name_th}}</td> {{-- <td>{{ ($subteams['sub']->role()) ? $subteams['sub']->role()->name : '' }}</td> --}} <td></td> <td class="text-center"> <ul class="m-auto p-0 list-unstyled"> <li class="dropdown action-menu"> <a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown"> <i class="fas fa-ellipsis-h pointer-cursor"></i> </a> <div class="dropdown-menu dropdown-menu-right"> <a href="{{url('/users/')}}/{{$subteams['sub']->id}}/edit" class="dropdown-item">Edit</a> @if ($role == 'Admin') <div class="dropdown-divider"></div> {{Form::open(['route' => ['users.destroy', $subteams['sub']->id] , 'method' => 'delete'])}} {{Form::submit('Remove' , ['class' => 'dropdown-item'])}} {{Form::close()}} @endif </div> </li> </ul> </td> </tr> </tbody> @foreach ($subteams['users'] as $i => $subteam) @if ($subteams['sub']->code == $subteam->code) @php continue; @endphp @endif <tbody class="collapse list-of-rows-{{$z}}"> <tr> <td><i class="fas fa-minus"></i></td> <td>{{$subteam->code}}</td> <td>{{$subteam->name_th}}</td> {{-- <td>{{ ($subteam->role()) ? $subteam->role()->name : '' }}</td> --}} <td>1</td> <td> <ul class="m-auto p-0 list-unstyled"> <li class="dropdown action-menu"> <a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown"> <i class="fas fa-ellipsis-h pointer-cursor"></i> </a> <div class="dropdown-menu dropdown-menu-right"> <a href="{{url('/users/')}}/{{$subteam->id}}/edit" class="dropdown-item">Edit</a> @if ($role == 'Admin') <div class="dropdown-divider"></div> {{Form::open(['route' => ['users.destroy', $subteam->id] , 'method' => 'delete'])}} {{Form::submit('Remove' , ['class' => 'dropdown-item'])}} {{Form::close()}} @endif </div> </li> </ul> </td> </tr> </tbody> @endforeach @endisset @php $z++; @endphp @endforeach @endisset @endforeach </table>
ิbondsanti
26-08-2022
15:05:58
คำแนะนำ และการใช้งาน
สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก
- ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
เว็บไซต์ของเราให้บริการเนื้อหาบทความสำหรับนักพัฒนา โดยพึ่งพารายได้เล็กน้อยจากการแสดงโฆษณา
โปรดสนับสนุนเว็บไซต์ของเราด้วยการปิดการใช้งานตัวปิดกั้นโฆษณา (Disable Ads Blocker) ขอบคุณครับ