Can't get a variable inside function query() and use it.
Route::get('posts/{category_id}', ['as' => 'post', 'uses' => 'PostController@index']);
public function index(PostsDataTable $dataTable, $category_id)
{
return $dataTable->render('dashboard.posts.index', ["category_id" => $category->id]);
}
protected function getBuilderParameters()
{
return [
'order' => [0,'desc'],
'lengthMenu' => [10,25,50],
'ajax' => ['category_id' => $category_id], //doesn't work
];
}
protected function query()
{
$query = Post::query()
->join('categories', 'categories.id', '=', 'posts.category_id')
->where('posts.category_id', $this->request()->get('category_id')) //doesn't work
->select([
'posts.id', 'categories.name', 'title', 'published', 'published_at', 'published_by'
]);
return $this->applyScopes($query);
}
ajax: {
url: 'myRoute',
data: {"category_id": '{{$category_id}}'},
type: "POST"
},
I am looking for the way to pass variable: category_id to postsDataTable without using javascript and use this variable at the function query().
This is how I do it on my project. Just add a method on your dataTable class like ->forCategory($cat) setter.
return $dataTable->forCategory($cat)
->render('dashboard.posts.index', ["category_id" => $category->id]);
DataTable class:
protected $category;
public function forCategory($cat) {
$this->category = $cat;
return $this;
}
Thank you, it worked!
Just what I was looking for!
For access to value?
public function query()
{
$categ = $this->category
}
Most helpful comment
This is how I do it on my project. Just add a method on your dataTable class like
->forCategory($cat)setter.DataTable class: