How to fill a column automatically while creating records in Laravel
How to fill a column automatically while creating records in Laravel
In this article, we will see how to solve "How to fill a column automatically while creating records in Laravel".
You can fill a column automatically while creating records in Laravel. Here we can use model observer to automatically fill the column data while you persist data to the database (e.g: slug).-
Fill a column (slug) automatically while you persist (insert) data to the table in Laravel
--PATH app\Models\Post.phpuse Illuminate\Support\Str; class Post extends Model { protected $fillable = [ 'title', 'slug', 'body' ]; protected static function boot() { parent::boot(); static::saving(function ($model) { $model->slug = Str::slug($model->title); }); } }
This code snippet will automatically fill the slug column with data while creating records in the posts table. You have to create the posts table with fields title, slug and body. Use code snippets in the Post model. You can make changes as per your requirements.
To create a records in posts table use the below code snippet.
$post = App\Models\Post::create([ 'title' => 'lorem ipsum title', 'body' => 'Lorem ipsum body' ]); dd($post);
Output:
#original: array:6 [▼
"title" => "lorem ipsum title"
"body" => "Lorem ipsum body"
"slug" => "lorem-ipsum-title"
"updated_at" => "2022-01-08 04:10:42"
"created_at" => "2022-01-08 04:10:42"
"id" => 24
]
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Pass variable from blade to controller Laravel
- How to check column value of a record is null or not in laravel
- Setup laravel project with docker
- How to get IP address in laravel
- Get today records in Laravel
- Ignore Records where a field has NULL value in Laravel
- Print query in laravel
- How to add background image to div using Tailwindcss, Vite in Laravel Environment
- Get Array of IDs from Eloquent Collection
- Generate unique username in Laravel
- How to add active class to menu item in laravel
- How to use bootstrap pagination in laravel 8
- SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.post_id' in 'where clause
- Update record after find method in lavavel
- How to make Copy or Duplicate table row in laravel
- Root composer.json requires php ^7.3 but your php version (8.0.0) does not satisfy that requirement
- How to delete record in Laravel with ajax
- How to insert ckeditor data into database in Laravel?
- Split an Eloquent Collection by half in Laravel
- Page loader in laravel
- Multiple Level eager loading in Laravel
- How to remove package from laravel
- How to remove P tag from CkEditor in Laravel?
- Laravel form request validation
- Add class to body in laravel view