0

I am trying to export/import Excel files from/to the database. For that, I have viewed few tutorials. All show the same way for it. Here are the links.

And some other.

Exporting excel from the database is working fine. I am getting errors only while importing.

In both tutorials, they showed same way of importing code as shown below

public function model(array $row) {
    return new product([
        'vendor_id'    => $row[0],
        'product_name'    => $row[1],
        'product_price'    => $row[2],
        'product_model'    => $row[3],
        'created_at'    => $row[4],
        'updated_at'    => $row[5],
        ]);  
}

It is giving me this error while I am trying to import excel.

Add [vendor_id] to fillable property to allow mass assignment on [App\Models\product].

I try to search about fillable and found some solutions. I tried to apply on it but the error isn't getting resolved. Any idea where I went wrong?

UPDATE

Product Model file code

<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class product extends Model {
   use HasFactory;
}
8
  • is vendor_id added to fillable property in model ? Commented Jul 26, 2021 at 5:47
  • @JohnLobo I am new in laravel. I am not fully clear, what actually fillable mean. How do I check if it is fillable? I am able to insert/update data in product model in which vendor_id column is present. Commented Jul 26, 2021 at 5:50
  • can you share product model Commented Jul 26, 2021 at 5:50
  • @JohnLobo You mean full code of product model in app->models->product.php? Commented Jul 26, 2021 at 5:52
  • better add so some one can help you to solve or try adding protected $guarded=['id'] in product model Commented Jul 26, 2021 at 5:53

1 Answer 1

1

You will need to specify either a fillable or guarded property on your model class. These properties are required because all Eloquent models are protected against mass assignment vulnerabilities by default.

A mass assignment vulnerability occurs when a user passes an unexpected HTTP request field and that field changes a column in your database that you did not expect. For example, a malicious user might send an is_admin parameter through an HTTP request, which is then passed to your model's create method, allowing the user to escalate themselves to an administrator.

If you are not specifying $guarded property then all fields need to be mentioned in $fillable property.

protected $fillable = ['vendor_id',
                       'product_name',
                       'product_price',
                       'product_model',
                      ];

or

protected $guarded=['id']

Ref:https://laravel.com/docs/8.x/eloquent#mass-assignment

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.