0

I am trying to import excel file to db using maatiswebsite. For that I am using queue because it would take some time to import file. I want to show user a quick message that "your file is being imported. we will inform you once it is done." But not able to do it.

QUEUE_DRIVER = database

Here is what I did so far -
Controller -

public function import(Request $request) 
{
    Excel::filter('chunk')->load($request->file('import_file')
            ->getRealPath())->chunk(250, function($reader)
    {
        ImportDistributor::dispatch(new ImportDistributor($reader->toArray()));
    });

    dd('your file is being imported. we will inform you once it is done.');

}    

Job -

public function handle()
{
    if(isset($this->data) && !empty($this->data))
    {
        foreach($this->data as $data)
        {
            DB::table('user_details')->insert($data);
        }
    }
}    

Model -

<?php

namespace App\Model;
use Illuminate\Database\Eloquent\Model;

class UserDetailsModel extends Model
{
    protected $table = 'user_details';
}

I am not able to store data in user_details.

4
  • please share your model. Commented Oct 10, 2018 at 10:50
  • @Deepak show error Commented Oct 10, 2018 at 10:58
  • @LuckySaini I have updated question. Commented Oct 10, 2018 at 11:01
  • @JigneshJoisar Not getting any error, it's storing data to jobs table but not storing in user_details table. Commented Oct 10, 2018 at 11:02

2 Answers 2

1

You need to add $fillable property in your model and add all the fields which you want to save to your database

<?php

namespace App\Model;
use Illuminate\Database\Eloquent\Model;

class UserDetailsModel extends Model
{
    protected $table = 'user_details';
    protected $fillable = ["name", "etc", "etc1"];
}

And in Job, change code to this.

use App\UserDetailsModel;

public function handle()
{
    if(isset($this->data) && !empty($this->data))
    {
        foreach($this->data as $data)
        {
            UserDetailsModel::insert($data);
        }
    }
}

Try this. I think this will solve your problem.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer but nothing gets change.
if i change QUEUE_DRIVER to sync, it stores data in database table user_details
1

If the import works fine when you use sync drive, that means you need to run the queue worker:

php artisan queue:work

Comments

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.