我不能在列上输入变量,我在控制器上有这个
public function save()
{
$book = new Book;
$ta = DB::select('SELECT MAX(ta) from book');
$book->ta=$ta;
$book->save();
}
我该如何解决这个问题?
答案 0 :(得分:0)
您必须将结果用作数组,您只需要第一个(也是唯一的)元素
$book = new Book;
$result = DB::select('SELECT MAX(ta) AS ta FROM book');
$book->ta = $result[0]->ta;
$book->save();
或者您可以再使用查询构建器并执行此操作
$book = new Book;
$result = DB::table('book')->selectRaw('MAX(ta) AS ta')->first();
$book->ta = $result->ta;
$book->save();