如何显示由::create Laravel形成的SQL查询?
是否可以在操作后显示已准备好的SQL:如何显示由::create Laravel形成的SQL查询?,laravel,laravel-5,Laravel,Laravel 5,是否可以在操作后显示已准备好的SQL: $r = \App\Visitor::create($item); 其中,$item是数组数据 我想知道为什么这个操作在插入的模型中返回$r,但在db表中并没有变化 因此,感谢您的回答我尝试了您的解决方案: 这是查询: insert into `visitors` (`lastname`, `firstname`, `middlename`, `birthday`, `company`, `document_number`, `pincode`, `c
$r = \App\Visitor::create($item);
其中,$item
是数组数据
我想知道为什么这个操作在插入的模型中返回$r
,但在db表中并没有变化
因此,感谢您的回答我尝试了您的解决方案:
这是查询:
insert into `visitors` (`lastname`, `firstname`, `middlename`, `birthday`, `company`, `document_number`, `pincode`, `code`, `idEvent`) values ("Huse", "Huseynkhanov", "Akif", "1981-04-09", "XXX", 16428285, "QT0FE12", 19283746564923, "17");
class Visitor extends Model
{
public $timestamps = false;
public $table = 'visitors';
protected $fillable = [
"lastname",
"firstname",
"middlename",
"birthday",
"company",
"document_number",
"pincode",
"status",
"code",
"idEvent"
];
protected $primaryKey = 'idVisitor';
protected static function boot()
{
parent::boot();
static::addGlobalScope(new StatusScope);
}
}
foreach ($items as $item) {
\App\Visitor::create($item);
}
这是绑定屏幕:
insert into `visitors` (`lastname`, `firstname`, `middlename`, `birthday`, `company`, `document_number`, `pincode`, `code`, `idEvent`) values ("Huse", "Huseynkhanov", "Akif", "1981-04-09", "XXX", 16428285, "QT0FE12", 19283746564923, "17");
class Visitor extends Model
{
public $timestamps = false;
public $table = 'visitors';
protected $fillable = [
"lastname",
"firstname",
"middlename",
"birthday",
"company",
"document_number",
"pincode",
"status",
"code",
"idEvent"
];
protected $primaryKey = 'idVisitor';
protected static function boot()
{
parent::boot();
static::addGlobalScope(new StatusScope);
}
}
foreach ($items as $item) {
\App\Visitor::create($item);
}
若在数据库中执行上面的查询,它就会工作。但拉威尔不插入数据
型号为:
insert into `visitors` (`lastname`, `firstname`, `middlename`, `birthday`, `company`, `document_number`, `pincode`, `code`, `idEvent`) values ("Huse", "Huseynkhanov", "Akif", "1981-04-09", "XXX", 16428285, "QT0FE12", 19283746564923, "17");
class Visitor extends Model
{
public $timestamps = false;
public $table = 'visitors';
protected $fillable = [
"lastname",
"firstname",
"middlename",
"birthday",
"company",
"document_number",
"pincode",
"status",
"code",
"idEvent"
];
protected $primaryKey = 'idVisitor';
protected static function boot()
{
parent::boot();
static::addGlobalScope(new StatusScope);
}
}
foreach ($items as $item) {
\App\Visitor::create($item);
}
代码为:
insert into `visitors` (`lastname`, `firstname`, `middlename`, `birthday`, `company`, `document_number`, `pincode`, `code`, `idEvent`) values ("Huse", "Huseynkhanov", "Akif", "1981-04-09", "XXX", 16428285, "QT0FE12", 19283746564923, "17");
class Visitor extends Model
{
public $timestamps = false;
public $table = 'visitors';
protected $fillable = [
"lastname",
"firstname",
"middlename",
"birthday",
"company",
"document_number",
"pincode",
"status",
"code",
"idEvent"
];
protected $primaryKey = 'idVisitor';
protected static function boot()
{
parent::boot();
static::addGlobalScope(new StatusScope);
}
}
foreach ($items as $item) {
\App\Visitor::create($item);
}
我相信,你可以这样做:
$r = \App\Visitor::create($item)->toSql();
dd($r);
你可以用这个
DB::enableQueryLog();
\App\Visitor::create(['key' => 'value', ....]);
$data = DB::getQueryLog();
$query = str_replace(array_fill(0, count($data[0]['bindings']), '?'), $data[0]['bindings'], $data[0]['query']);
dd($query);
请具体说明。您想让执行的sql查询插入数据吗?当然,下面的用户已经回答了,并且得到了正确的答案。看起来我对你的问题回答得太晚了:DVery奇怪,但它给了我选择查询,而不是插入:
“SELECT*from
visitors`wherestatus
=?”`你可以尝试使用吊柱的答案,也可以使用debugbar运行。什么是问题??显示$item
值请查看我的问题是否使用事务??这是什么意思?我使用loopSee中的::create
。你能展示你的全部代码吗?