Thursday, March 30, 2017

Alert users according to their saved search terms via email

I want to alert users based on their saved search term. So, I have alerts table where users id and terms are stored. For example:

ID: 1 | uid: 27 | emailSentAt: null | search: Adidas

ID: 2 | uid: 27 | emailSentAt: null | search: Samsung

ID: 3 | uid: 34 | emailSentAt: null | search: Pro Evolution Soccer 2017

When user searches something it is read from campaigns table. For the simplicity, lets say it has id and search (VARCHAR) field.

What I want is to alert user (according to their stored search term) every time when new campaign is added related to that term. It doesn't have to be automatically. I can do it manually, and I've tried something like this... This is a function from Mailable class:

    public function build()
        {
            echo "Sending email alert...\n";
            $user = DB::table('alerts')->select('*')
            ->join('users', 'alerts.uid', '=', 'users.id')->whereNotNull('search')->get();

            return $this->view('emails.newSearchAlert', compact('user'));
        }

And from Command class:

    public function handle()
    {
        $when = Carbon::now();
        $user = DB::table('alerts')->select('search', 'email')
        ->join('users', 'alerts.uid', '=', 'users.id')->whereNotNull('search')->get();
        foreach ($user as $u) {
            Mail::to($u->email)->later($when, new NewAlertsEmail);
        }
        echo "All email alerts are sent.";
    }

And when I run the command php artisan email:users, it sends emails with all search terms to all users. My question is: How can I manage to get query result to display in a view something like this:

<h1>New campaigns are available for your search terms: : </h1> <img src="http:/mydomain.com/wp-content/uploads/2015/10/.png">

Btw. I'm using Laravel 5.3.



via harunB10

Advertisement