Answer the question
In order to leave comments, you need to log in
How to fetch data from db using ajax + laravel?
I'm trying to extract the news and likes to the news in the controller, here is the controller. Tell me how to write Ajax correctly so that the likes on the page are updated automatically. I made the post method on Ajax, it adds data to the database, but I don’t understand how to extract it.
public function create()
{
$news = NewsModel::all();
$like = DB::table('like')
->join('news', 'news.id', '=', 'like.news')
->first();
return view('welcome', ['news' => $news, 'like' => $like]);
}
Answer the question
In order to leave comments, you need to log in
AJAX is no different from a regular request.
Create a method in the controller that will receive the necessary data and return it in JSON format. Create a get route for this method.
Make an AJAX request on the created route, get likes in response.
$like = DB::table('like')
->join('news', 'news.id', '=', 'like.news')
->first();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question