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

Raimondas's avatar

How to create event???

I want to create an event that will notify the user when someone placed higher bid on a product than the user!

How to write it ?? I have this function in my User.php that will checks if user is placed bid on a product:

    public function checked($auctionId)
    {
        return $this->bidCheck()->where('product_id', $auctionId)->count() > 0;
    }

Can someone write me that event ? Please!

0 likes
9 replies
ebukz's avatar

You first need to generate an Event class with artisan i.e. AuctionUserWasOutbidded with:

 php artisan make:event AuctionUserWasOutbidded

That will create the class for you in the app/Events as so


<?php

namespace App\Events;

use App\Bid;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;

class AuctionUserWasOutbidded extends Event
{
    use SerializesModels;

    public $bid;

    /**
     * Create a new event instance.
     *
     * @param  Bid  $bid
     * @return void
     */
    public function __construct($bid)
    {
        $this->bid = $bid;
    }
}

You then need a listener to handle this event. Generate one with:

php artisan make:listener EmailOutbiddedUser --event=AuctionUserWasOutbidded

Finally add it to the EventServiceProvider listen array:

'SLOCO\Events\AuctionUserWasOutbidded' => [
            'SLOCO\Listeners\EmailOutbiddedUser',
        ], 

Then anything that needs to done can be handled via the EmailOutbiddedUser listener handle() method. You can add a dd("test") to check it reaches there

To raise the event do:

event(new AuctionUserWasOutbidded())

Hope that helps

3 likes
Raimondas's avatar

@ebukz When I write this: event(new AuctionUserWasOutbidded) I get bash: syntax error near unexpected token `new'

ohffs's avatar

Urm, it's PHP code - not shell code...

Raimondas's avatar

Whatever. I'm not able create this kind of thing. thanks

EdwinL's avatar

I am worried that you are working on an aplication like this. Seeing this a lot lately, people picking up jobs above their league and then when asking help treating the people who try to help and teach them like shit.

3 likes
Raimondas's avatar

@EdwinL what ? No. I'm struggling too much with only this ONE function. I need the code that checks if user placed higher bid than the user who is already placed a bid. THAT'S IT.

Please or to participate in this conversation.