
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 decrypt laravel password
- Best code example for create order in Laravel for ecommerce
- How to upload image in laravel 8
- Composer\Exception\NoSslException
- How to get random string in Laravel
- Cannot end a section without first starting one
- FirstOrCreate() Not Inserting Model
- Laravel delete all rows older than 30 days
- Undefined property: stdClass::$title
- Redirect to previous page or url in laravel
- Class "App\Http\Controllers\Auth\Verified" not found
- How to run a specific seeder class in laravel
- Create model with migration and seeder
- Laravel onclick function not working
- Validation errors for multiple forms on same page Laravel
- Laravel hasmany select not working
- How to get session in blade Laravel ?
- Validation for multiple forms on same page in laravel
- SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel8.projects' doesn't exist
- Laravel change date format
- On delete set foreign id column value null using migration in laravel 8
- Conditional validation in laravel
- Laravel form request validation
- Laravel route parameter
- How to get column names from table in Laravel