Best Practices for Error Handling in Production Server Code (example code)
When developing applications for production servers, it's crucial to implement robust error handling mechanisms to ensure smooth operation and prevent unexpected failures.
Here's an example of how to handle errors effectively in production server code in Laravel:
Create a wallet column in users table using migration command
php artisan make:migration add_wallet_column_to_users_table --table=users
Add code to migration file database\migrations\2024_03_18_053755_add_wallet_column_to_users_table.php
and run php artisan migrate
command to run migration after copy/paste code in it.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->integer('wallet')->unsigned()->default(2);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('wallet');
});
}
};
Add below code to web.php file
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use App\Models\User;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
Route::get('/buy/{cookie}', function($cookie) {
$validator = Validator::make(['cookie' => $cookie], [
'cookie' => 'required|integer|min:1'
]);
if($validator->fails()){
return 'Invalid number of cookies!';
}
if (Auth::check()) {
$wallet = Auth::user()->wallet;
$purchaseAmount = $cookie * 1;
if ($wallet < $purchaseAmount) {
return 'Insufficient balance';
}
DB::beginTransaction();
try{
User::where(['id' => Auth::user()->id])
->update(['wallet' => ($wallet - $purchaseAmount)]);
DB::commit();
return 'Purchase successful!';
}catch(Exception $e){
DB::rollBack();
throw new UnprocessableEntityHttpException($e->getMessage());
}
} else {
return 'You must be logged in to make a purchase!';
}
});
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
- How to get date from created_at field in laravel
- Get current URL on visit URL in Laravel
- How to Run CRON Job on LIVE SERVER on Cpanel in Laravel Project
- How to display serial number in Laravel?
- How to add columns in existing table using migration in laravel
- How to use more than one query scope in Laravel
- How to disable timestamps in laravel
- The use statement with non-compound name 'Auth' has no effect
- How to get path from current URL in Laravel
- First and last item of the array using foreach iteration in laravel blade
- How to make Copy or Duplicate table row in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1055
- How to use bootstrap pagination in laravel 8
- Validation errors for multiple forms on same page Laravel
- How to pass two variables in HREF in laravel
- Extract only time from datetime in laravel
- How to get route name on visit URL in laravel
- Recursive function example code PHP Laravel
- Ajax GET request in laravel
- Symlink(): No such file or directory
- Call to undefined method Illuminate\Support\Facades\Request::all()
- How to get specific columns using Laravel eloquent methods
- Insert data with form validation using ajax in laravel
- Laravel URL validation not working
- How to automatically update the timestamp of parent model in Laravel