/
home
/
sjslayjy
/
public_html
/
ccbfsoution
/
app
/
Http
/
Controllers
/
Admin
/
Upload File
HOME
<?php namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Auth; class CropProtectionController extends Controller { public function index(Request $request) { $user = Auth::user(); $siteId = $user->site_name; $role = $user->role; $selectedSiteId = $request->get('site_id', $siteId); $block = $request->get('block_name'); $keyword = $request->get('search'); // Get site list $sites = ($role == 1) ? DB::table('master_sites')->pluck('site_name', 'id') : DB::table('master_sites')->where('id', $siteId)->pluck('site_name', 'id'); // Get all machines and tractors $allMachines = DB::table('master_machine')->pluck('machine_name', 'id')->toArray(); $allTractors = DB::table('master_tractors')->pluck('tractor_name', 'id')->toArray(); // Get distinct blocks $blocksQuery = DB::table('crop_protection'); if ($role == 1 && $request->filled('site_id')) { $blocksQuery->where('site_id', $selectedSiteId); } elseif ($role != 1) { $blocksQuery->where('site_id', $siteId); } $blocks = $blocksQuery->select('block_name')->distinct()->pluck('block_name'); // Build query $query = DB::table('crop_protection') ->leftJoin('master_chemical', 'crop_protection.chemical_id', '=', 'master_chemical.id') ->select('crop_protection.*', 'master_chemical.chemical_name'); // Site filter if ($role == 1 && $request->filled('site_id')) { $query->where('crop_protection.site_id', $selectedSiteId); } elseif ($role != 1) { $query->where('crop_protection.site_id', $siteId); } // Block filter if ($block) { $query->where('crop_protection.block_name', $block); } // Search filter if ($keyword) { $query->where(function ($sub) use ($keyword) { $sub->where('crop_protection.plot_name', 'like', "%$keyword%") ->orWhere('crop_protection.application_method', 'like', "%$keyword%") ->orWhere('crop_protection.company', 'like', "%$keyword%") ->orWhere('crop_protection.dose', 'like', "%$keyword%") ->orWhere('crop_protection.application_stage_source', 'like', "%$keyword%"); }); } $query = $query->orderBy('crop_protection.id', 'desc')->paginate(10)->appends($request->all()); return view('crop_protection.index', compact( 'query', 'blocks', 'block', 'keyword', 'allMachines', 'allTractors', 'sites', 'selectedSiteId', 'role' )); } }