A Ghosal A Ghosal - 4 months ago 18x
PHP Question

Don't know how Ajax - "result" to place in @foreach() - [in a Laravel project]

I don't know how to use "result" array in @foreach()
Please tell me what is wrong in it.
And is there any better way to do this ?
This is in a Laravel 5.2 project

+' <option value="">--- Sub Category ---</option>'
+' @foreach('result' as $subCategoryItem)'
+' <option value="{{$subCategoryItem->id}}">{{$subCategoryItem->category}}</option>'
+' @endforeach'

The result is coming from the controller below -

public function subCategorySelect(){
$category_id = trim(Input::get('category_id'));
$subCategoryData = DB::table('categories')
return $subCategoryData;

Please help me...............


Well mainly it's impossible becouse AJAX request is the part of the client side such as JavaScript and blade templates are the part of server side such as PHP. So what you can do is, create a blade template with:

<option value="">--- Sub Category ---</option>'
@foreach($result as $subCategoryItem)'
    <option value="{{$subCategoryItem->id}}">{{$subCategoryItem->category}}</option>

then you have to retunr this template for AJAX request (in routes.php):

Routes::get('sub_items', function () {
    $results = //here you have to recive categories somehow
    return view('the_template_above', compact('results'));

And the the JS part will looks like: