yes in log file : 2021-10-18 16:36:29] local.ERROR: Call to a member function compact() on array {"exception":"[object] (Error(code: 0): Call to a member function compact() on array at E:\xampp\htdocs\laravelapp\app\Http\Controllers\Admin\TtableController.php:43).......................
Seems like you're trying to pass an array to the compact
method.
From the PHP docs:
For each of these, compact() looks for a variable with that name in the current symbol table and adds it to the output array such that the variable name becomes the key and the contents of the variable become the value for that key. In short, it does the opposite of extract().
Rather than using compact, can you just return the array directly?
im passing these variables with return view: $sss=$request->ses_id; ....it is the ajax response $staffs = \DB::table('Staff')->get(); ... table data so, how to send it ? compact('staffs') or with(compact('staffs'))
You can return the view directly like so:
$staffs = \DB::table('Staff')->get();
return view('myview', compact('staffs'));
Yes using same return response()->view('myview', compact('ajaxdata')); but still getting 500 error, but when I return response with Jason() it works but with view it gives error.
Can you send over the contents of TtableController.php
line 43 as per the error above please?
You return your data from controller back to ajax with return response()->json(), and get the response back,with that your app should not throw error
code for returning response on controller is: return response()->view('admin/ttable.create_ttable', compact('sss')->compact('staffs')->compact('clazs')->compact('tts'));
You don't need to chain the compact
functions like that. The following should work for you:
return view('admin/ttable.create_ttable', compact('sss', 'staffs', 'clazs', 'tts'));
zahidzee liked this reply
I'm not sure how that can return a 500 when you are first checking the variable is set. Is it definitely that line causing the issue?
if($request->ajax()) { $sss=$request->ses_id; ------------------- this is ajax data i want this on my view
$staffs = \DB::table('Staff')->get();
$clazs = \DB::table('claz')->get();
$tts = \DB::table('ttable')->get();
return response()->view('admin/ttable.create_ttable', compact('sss','staffs','clazs','tts'));
I can't see where the if block ends, but does if ($request->ajax())
actually return true?
this is the whole function... yes it shows the ajax post data in network tab in inspect ...
public function createTtable(Request $request) {
if($request->ajax())
{
$sss=$request->ses_id;
$staffs = \DB::table('Staff')->get();
$clazs = \DB::table('claz')->get();
$tts = \DB::table('ttable')->get();
return response()->view('admin/ttable.create_ttable', compact('sss','staffs','clazs','tts'));
}
else
{
$staffs = \DB::table('Staff')->get();
$clazs = \DB::table('claz')->get();
$tts = \DB::table('ttable')->get();
return view('admin/ttable.create_ttable')->with(compact('staffs','clazs','tts'));
}
}
Laravel will only see the request as an ajax request if the relevant headers are set.
This is the check it does: return 'XMLHttpRequest' == $this->headers->get('X-Requested-With');
If you add a dd('ajax');
in the if section and dd('not ajax');
in the else, which is triggered?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community