使用laravel 5中的ajax在非对象上调用成员函数save() [英] Call to a member function save() on a non-object using ajax in laravel 5
问题描述
我试图用ajax制作一个喜欢和不喜欢的按钮将它保存到数据库。
但是,它有时有效,有时它会在inspect的网络选项卡中给出错误响应,Call to非对象的成员函数save()。
这是我的LikeController.php:
I am trying to make a like and dislike button with ajax to save it to the database.
But, it works sometimes and sometimes it gives error response in the network tab of inspect, Call to a member function save() on a non-object.
this is my LikeController.php :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Depress;
use App\Http\Requests;
class LikeController extends Controller
{
public function postLike(Request $request)
{
$post_id = $request->input('postId');
//dd($post_id);
$post = Depress::find($post_id);
$post['like'] = $post['like'] + 1;
$post->save();
return null;
}
public function postDislike(Request $request)
{
$post_id = $request->input('postId');
$post = Depress::find($post_id);
$post['dislike'] = $post['dislike'] + 1;
$post->save();
return null;
}
}
问题似乎在
The problem seems to be in
$post->save();
但是,我不知道是什么问题。
任何人都请来救援,过去2天我一直被困在这里。
But, I don't know what is the problem.
Anyone please come to the rescue, I have been stuck here for the past 2 days.
<pre lang="PHP">
我的尝试: >
我试过dd($ post)并且似乎给出了正确的值。
What I have tried:
I have tried to dd($post) and it seems to give proper value.
推荐答案
request)
{
request) {
post_id =
post_id =
request->输入(' postId');
// dd(
request->input('postId'); //dd(
这篇关于使用laravel 5中的ajax在非对象上调用成员函数save()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!