我对 php/laravel 完全陌生。
我已经定义了一个模型如下(我在 DB,AlarmActions 中有一个表)。在Doupdate方法中,我喜欢根据 ID 返回一个条目。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class AlarmActions extends LobModel
{
protected $table = 'AlarmActions';
protected $primaryKey = 'AlarmActionId';
public function scopeDoUpdate($alarmId)
{
return $alarmAction = \DB::table('AlarmActions')->where('AlarmActionId', $alarmId)->first();
}
}
我打电话给上面的班级;
$alarmAction = AlarmActions::doUpdate($id);
但我得到了;
Object of class Illuminate\Database\Eloquent\Builder could not be converted to string
t HandleExceptions->handleError(4096, 'Object of class Illuminate\\Database\\Eloquent\\Builder could not be converted to string', '/app/vendor/laravel/framework/src/Illuminate/Support/Str.php', 331, array('search' => '?', 'replace' => object(Builder), 'subject' => 'select top 1 * from [AlarmActions] where [AlarmActionId] = ?', 'position' => 59))
看起来我的ID没有正确传递?我在这里做错了什么?
实际上任何scope
方法中的第一个参数都是Query Builder
实例传递的Laravel
,你应该在它之后使用其他参数,
所以替换:
public function scopeDoUpdate($alarmId)
{
return $alarmAction = \DB::table('AlarmActions')->where('AlarmActionId', $alarmId)->first();
}
经过:
public function scopeDoUpdate($query, $alarmId)
{
return $query->where('AlarmActionId', $alarmId)->first();
}
你仍然可以调用它:
$alarmAction = AlarmActions::doUpdate($id);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句