Php Laravel正在尝试删除上次添加的MySQL行

Php Laravel正在尝试删除上次添加的MySQL行,php,mysql,laravel,Php,Mysql,Laravel,我试图删除MySQL表中添加的最后一行,该行具有特定的用户ID。当我点击我的按钮时,我当前遇到以下错误 致命错误:调用整数上的成员函数Where() 这是我在刀片文件中的代码,按钮就在这里 <div class="element1"> {{ HTML::image('progress2/Icons/Meetings_Icon.png', 'alt', array('width' =>150)) }} <div class="text1"> <for

我试图删除MySQL表中添加的最后一行,该行具有特定的用户ID。当我点击我的按钮时,我当前遇到以下错误

致命错误:调用整数上的成员函数Where()

这是我在刀片文件中的代码,按钮就在这里

<div class="element1">
  {{ HTML::image('progress2/Icons/Meetings_Icon.png', 'alt', array('width' =>150)) }}
   <div class="text1">
 <form action="{{ route("progress") }}" method="post">
    <button class="styleHover styleButton"> + </button> </form>
    &nbsp; &nbsp; 5 &nbsp; &nbsp;
<form action="{{route("progressDeincrement") }}" method="post">
 <button class="styleHover styleButton"> - </button>
  </form>
 </div>
这是我控制器中的代码

public function delete() 
{
   $idForDelete = DB :: table("users_has_activities") -> max("pointsID") -> 
  Where("usersID", "=", "12");
  DB:: table("users_has_activities") -> Where("pointsID", "=",  $idForDelete) ->  
delete();
}
+按钮可以向MySQL表中添加一行,但是,删除一行对我来说是一个挑战

我猜
max(“pointsID”)
返回一个整数,您试图对整数而不是模型或集合使用
Where
子句

也许你想用这个:

$idForDelete = DB::table('users_has_activities')->where('usersID', '12')->max('pointsID');

您可以将其简化为单个数据库查询:

DB::table("users_has_activities")
    ->where("usersID", "=", "12")
    ->orderBy("pointsID", "DESC")
    ->take(1)
    ->delete();
这将为您提供如下SQL查询:

DELETE FROM users_has_activities
WHERE usersID = '12'
ORDER BY `pointsID` DESC
LIMIT 1;

结合使用
Order By column DESC
LIMIT 1
可确保获得最高的可用ID,因此可以模仿
MAX()

的行为,尝试使用
first()
降序

$idForDelete = DB :: table("users_has_activities") ->where("usersID", "=", 12)->orderBy('pointsID', 'DESC')->first();

文档说明必须在最晚的位置调用
max()
函数,因为它运行查询

请看聚合中的示例

用例是。。。表->过滤结果->获取我想要的内容,例如获取一个、获取全部、删除、更新等


尝试在呼叫时更改位置。。如。。。table->where->max(),因为max可能与delete等类似。。然后运行查询,因此必须至少两个W感谢所有回复@kirkbeard ur建议对我有效如果你想得到最后一行,但他想按id删除最后一条记录,那么限制是件好事,因此最大值将非常好,因为他只需要得到id,而不需要整行数据。ofc你是对的,但问题是“我的脚本中出现了错误”,而不是“如何优化我的查询或源”。这个答案告诉我“使用此复制和粘贴并将起作用”,但没有让提问者理解问题所在。因此我觉得下一个问题可能是“我的查询中出现了错误”IMHO.=)
$idForDelete = DB :: table("users_has_activities") ->where("usersID", "=", 12)->orderBy('pointsID', 'DESC')->first();