第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel Notification如何設(shè)置自定義列值

Laravel Notification如何設(shè)置自定義列值

PHP
動漫人物 2021-05-14 17:13:41
我使用該php artisan notification:table命令user_id在通知遷移中創(chuàng)建了一個自定義列。如何設(shè)置自定義列的值user_id?Schema::create('notifications', function (Blueprint $table) {    $table->uuid('id')->primary();    $table->unsignedBigInteger('user_id');    $table->string('type');    $table->morphs('notifiable', 50);    $table->text('data');    $table->timestamp('read_at')->nullable();    $table->timestamps();    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate("cascade");});在此功能中,我發(fā)送了通知,但引發(fā)了錯誤。user_id列沒有默認(rèn)值public function saveUser($formdata){    $password = $formdata['password'];    $securePassword = Hash::make($password);    $user = User::create(array_merge($formdata, ['password' => $securePassword]));    $admin = User::where('type', 'admin')->first();    $letter = collect([        'title' => $user->name.' New User Registered in your Portal',    ]);    Notification::send($admin, new DatabaseNotification($letter));    return $user;} 請幫助我弄清楚如何設(shè)置該user_id值。
查看完整描述

1 回答

?
慕桂英4014372

TA貢獻(xiàn)1871條經(jīng)驗 獲得超13個贊

您應(yīng)該使用toDatabase或toArray方法來保存任何其他數(shù)據(jù)。另外,您無需更改通知遷移。您應(yīng)該$letter在內(nèi)部構(gòu)造變量DatabaseNotification,例如:


public function saveUser($formdata)

{

    $password = $formdata['password'];

    $securePassword = Hash::make($password);


    $user = User::create(array_merge($formdata, ['password' => $securePassword]));

    $admin = User::where('type', 'admin')->first();

    // $letter = collect([

    //     'title' => $user->name.' New User Registered in your Portal',

    // ]);


    // This syntax or below.

    // Notification::send($admin, new DatabaseNotification($user, $message));


    // Make sure you added `use Notifiable` trait in your `User` model

    $admin->notify(new DatabaseNotification($user, $message));


    return $user;

然后在您的toArray或toDatabase方法中:


    protected $message;

    protected $user;


    /**

     * Create a new notification instance.

     *

     * @return void

     */

    public function __construct($message, User $user)

    {

        $this->message = $message;

        $this->user = $user;

    }


    /**

     * Get the array representation of the notification.

     *

     * @param  mixed  $notifiable

     * @return array

     */

    public function toArray($notifiable)

    {

        return [

            'user_id' => $this->user->id,

            'message' => $this->message

        ];

    }

最終toArray輸出序列化到表的data列中notifications。


查看完整回答
反對 回復(fù) 2021-05-28
  • 1 回答
  • 0 關(guān)注
  • 251 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號