Home > database >  Database column won't update Laravel Eloquent
Database column won't update Laravel Eloquent

Time:09-29

my column "conversion_rate" won't update, my "iso" column updates without any problem. I tried changing the column's type in the model but nothing helps.

    $feed = simpleXML_load_file($url,LIBXML_NOCDATA);
        foreach ($feed->list->currency as $currency) {
            if (Currency::all()->contains('iso', $currency['iso_code'])) {
                Currency::where("iso", $currency['iso_code'])->get()->first()->update(['conversion_rate' => 2.00]);
            }
        }

edit: My database stucture

    Schema::create('currencies', function (Blueprint $table) {
        $table->id();
        $table->string('iso');
        $table->decimal('conversion_rate');
        $table->timestamps();
    });

CodePudding user response:

You should add your fields in model fillable property.

    class Currency extends Model
    {
        use HasFactory;
    
        /**
         * @var string
         */
        protected $table = 'currencies';
    
        /**
         * @var string[]
         */
        protected $fillable = ['iso', 'conversion_rate'];
     }
  • Related