Leon Segal
Leon Segal

Reputation: 751

laravel job/notification failing

I am trying to set up a contact form on my site whereby when someone clicks send, then a job is run and in that job, a notification is sent to all admin users. I keep getting this error in my failed jobs table though:

Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Contact]. in /var/www/html/leonsegal/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:412

I have been all over my code and I can't see what I have done wrong. Would anyone be able to help please?

Here is my controller:

<?php

namespace App\Http\Controllers;

use App\Contact;
use App\Jobs\SendContactJob;

class ContactController extends Controller
{
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        return view('contact');
    }

    public function store()
    {
        request()->validate([
            'name' => 'required|max:255',
            'email' => 'required|email|unique:contacts|max:255',
            'message' => 'required|max:2000',
        ]);

        $contact = Contact::create(
            request()->only([
                'name',
                'email',
                'message',
            ])
        );

        SendContactJob::dispatch($contact);

        return back()->with('success', 'Thank you, I will be in touch as soon as I can');
    }
}

my job:

<?php

namespace App\Jobs;

use App\Contact;
use App\Notifications\SendContactNotification;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Notification;

class SendContactJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $contact;

    /**
     * Create a new job instance.
     *
     * @param Contact $contact
     */
    public function __construct(Contact $contact)
    {
        $this->contact = $contact;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $users = User::all()
            ->where('admin', 1)
            ->where('approved', 1);

        Notification::send($users, new SendContactNotification($this->contact));
    }
}

my notification:

<?php

namespace App\Notifications;

use App\Contact;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;

class SendContactNotification extends Notification implements ShouldQueue
{
    use Queueable;

    protected $contact;

    /**
     * Create a new notification instance.
     *
     * @param $contact
     */
    public function __construct(Contact $contact)
    {
        $this->contact = $contact;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line($this->contact->name)
                    ->line($this->contact->email)
                    ->line($this->contact->message);
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

The weird thing is that when I run a die dump in the handle method of the job, it never fires, but the artisan queue worker says it was processed correctly but the subsequent notification is where it is failing. I am not sure why that handle method in the job wouldn't be firing.

I have set my .env file to database queue driver.

I thought it might be that I didn't import the contact model, but you can see I have.

Any help would be appreciated.

Upvotes: 3

Views: 4438

Answers (2)

Jiwon Lim
Jiwon Lim

Reputation: 11

Did you check on your model path? Coz for newer laravel the path should be use App\Models\Contact;

Upvotes: 1

John Halsey
John Halsey

Reputation: 2008

Could be that because both the job and the notification are queued, the contact could be getting 'lost in transit' so to speak. try making the job non queueable, and only queue the notification (or the other way around). Or scrap the job altogether and just send the notification from the controller.

Upvotes: 5

Related Questions