0

Is it possible to display a ready formed SQL after operation:

 $r = \App\Visitor::create($item);

Where $item is array data.

I wonder why this operation returns in $r inserted model, but there is no changes in db table.

So, thanks for answers I tried yours solutions:

This is query:

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");

This is screen of binding:

enter image description here

If execute query above in database it works. But Laravel does not insert data.

Model is:

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);
    }
}

Code is:

foreach ($items as $item) {
    \App\Visitor::create($item);
}
3
  • please be specific. do you want the sql query that preformed to insert data? Commented Mar 9, 2019 at 19:21
  • Sure, user below have answered and have got my issue Commented Mar 9, 2019 at 19:23
  • yes that's correct. looks like I replied too late to your question :D Commented Mar 9, 2019 at 19:27

2 Answers 2

2

I believe, you can do something like this:

$r = \App\Visitor::create($item)->toSql();
dd($r);
Sign up to request clarification or add additional context in comments.

2 Comments

Very strange but it gives me SELECT query instead INSERT: "select * from visitors` where status = ?"`
You can either try wigh the Davit's answer, or running with debugbar. github.com/barryvdh/laravel-debugbar
1

You can use this

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);

6 Comments

What is a problem?? Show $item value
See my question please
Are you use transactions??
What does it mean? I use ::create in loop
See laravel.com/docs/5.7/database#database-transactions. Also can you show your whole code?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.