ali
ali

Reputation: 117

how can i check ajax request before controller

i have a question about ajax requests. is there a solution to define a rout just for ajax requests before controller?? i always use a Condition like this in the related method:

    public function ajaxRequest(Request $request)
    {
        if ($request->ajax())
        {
            ...
        }
    }

thanks for your help

Upvotes: -1

Views: 281

Answers (1)

save0r
save0r

Reputation: 46

A route cannot be defined only for ajax requests without some type of key.

But ajax requests can be defined before controller without a controller at routes/web.php

Route::get('/crud', function(){
   //get
   return \App\Models\Products::all();
});

Route::post('/crud', function(){
   //set   
   return \App\Models\Products::update([
      'product_name' => (new \Illuminate\Http\Request)['name_product']
   ]);
});

...

Upvotes: 1

Related Questions