1. Add a new folder inside the app folder on the Laravel project folder that we created. For example, I created a folder named CustomClass
2. Add a new file in the CustomClass folder with the name record_log.php
3. Fill in the following class code in the record_log.php file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php namespace App\CustomClass; use Illuminate\Http\Request; use Illuminate\Validation\ValidationException; use Illuminate\Support\Facades\Hash; use App\t_log; use App\User; class record_log { public static function save_log($token, $ket_log, $post_log) { try{ $getuser = User::where('api_token', $token)->first(); if(!$getuser){ $id = 0; }else{ $id = $getuser->id_user; } try { $data = t_log::create([ 'id_user'=> $id, 'ket_log'=> $ket_log, 'post_log'=> $post_log, ]); } catch (\Illuminate\Database\QueryException $ex) { //dd ($ex->getMessage()); } } catch(\Illuminate\Database\QueryException $ex){ //dd ($ex->getMessage()); } } } |
A brief description
The above class is used to store user logs in a database every time a user performs an activity on an application system.
Don’t forget to record the namespace
1 |
namespace App\CustomClass; |
4. To access the class as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
use Illuminate\Http\Request; use Illuminate\Validation\ValidationException; use Illuminate\Support\Facades\Hash; use App\User; use App\CustomClass\record_log; class loginController extends Controller { public function login(Request $request) { ....................... record_log::save_log($api_token,'access login',json_encode($request->all())); ....................... } } |
Leave a reply