At first my code works fine with just one parameter when querying, but when I add more parameters, it start to throw errors.
Here is MyEditController
file:
class AdminEditController extends Controller { //edit didalam lihat_datafile public function edit(Request $request,$id){ $data = lokasipekerjaan::find($id); $data->mid=$request->mid; $data->subid=$request->subid; $data->pekerjaan=$request->pekerjaan; $data->lokasi=$request->lokasi; $data->uom=$request->uom; $data->cut=$request->cut; $data->fill=$request->fill; $data->area=$request->area; $data->tanggal=$request->tanggal; $data->save(); return redirect()->back(); }
Here is my web.php
:
Route::resource('lokasipekerjaan', AdminEditController::class);
Here is my Blade:
@foreach($data as $datas) <form action="{{route('lokasipekerjaan.edit',[$datas->id])}}" method="PUT"> <tr class="table-info"> <td> <input type="text" value="{{ $datas->id }}" class="" style="background-color: #E0FFFF;width:40px;height:40px;" name="id" :value="old('id')"> </input> </td> <td><input id="{{ $datas->mid }}" type="text" value="{{ $datas->mid }}" class="" style="background-color: #E0FFFF;width:40px;height:40px;" name="mid" :value="old('mid')"></td> <td> <input id="{{ $datas->subid }}" type="text" value="{{ $datas->subid }} " class="" style="background-color: #E0FFFF;width:100px;height:40px;" name="subid" :value="old('subid')"> </td> <td> <input id="{{ $datas->pekerjaan }}" type="text" value="{{ $datas->pekerjaan }} "class="" style="background-color: #E0FFFF;height:40px;" name="pekerjaan" :value="old('pekerjaan')"> </td> <td> <input id="{{ $datas->lokasi }}" type="text" value="{{ $datas->lokasi }} " class="" style="background-color: #E0FFFF;height:40px;" name="lokasi" :value="old('lokasi')"> </td> <td> <input id="{{ $datas->cut }}" type="text" value="{{ $datas->cut }} " class="" style="background-color: #E0FFFF;height:40px;" name="volume" :value="old('cut')"> </td> <td> <input id="{{ $datas->fill }}" type="text" value="{{ $datas->fill }} " class="" style="background-color: #E0FFFF;height:40px;" name="volume" :value="old('fill')"> </td> <td> <input id="{{ $datas->area }}" type="text" value="{{ $datas->area }} " class="" style="background-color: #E0FFFF;height:40px;" name="volume" :value="old('area')"> </td> <td> <input id="{{ $datas->uom }}" type="text" value="{{ $datas->uom }} " class="" style="background-color: #E0FFFF;width:40px;height:40px;" name="uom" :value="old('uom')"> </td> <td> <input value="{{ $datas->tanggal }} " type="text" value="{{ $datas->tanggal }} " class="" style="background-color: #E0FFFF;width:100px;height:40px;" name="tanggal" :value="old('tanggal')"> </td> <td> <input type="hidden" name="_method" value="PUT"> <input type="hidden" name="_token" value="{{csrf_token()}}"> <input type="submit" onclick="return confirm('apakah anda yakin ingin mengedit data ini ?');" class="btn btn-primary" value="Edit"/>@method('PUT') </form></td>
Here is My Controller:
public function lihatdata(){ if(Auth::id()){ $caridata = $_GET['query']; $tanggal_dari = $_GET['tanggaldari']; $tanggal_ke = $_GET['tanggalke']; $filtersubid = dataacuan::select('subid')->whereNotNull('subid')->distinct()->get(); $data = lokasipekerjaan::when($caridata,function($caridatat,$caridata){ return $caridatat->where('cut','LIKE','%'.$caridata.'%') ->orwhere('fill','LIKE','%'.$caridata.'%') ->orwhere('mid','LIKE','%'.$caridata.'%') ->orwhere('subid','LIKE','%'.$caridata.'%') ->orwhere('lokasi','LIKE','%'.$caridata.'%') ->orwhere('id','LIKE','%'.$caridata.'%') ->orwhere('pekerjaan','LIKE','%'.$caridata.'%') ->groupby('subid')->select( DB::raw("DATE(tanggal) as tanggal"), ); })->whereDate('tanggal','>=',$tanggal_dari) ->whereDate('tanggal','<=',$tanggal_ke)->paginate(5); return view('admin.lihat_data',compact('data','caridata','filtersubid','tanggal_dari','tanggal_ke'));}else{ return view('admin.notfound'); } }
When I adding Some Parameter using this code it start showing error:
->select( DB::raw("DATE(tanggal) as tanggal"), ); })->whereDate('tanggal','>=',$tanggal_dari) ->whereDate('tanggal','<=',$tanggal_ke)
When I filter it with date it work fine but when I start to querying with strings it showing this error below:
How Can i fix that please ?
Thank You….
Advertisement
Answer
The parameter needs to be named:
{{route('lokasipekerjaan.edit',['lokasipekerjaan'=>$datas->id])}}
Also make sure you’re getting matches.
$datas->id
is probably null. When paginating you can check $data->total()
if it’s 0 that means there’s no matches when you’re filtering by a keyword.
Edit: So it looks like your when logic overrides the select logic:
->select( DB::raw("DATE(tanggal) as tanggal"), )
In the above, when you before were getting *
, you now no longer get the id field. You should add the fields which you need:
->select( DB::raw("DATE(tanggal) as tanggal, id") )
Edit2:
Regarding the outside date range, there’s no parenthesis within the query that will get generated. You’d want to wrap the searching logic within parenthesis otherwise the OR’s would check whichever is true, and short circuit before the date range check.
Instead of this:
$data = lokasipekerjaan::when($caridata,function($caridatat,$caridata){ return $caridatat->where('cut','LIKE','%'.$caridata.'%') ->orwhere('fill','LIKE','%'.$caridata.'%') ->orwhere('mid','LIKE','%'.$caridata.'%') ->orwhere('subid','LIKE','%'.$caridata.'%') ->orwhere('lokasi','LIKE','%'.$caridata.'%') ->orwhere('id','LIKE','%'.$caridata.'%') ->orwhere('pekerjaan','LIKE','%'.$caridata.'%') ->groupby('subid')->select( DB::raw("DATE(tanggal) as tanggal"), ); })->whereDate('tanggal','>=',$tanggal_dari) ->whereDate('tanggal','<=',$tanggal_ke)->paginate(5);
Wrap the search within a where like so:
$data = lokasipekerjaan::when($caridata,function($caridatat,$caridata){ return $caridatat->where(function($caridatat) use ($caridata) { $caridatat->where('cut','LIKE','%'.$caridata.'%') ->orwhere('fill','LIKE','%'.$caridata.'%') ->orwhere('mid','LIKE','%'.$caridata.'%') ->orwhere('subid','LIKE','%'.$caridata.'%') ->orwhere('lokasi','LIKE','%'.$caridata.'%') ->orwhere('id','LIKE','%'.$caridata.'%') ->orwhere('pekerjaan','LIKE','%'.$caridata.'%') ->groupby('subid')->select( DB::raw("DATE(tanggal) as tanggal"), ); }); })->whereDate('tanggal','>=',$tanggal_dari) ->whereDate('tanggal','<=',$tanggal_ke)->paginate(5);