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
- Call to a member function getRelationExistenceQuery() on array in Laravel
- Update last created record in Laravel
- How to check find method executed successfully in laravel
- Route [password.request] not defined
- How to insert dynamic value to additional column in pivot table in laravel
- Check if Relationship Method Exists in Laravel
- Seed database using SQL file in Laravel
- How to get specific columns using Laravel eloquent methods
- How to get last year records count with month wise in Laravel
- Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given
- File_put_contents(/var/www/html/w3code/storage/framework/sessions/CXwN3EXKxERD6jgy3rADcaAAbAx8FRKih2JK7UO9): Failed to open stream: Permission denied
- Undefined property: stdClass::$title
- Show old value while editing the form in Laravel
- Laravel migration add foreign key to existing table
- Credit card validation in laravel
- Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()
- Array to string conversion laravel Controller
- How to send ID to another page in Laravel
- How to create and run user seeder in laravel
- Get 30 days older records from table in laravel
- Remove public from url in laravel project
- Get laravel version
- How to customize pagination view in laravel
- How to get images from AWS s3 and display in Laravel blade
- Route not defined in Laravel