
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
- SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.post_id' in 'where clause
- Declaration of App\Models\Post::sluggable() must be compatible with Cviebrock\EloquentSluggable\Sluggable
- Call to undefined method App\Models\User::follow()
- Get content from web URL in laravel
- Wheredate in laravel not working
- Get the post details if it has at least one comment in comments table
- Array to string conversion laravel Controller
- How to add class to tr in table using foreach in laravel
- Insert data with form validation using ajax in laravel
- How to validate form input data in laravel
- Pass value from controller to model in laravel
- Global scope in Laravel with example
- Retain selected value of select box in Laravel
- Page loader in laravel
- Add a subselect based on relationship using withAggregate method
- Display data in table using foreach in Laravel
- Laravel get single row by id
- The openssl extension is required for SSL/TLS protection but is not available
- Datetime field in Laravel migration
- Class 'App\Providers\Auth' not found
- How to create belongstomany relation using custom name on custom pivot table
- How to create and run user seeder in laravel
- Validation for multiple forms on same page in laravel
- How to call Laravel route in jQuery
- Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails