Answers for "laravel model casts"

PHP
1

laravel model casts

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'options' => 'array',
    ];
}
Posted by: Guest on August-24-2021
0

$this- attribute laravel

To define a mutator, define a setFooAttribute method on your model where Foo
  is the "studly" cased name of the column you wish to access. So, again,
lets define a mutator for the first_name attribute. This mutator will
be automatically called when we attempt to set the value of the first_name
attribute on the model:

class User extends Model
{
    public function setFirstNameAttribute($value)
    {
        $this->attributes['first_name'] = strtolower($value);
    }
}
Posted by: Guest on July-24-2020
1

casts in model laravel

Once the cast is defined, you may access
  the options attribute and 
  it will automatically be deserialized 
  from JSON into a PHP array. When you set 
  the value of the options attribute, the given 
  array will automatically be serialized back into 
  JSON for storage:


use App\Models\User;

$user = User::find(1);

$options = $user->options;

$options['key'] = 'value';

$user->options = $options;

$user->save();
Posted by: Guest on June-12-2021

Browse Popular Code Answers by Language