A
A
Alexander2019-01-09 11:59:54
Design patterns
Alexander, 2019-01-09 11:59:54

How is it architecturally correct in Laravel to design a method that is called both through the action of the controller and through ajax?

Hello.
Prompt, please, on the following question:
There is a ready project in which there is an action:
[email protected]
Action marks news in a DB for certain needs.

public function makeRevision(){
        $ids = $request->input('ids');
        $news = News::whereIn('id', $ids)->get();

         ... некая логика и заполнение БД ...

        Session::flash('status', 'Complete');
        return back();
}

There is a post request path for this action: Route::post('/makerevision', '[email protected]')->name('makerevision'
) this action? (i.e. where can it be taken out?)
There is no service layer in the project.
For an ajax request, you need to return a json response, not a redirect back and setting the status to the session ..
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad, 2019-01-09
@Result007

As an option, check in the controller for ajax request and return json

if ($request->ajax()) {
    ...
 }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question