Support the ongoing development of Laravel.io →
posted 3 years ago
Laravel
Last updated 2 years ago.
0
moderator

Is there anything in your log file?

0

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).......................

0
moderator

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?

0

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'))

0
moderator

You can return the view directly like so:

$staffs = \DB::table('Staff')->get();

return view('myview', compact('staffs'));
0

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.

0
moderator

Can you send over the contents of TtableController.php line 43 as per the error above please?

0

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

0

code for returning response on controller is: return response()->view('admin/ttable.create_ttable', compact('sss')->compact('staffs')->compact('clazs')->compact('tts'));

0
moderator

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

1

Thx, Joedixon,500 error gone, but now I'm unable to get my 'sss' variable on my view . undefined variable error. I'm using it as below on my view @if(isset($sss)) {{$sss}} @endif

0
moderator

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?

0

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'));
0
moderator

I can't see where the if block ends, but does if ($request->ajax()) actually return true?

0

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'));

 }
 
  }
0
moderator

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?

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Zahid zahidzee Joined 24 Aug 2021

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.