0

I am trying to pass the ID value from the blade file into ajax so it the data table will be routed and will call the ProductActivities function in ProductController.

Here's my snippet code for show() function in ProductController:

public function show($id)
{
    $product = Product::find($id);
    $data = Product::with(['user_modify'], 'id', $product->user_modified)
                    ->where('product_id', '=', $id)->first();
                    
    $category = Category::select('category_name')
                        ->where('category_id', '=', $data->product_type)
                        ->pluck('category_name')
                        ->first();

    if($data->count() > 0){
        return view('product.view', compact('data', 'category'));
    }else{
        Toastr::error('Product cannot be retrieved.', 'Error');
        return view('product.index');
    }
}

And here's the snippet code of JavaScript for the DataTable initialization in view.blade.php file:

@push('js')
<script>
  $(function () {
    
    $("#prod_log_tbl").DataTable({
      responsive:true,
      stateSave:false,
      scrollY:true,
      autoWidth: false,
      ajax: {{ url('product/activities', [Request::segment(3)]) }},
      order:[0, 'desc'],
      searchable: false, 
      sortable:false,
      fixedColumns: true
    });
  });
  </script>
@endpush

line of code for route in web.php:

Route::get('product/activities/{id}', 'Master\ProductController@ProductActivities')->name('product/activities/{id}');

snippet code for the ProductActivities() function in ProductController:

public function ProductActivities($id)
{
    $dataAct = Activity::all()->where('subject_id', '=', $id);
    return Datatables::of($dataAct)->make(true);
}

And here's the result of my current progress:

enter image description here

In the screenshot of result the URL that ajax shows has additional values after the ID which I think causes of the DataTable error.

I don't know How I am getting this error? How can I implement the passing of ID from the view blade file through DataTable ajax into the ProductController?

P.S. I use Yajra/DataTable package for Laravel.

1
  • Datatables accept collection, not the model instance, you need to make collection $data = Product::with(['user_modify'], 'id', $product->user_modified) ->where('product_id', '=', $id)->get(); and this one $dataAct = Activity::where('subject_id', '=', $id)->get(); Commented Sep 27, 2020 at 18:13

1 Answer 1

1

I think you do not need php echo in you Ajax url, route helper syntax is

{{ route('routeName', ['id' => 1]) }}

you need route name and parameter, another way is using url helper

{{ url('product/activities/', [Request::segment(3)]) }}

Beside this if you want to use model refer documentation, using first() will give you only one object, you need a collection, better considering get().

Sign up to request clarification or add additional context in comments.

4 Comments

gives me this error in javascript: Uncaught SyntaxError: Unexpected token ':' and this output in the url became: ajax: 127.0.0.1:8000/product/activities/104,
@Taguro unexpected token comes because of some syntax error, please post your updated code and also what is the expected url let me know.
updated my code for view blade file (DataTable initialization)
@Taguro It seems you need to wrap your url with quotes. "{{ url('product/activities', [Request::segment(3)]) }}" because you are assigning a string here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.