In Laravel 9, query the value of a single row and a column
1. When there are multiple rows of data in the table, the implementation is as follows. Error: call to a member function value() on null. as shown in Figure 1
$return = Order::query()
->select('id')
->where('transaction_no', '=', '1')
->first()
->value('id');
var_dump($return);
exit;
2. The adjustment is realized as follows. When the record does not exist, the query result is NULL. When the record exists, the query result is int(6), which is the single-column value of the first record. in line with expectations.
$return = Order::query()
->select('id')
->where('transaction_no', '=', '1')
->limit(1)
->value('id');
var_dump($return);
exit;
3. Check the execution sql, which is in line with expectations. as shown in Figure 2
select `id` from `orders` where `transaction_no` = '1' limit 1

