Be part of JetBrains PHPverse 2026 on June 9 โ€“ a free online event bringing PHP devs worldwide together.

mehrabt's avatar

implement a Log System in Laravel

Hi guys!

I want to implement a logging system for my Laravel project

is this Method is clean and true? Do you have a better solution?

and here is my controller:

    public function store(Request $request)
    {
        $image = $request->image->store('products');
        // create the post
        Products::create([
          'title' => $request->title,
          'price' => $request->price,
          'image' =>$image,

        ]);
        $success_create_status="post created successfully!";
//create success log
        $data=[
            'status'=>$success_create_status
        ];
        
        DB::table('logs')->insert($data);


        return redirect(route('products.index'));
    }

and log migration:

    public function up()
    {
        Schema::create('logs', function (Blueprint $table) {
            $table->id();
            $table->string('status');
            $table->timestamps();
        });
    }
0 likes
10 replies
Sinnbeck's avatar

@mehrabt Be careful deleting your replies. I was just writing a guide for you, but when submitting your reply was gone, and so was my text :/

1 like
mehrabt's avatar

@Sinnbeck I was Deleted my question just because I didn't want to be disturbed

forgive me if you are bothered๐Ÿ™ let me ask my question Again

it was my question:

I'm really confused, Can you take or send an example that how I send a simple status[like id, name,create_date] to the database with the event, listener, and provider after creating a post

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@mehrabt No worries

Make a table like this

Schema::create('logs', function (Blueprint $table) {
    $table->id();
    $table->string('message');
    $table->timestamps();
});

and a model called Log

On the Post model add this

protected static function booted()
    {
        static::created(function ($post) {
           $userId  = auth()->id();
           $postId = $post->id;
            \App\Models\Log::create([
                  'message' => "User $userId created a new post: $postId",
             ]);
        });
    }
1 like
mehrabt's avatar

@Sinnbeck thank you ๐Ÿ™It worked.

if we have a lot of commands for events, Is this method optimal?

remember: I want to implement a logging system for a website

Sinnbeck's avatar

@mehrabt I assume you mean if you need to do the logging in alot of places?

For that I would probably wrap it in an action

protected static function booted()
    {
        static::created(function ($post) {
           app(LogCreatedAction::class)->execute($post);
        });
    }

And then in the LogCreatedAction class

public function execute($model)
{
           $userId  = auth()->id();
           $type = $reflect = (new \ReflectionClass($model))->getShortName();
           $id = $model->id;
             \App\Models\Log::create([
                  'message' => "User $userId created a new $type: $id",
             ]);
}

https://freek.dev/1371-refactoring-to-actions

1 like

Please or to participate in this conversation.