0
0
Laravelframework~20 mins

Creating notifications in Laravel - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Notification Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Laravel notification code?
Consider this Laravel notification class that sends a mail notification. What will be the subject of the email sent?
Laravel
namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class WelcomeUser extends Notification
{
    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('Welcome to Our App')
            ->line('Thank you for joining us!');
    }
}
AThe email subject will be 'Thank you for joining us!'.
BThe email subject will be 'Welcome to Our App'.
CThe email will have no subject.
DThe email subject will be the user's name.
Attempts:
2 left
💡 Hint
Look at the subject() method inside the toMail() function.
state_output
intermediate
2:00remaining
What is the value of $channels after calling via()?
Given this notification class, what will be the value of $channels when calling via($user)?
Laravel
namespace App\Notifications;

use Illuminate\Notifications\Notification;

class AlertUser extends Notification
{
    public function via($notifiable)
    {
        if ($notifiable->prefers_sms) {
            return ['nexmo'];
        }
        return ['mail', 'database'];
    }
}

$user = (object) ['prefers_sms' => false];
$notification = new AlertUser();
$channels = $notification->via($user);
Anull
B['nexmo']
C[] (empty array)
D['mail', 'database']
Attempts:
2 left
💡 Hint
Check the condition inside via() and the value of prefers_sms.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in this Laravel notification?
Identify which notification class code snippet will cause a syntax error.
Apublic function toMail($notifiable) { return new MailMessage->line('Hello'); }
Bpublic function toMail($notifiable) { return (new MailMessage())->line('Hello'); }
Cpublic function toMail($notifiable) { return (new MailMessage)->line('Hello'); }
Dpublic function toMail($notifiable) { return (new MailMessage)->subject('Hi')->line('Hello'); }
Attempts:
2 left
💡 Hint
Check how the new keyword is used with method calls.
🔧 Debug
advanced
2:00remaining
Why does this notification not send an email?
This notification class is supposed to send an email but does not. What is the cause?
Laravel
namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;

class NotifyUser extends Notification
{
    public function via($notifiable)
    {
        return ['database'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
            ->subject('Alert')
            ->line('You have a new alert.');
    }
}
AThe via() method does not include 'mail' channel.
BThe toMail() method is missing a return statement.
CThe MailMessage class is not imported.
DThe notification class does not extend Notification.
Attempts:
2 left
💡 Hint
Check the channels returned by via() method.
🧠 Conceptual
expert
3:00remaining
Which option correctly describes Laravel notification broadcasting?
In Laravel, when using notification broadcasting, which statement is true?
ABroadcast notifications require the notifiable model to implement the ShouldBroadcast interface.
BBroadcast notifications are sent automatically without defining a toBroadcast method.
CBroadcast notifications require the notification class to implement ShouldBroadcast and define toBroadcast method.
DBroadcast notifications can only be sent via mail channel.
Attempts:
2 left
💡 Hint
Think about the interface and method needed for broadcasting notifications.