$request->ajax is false in insert

Published

I would like to insert data into a database, but in controller is $request->ajax() false. Can anybody help me? I spent a whole day on Google, but nothing helped me. I am using laravel 8. I used ajax for filter books. It works fine. But here I don’t know where the error is. When I use dd($request) before if the result is:

This is .js file:

$(document).ready(function () {
    $("#review-form").submit(function (e) {
        e.preventDefault();

        let reviewText = $("#reviewText").val();
        let bookID = $("#bookID").val();
        let userID = $("#userID").val();

        $.ajax({
            type: "POST",
            url: "catalog/singleBook/" + bookID,
            dataType: "json",
            data: {
                reviewText: reviewText,
                bookID: bookID,
                userID: userID,
                _token: $('meta[name="csrf-token"]').attr("content"),
            },
            success: function (response) {
                alert("X");
                console.log("X");
                $("#review-container").prepend(
                    "<div id='review'>" + response.reviewText + "</div>"
                );
                $("#review-form")[0].reset();
            },
            error: function (error) {
                console.log(error);
                console.log("RREEER");
            },
        });
    });
});

Controller:

 public function storeReview(Request $request) {
      
      dd($request->ajax());
      
      if($request->ajax()) {
      $review = new Review;
      $review->bookID = $request->bookID;
      $review->userID = $request->userID;
      $review->reviewText = $request->reviewArea;
      $review->save();
      
      return response()->json($review);   
      }   
   }

view:

@extends('layouts.main')

@section('title', 'Detail')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="row">
            <div class="singleBook-container">
                <div class="col-sm-2">
                    <img class="singleBook-img" src="{{ $book->img }}" alt=" {{ $book->title }}">
                </div>
                <div class="col-sm-3">
                    <ul class="singleBook-list">
                        <li>
                            <div class="title"> Nazov:</div>
                            <p>{{ $book->title }}</p>
                        </li>
                        <li>
                            <div class="title"> Autor: </div>
                            <p>{{ $book->firstname }} {{ $book->lastname }}</p>
                        </li>
                        <li>
                            <div class="title"> Rating:</div>
                            <p>{{ $book->rating }}</p>
                        </li>
                        <li>
                            <div class="title"> ISBN: </div>
                            <p>{{ $book->ISBN }}</p>
                        </li>
                    </ul>
                </div>
                <div class="col-sm-7">
                    <p>{{ $book->description }}</p>
                </div>
            </div>
        </div>

        <div class="row">
            <form id="review-form" method="Post">
                @csrf
                <div class="form-group">
                    <input type="hidden" value="{{ $book->id }}" id="bookID" name="bookID" readonly />
                    <input type="hidden" value="{{ auth()->user()->id }}" id="userID" name="userID" readonly>
                    <textarea class="form-control" name="reviewArea" id="reviewText exampleFormControlTextarea1"
                        rows="3" placeholder="Napíš recenziu.." required></textarea>
                </div>
                <button id="submitBTN" type="submit" class="btn btn-light">Odoslať</button>
            </form>
        </div>

        <div class="row">
            <h2 class="review-header">Recenzie:</h2>
            <div id="review-container">
            </div>
        </div>
    </div>
</div>
@endsection

web.php:

Route::post('missingbook', [MissingBookController::class, 'storeBook'])->middleware('auth');

Source: Laravel

Published
Categorised as ajax, database, insert, laravel, request Tagged , , , ,

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq