在发出Ajax请求Laravel时抛出错误

时间:2018-06-04 02:36:53

标签: php jquery ajax laravel laravel-5

当我在Ajax中执行请求时,它会给我这个错误。

这是它向我投掷的新错误:

"message": "Undefined variable: user_id",
    "exception": "ErrorException",
    "file": "/Users/danhergir/Desktop/startup/app/Http/Controllers/ProductReviewController.php",
    "line": 113,
    "trace":

错误在于:

$like->user_id = $user_id;

我可以做些什么来解决它?

这是我的路线。

Web.php

Route::post('/like', [
        'uses' => 'ProductReviewController@postLike',
        'as' => 'like'
    ]);

这是我的视图文件。 (这是我喜欢/不喜欢按钮的部分)

Show.blade.php

<div class="container">
        <div class="row">
            <div class="col-md-6" id="user-reviews">
            <h3>Recent comments</h3>
                @forelse($product->reviews as $review)
                <div class="mt-5 border border-dark pl-3 pt-3 pb-3 mb-3 rounded reviewid" data-reviewid="{{ $review->id }}">
                    <div class="title">
                        <h4>{{ $review->headline }}</h4>
                    </div>
                    <div class="user-rating">
                        <star-rating class="pr-3" :star-size="20" :read-only="true" :show-rating="false" :rating="{{ $review->rating }}"></star-rating>
                    </div>
                    <div class="body-text pt-3 pr-5">
                        <p style="text-align:justify"><strong>{{ $review->description }}</strong></p>
                    </div>
                    <div class="body-text pt-3">
                        <h6>
                            <a href="#" class="btn btn-xs btn-warning like">Like</a>
                            <a href="#" class="btn btn-xs btn-danger like">Dislike</a>
                        </h6>
                    </div>
                    <div class="author pt-2">
                        <h6 class="text-muted">{{ $review->user_name }},  {{ date('d-m-Y', strtotime( $review->created_at )) }}</h6>
                    </div>
                </div>
                @empty
                <h6>There are not reviews for this product</h6>
                @endforelse
            </div>
        </div>
    </div>
</div>


@endsection

@section('scripts')
<script src="{{ asset('js/like.js') }}"></script>
<script type="text/javascript">
    var token = '{{ Session::token() }}';
    var urlLike = '{{ route('like') }}';
</script>
@endsection

最后,这是我的JS文件。

Like.js

$('.like').on('click', function(event) {
    event.preventDefault();
    reviewId = $(this).closest(".reviewid").attr("data-reviewid");;
    var isLike = event.target.previousElementSibling == null;

    $.ajax({
        method: 'POST',
        url: urlLike,
        data: {isLike: isLike, reviewId: reviewId, _token:token},
    })
});

2 个答案:

答案 0 :(得分:0)

在您的方法中使用try and catch,如下所示:

public function postLike(Request $request) 
{ 
     try{


        } catch(Exception $e){
            return $e->getMessage();
        } 

 }

这将返回可由ajax响应跟踪的错误。在storage/logs/laravel.log文件中,您可以获得跟踪错误。

是的,您可以同时使用$request['name']$request->name

希望这会有所帮助。

答案 1 :(得分:0)

我在它抛出以下错误的行中解决了它:

"message": "Undefined variable: user_id",
"exception": "ErrorException",
"file": "/Users/danhergir/Desktop/startup/app/Http/Controllers/ProductReviewController.php",
"line": 113,
"trace":

我刚改变了我的路线:

$like->user_id = $user_id;

到此:

$like->user_id = $user->id;