html - Send notifications on database php laravel mysql
I have a button that sends a notification. This is the request button. Only what I write to the toArray method is sent to the database in the data field. Now this is my 'message'. How can I change the data field outside the Notify.php file? That is, when the request button is pressed, a form should be opened, into which data is entered, which are then sent to the database instead of 'message'.First, tell me at least how I can fill in the data field, and I'll figure it out with the form myself. I am new to php laravel. I hope you can help me. web.php:
<?php
use App\User;
use App\Notifications\Notify;
use Illuminate\Support\Facades\Notification;
Route::name('user.')->group(function (){
Route::get('/private',function (){
return view('private');
});
Route::view('/private','private')->middleware('auth')->name('private');
Route::get('/login',function(){
if(Auth::check()){return redirect(route('user.private'));}
// ?????????�?????� ???� ?�???�???????�?�?�???? ?????�???�?????�?�?�?�??. ?�???�?? ???�?� ?�???�???????�?????�??, ?�?? ???�???????�???� ???� ???�???�?????�?? ???????�???�??
return view('login');
})->name('login');
Route::post('/login',[\App\Http\Controllers\LoginController::class,'login']);
Route::get('/logout',function(){
Auth::logout();
return redirect('/');
})->name('logout');
Route::get('/registration', function () {
if(Auth::check()){
return redirect(route('user.private'));
}
return view('registration');
})->name('registration');
Route::post('/registration',[\App\Http\Controllers\RegisterController::class,'save']);
Route::get('/private/request',function (){
$users = User::all();
Notification::send($users,new App\Notifications\Notify);
return redirect(route('user.private'));
});
Route::get('/private/markAsRead',function (){
auth()->user()->unreadNotifications->markAsRead();
return redirect()->back();
});
});
private.blade.php:
@extends('layouts.app')
@section('title-block')
???????�???�??
@endsection
@section('content')
<p>?????�?????? ???�?? ?�???�?�???�???�???�?????????�???�?� ?????�???�?????�?�?�?�?�??</p>
<a href="/logout"><button onlick="return false;">LogOut</button></a> <p>
<?php $id = Auth::id();
$user = DB::table('users')->find($id);
?>
{{$user->surname}}
{{$user->name}}
{{$user->fathers_name}}
{{$user->number}}
{{$user->studies}}
<p> <a href="/private/request"><button onlick="return false;">Request</button></a> <p>
<a href="#"></a>
<li class ='dropdown'>
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true">
<i class="fa fa-bell"></i>
<span class="badge bg-secondary">{{auth()->user()->unreadNotifications->count()}}</span>
</a>
<li ><a href="/private/markAsRead" style="color: green">Mark all as Read</a></li>
<ul >
@foreach(auth()->user()->readNotifications as $notification)
<li><a href="#"> {{$notification->data['data']}}</a>
</li>
@endforeach
@foreach(auth()->user()->unreadNotifications as $notification)
<li style="background-color: lightgray"><a href="#"> {{$notification->data['data']}}</a>
</li>
@endforeach
</ul>
</li>
@endsection
Notifications/Notify.php:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class Notify extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['database'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'data' => 'message'
];
}
}
This is screen from my database: Here, instead of message, there should be my data enter image description here
Answer
Solution:
you need to use attributes in notifiable object not just typing the attributes (message) like this:
$notifiable->message
Source