从表中删除行有问题。
这个问题看起来与this question非常相似,但它并没有解决问题。
我的控制器功能如下:
public function favourites() {
//get relevant data for DB insert
$spot_id = Input::get('spot_id');
$user_id = Auth::user()->id;
//query DB if favourite exists
$query = DB::table('favourites')
->where('spot_id', '=', $spot_id)
->where('user_id', '=', $user_id);
$check_favourites = $query->first();
if (is_null($check_favourites)) {
//doesnt exist - create record
$favourite = new Favourite;
$favourite->spot_id = $spot_id;
$favourite->user_id = $user_id;
$favourite->save();
//return ajax true
return json_encode(true);
} else {
// exists - delete record
$check_favourites->delete();
//return ajax false
return json_encode(false);
}
}
我用的是拉弗4.2
提前谢谢
最佳答案
为了使用雄辩的delete()
方法,您需要使用Eloquent,而不是Query Builder。
而不是:
//query DB if favourite exists
$query = DB::table('favourites')
->where('spot_id', '=', $spot_id)
->where('user_id', '=', $user_id);
$check_favourites = $query->first();
应该是:
$check_favourites = Favourite::where('spot_id', '=', $spot_id)
->where('user_id', '=', $user_id)
->first();
注意:查询生成器上的所有可用方法也都可用
当查询雄辩的模型时。
关于php - 尝试在Laravel中删除行时出现“调用未定义方法stdClass::delete()”错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29698925/