This question has been asked here: How can I return a view from an AJAX call in Laravel 5? And a few other questions. The answers are a mixed bag of responses with top answers not working for me. I'll explain all below.
I currently have an Ajax call returning a successful response when I do this:
public function search (Request $request)
{
$output = echo '<td>Notes</td>';
echo '<td colspan="8" id="orderNotes"><input type="text" name="notes" style="width: 100%;"></td>';
echo '<td><button class="btn btn-info btn-fill btn-sm" id="saveNotes" data-orderNumber="';
// a bunch of more echos
return Response($output);
}
This causes a bunch of problems for me because I'm manually typing out each echo. I would instead like to replace it by returning a view.
From the answered above it would appear that this would be the correct answer:
public function search(Request $request)
{
$title = "just testing";
$view = view("pages.orders-display",compact('title'))->render();
return response()->json(['html'=>$view]);
}
I created a view called orders-display. The path is views/pages/orders-display.blade.php this gives me an internal 500 error.
The above answer I pointed out has this as the highest agreed upon answer but it also doesn't work for me.
public function search(Request $request)
{
$userjobs = 'this is a test';
$returnHTML = view('pages.orders-display')->with('userjobs', $userjobs)->render();
return response()->json(array('success' => true, 'html'=>$returnHTML));
}
Any ideas what I'm doing wrong?