I am beginner of laravel. I am involving in a small project and facing some issue. I want to get the id from the function 'getVoucher' in order to display it in my function 'index'. But I have tried my code at the below, I can't get any id from it. The dd displayed 'null' and I don't know any other solution. Any comments are appreciated. Thanks in advance.
public function getVoucher(Request $request)
{
$id = $request->id;
$voucher = Voucher::where('id','=',$id)->first();
return $voucher;
}
public function index(Request $request)
{
if(session()->has('LoggedAdmin')) {
$admin = Admin::where('id','=',session('LoggedAdmin'))->first();
$data = [
'LoggedAdminInfo' => $admin
];
}
if ($request->ajax()) {
$data = Voucher::latest()->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = '<a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Display" >Display</a>';
$btn = $btn. '<a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Edit" >Edit</a>';
$btn = $btn.' <a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Delete" ><i ></i></a>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
$voucher = $this->getVoucher($request);
$voucherId = $voucher->id;
dd($voucher);
$voucherStatus = VoucherStatus::where('voucher_id','=', $voucherId)->get();
return view('pages.voucher', $data, compact('voucherStatus'));
}
CodePudding user response:
you have to use self:: not $this. like this:
public function getVoucher(Request $request)
{
$id = $request->id;
$voucher = Voucher::where('id','=',$id)->first();
return $voucher;
}
public function index(Request $request)
{
if(session()->has('LoggedAdmin')) {
$admin = Admin::where('id','=',session('LoggedAdmin'))->first();
$data = [
'LoggedAdminInfo' => $admin
];
}
if ($request->ajax()) {
$data = Voucher::latest()->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = '<a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Display" >Display</a>';
$btn = $btn. '<a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Edit" >Edit</a>';
$btn = $btn.' <a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Delete" ><i ></i></a>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
$voucher = self::getVoucher($request);
$voucherId = $voucher->id;
dd($voucher);
$voucherStatus = VoucherStatus::where('voucher_id','=', $voucherId)->get();
return view('pages.voucher', $data, compact('voucherStatus'));
}
or even better, you can do this instead:
public function index(Request $request)
{
if(session()->has('LoggedAdmin')) {
$admin = Admin::where('id','=',session('LoggedAdmin'))->first();
$data = [
'LoggedAdminInfo' => $admin
];
}
if ($request->ajax()) {
$data = Voucher::latest()->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = '<a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Display" >Display</a>';
$btn = $btn. '<a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Edit" >Edit</a>';
$btn = $btn.' <a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Delete" ><i ></i></a>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
$voucher = Voucher::find($request->id);
$voucherId = $voucher->id;
dd($voucher);
$voucherStatus = VoucherStatus::where('voucher_id','=', $voucherId)->get();
return view('pages.voucher', $data, compact('voucherStatus'));
}
CodePudding user response:
public function getVoucher(Request $request)
{
$id = $request->id;
$voucher = Voucher::where('id','=',$id)->first();
$this->index($voucher->id);`enter code here`
}
public function index(Request $request, $voucher)
{
if(session()->has('LoggedAdmin')) {
$admin = Admin::where('id','=',session('LoggedAdmin'))->first();
$data = [
'LoggedAdminInfo' => $admin
];
}
if ($request->ajax()) {
$data = Voucher::latest()->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = '<a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Display" >Display</a>';
$btn = $btn. '<a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Edit" >Edit</a>';
$btn = $btn.' <a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id.'" data-original-title="Delete" ><i ></i></a>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
dd($voucher);