Php mark as read

Mark as read a specific notification in Laravel 5.3 Notifications

This one we want to pass the parameter of notification ID Enter fullscreen mode Exit fullscreen mode How to update status Unread Notification Enter fullscreen mode Exit fullscreen mode How to update status All Read Notification Enter fullscreen mode Exit fullscreen mode How to update status All Unread Notification Enter fullscreen mode Exit fullscreen mode That way to create notification management. We want to object for user model for like that $user Enter fullscreen mode Exit fullscreen mode Get Unread Notifications Enter fullscreen mode Exit fullscreen mode Get Read Notifications Enter fullscreen mode Exit fullscreen mode How to update status Read Notification

Mark as read a specific notification in Laravel 5.3 Notifications

I know that there are many ways to mark as read all notifications of an User in L5.3 like this:

$user = App\User::find(1); foreach ($user->unreadNotifications as $notification) < $notification->markAsRead(); > 
$user->unreadNotifications->markAsRead(); 

But suppose I want to mark as read a specific notification with a given ID.

In fact, I have made a list of unread user notifications like this:

As you can see, each a tag has a data-notif-id attribute contain notif ID.

Now I want to send this ID to a script via Ajax (on click event) and mark as read that notification only. for that I wrote this:

$('a[data-notif-id]').click(function () < var notif_id = $(this).data('notifId'); var targetHref = $(this).data('href'); $.post('/NotifMarkAsRead', , function (data) < data.success ? (window.location.href = targetHref) : false; >, 'json'); return false; >); 

NotifMarkAsRead refers to bellow Controller :

class NotificationController extends Controller < public function MarkAsRead (Request $request) < //What Do I do Here. >> 

How can I do that while there is not any Notification Model?

Читайте также:  Javascript div font color

According to this Answer on Github, solution is :

Illuminate\Notifications\DatabaseNotification is where the Model for the notifications exists, you can use it to grab a notification by ID and delete it. Also if you don’t want to use the model you can use a normal DB query.

$id = auth()->user()->unreadNotifications[0]->id; auth()->user()->unreadNotifications->where('id', $id)->markAsRead(); 
$notification = auth()->user()->notifications()->find($notificationid); if($notification) < $notification->markAsRead(); > 

You must first make sure that the notification exists first so that you can make adjustments to it and I do not recommend using the unreadNotifications object because in the event that there are no unRead Notifications an error will occur because of that

$notification_id = "03fac369-2f41-43d0-bccb-e364aa645f8a"; $Notification = Auth::user()->Notifications->find($notification_id); if($Notification)< $Notification->markAsRead(); > 

or you can directly edit without any check

DB::table('notifications')->where('id',$notification_id)->update(['read_at'=>Carbon::now()]) 

Php — Mark as read a specific notification in Laravel 5.3, While this code may resolve the OP’s issue, it is best to include an explanation as to how your code addresses the OP’s issue. In this way, future visitors can learn …

Laravel 8 with notification management

We talking about how to create the Laravel 8 with notification management. We early created multiple things of Laravel. Notification is one of the valuable parts of the web application. why for I said that one? Sometimes we are allow to access users for payment or any financial thing. We want to record everything for that. We will create transactions or something. But notification is very readable for than notification. Let’s see how to create that one.

I used the early created project for this one. Click Here

Go to the project path and open the terminal.

First of all we want to create notification table for that part. After create that table we will migrate again our database.

php artisan notifications:table php artisan migrate 

After that type the code for create notification. You can any name for that notification.

php artisan make:notification PaymentsSave 

For this I used the User model therefore we want to add the use Notifiable for our user model like that way.

Awesome now we looking for how to use that notification use our system.

$data['message']="The Payment Successfully !"; $user->notify(new PaymentsSave($data)); 

That way we can use the notification with parameters. Let’s see how to fix PaymentsSave class. Go to the PaymentsSave class you can see multiple function on it. Go to the via function we can config that use sent class use for sent email or save data in database. That time we are use save data in database. Therefore via like this.

public function via($notifiable)

we want to edit the __construct function also that way.

public function __construct($notification) < $this->notification = $notification; > 

After we can edit the toDatabase function like that

public function toDatabase($notifiable) < return [ 'message' =>$this->notification['message'] ?? '']; > 

Remember that I will sent the data with message that will save the our database as JSON array in notification table data column.

Now, how to show the notifications?

Let’s see this. We want to object for user model for like that $user

Get Unread Notifications
Get Read Notifications
How to update status Read Notification

This one we want to pass the parameter of notification ID

$user->notifications->where('id', $id)->markAsRead(); 
How to update status Unread Notification
$user->notifications->where('id', $id)->markAsUnread(); 
How to update status All Read Notification
$user->notifications->markAsRead(); 
How to update status All Unread Notification
$user->notifications->markAsUnread(); 

That way to create notification management.Let’s meet again for the brand new tutorial.

How to use the notification in laravel Code Example, All Languages >> PHP >> Yii >> how to use the notification in laravel “how to use the notification in laravel” Code Answer’s. laravel notification render . php by …

Laravel + Ratchet: Pushing notifications to specific clients

I am new to websockets and I want to implement such service in my Laravel application. I have already read several posts/pages about this topic, but none explains what I need to do. All of them show how to create an «Echo» websocket server, where the server only responds to messages received from clients, which is not my case.

As a starting base I used the code provided at:

Where the WebSocket server is ran from the command line or another console. The server has its own class to define it and imports the WebSocketController class (MessageComponentInterface), which contains the classic WebSocket Server events (onOpen, onMessage, onClose, onError).

All that works fine as is but, how can I «tell» the Websocket server to send a message to a specific connection (client) from another class, which also belong to another namespace?. This is the case of a notification or event, where new web content must be sent to that specific client. There are no subscriptions nor publications on the way from the client side.

As @Alias asked in his post Ratchet PHP — Push messaging service I obviously cannot create a new instance of the Websocket server or its events management class, so what would be the best approach to send content or messages to the client?

As you all can see, the communication is only in one way: from the WebSocket Server to the client(s) and not the opposite. I already have a notification class and a listener class prepared for this but, I still don’t know how to address the communication with clients from the handle() method:

namespace App\Listeners; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Events\NotificationSent; use Illuminate\Queue\InteractsWithQueue; class LogNotification < /** * Create the event listener. * * @return void */ public function __construct() < // >/** * Handle the event. * * @param NotificationSent $event * @return void */ public function handle(NotificationSent $event) < // Can content or message be sent to the client from here? how? >> 

Ok, after studying and researching a lot, I could post an answer to a similar question in:

How to send a message to specific websocket clients with symfony ratchet?

This is the solution I found, according to what I wrote in my second comment in this thread.

I installed cboden/Ratchet package for the websocket server using composer. I needed to send notifications to a user/group of users, or update the UI, when an event is fired in the backend.

1) Installed amphp/websocket-client package using composer.

2) Created a separated class in order to instantiate an object that could get connected to the websocket server, send the desired message and disconnect:

namespace App; use Amp\Websocket\Client; class wsClient < public function __construct() < // >// Creates a temporary connection to the WebSocket Server // The parameter $to is the user name the server should reply to. public function connect($msg) < global $x; $x = $msg; \Amp\Loop::run( function() < global $x; $connection = yield Client\connect('ws://ssa:8090'); yield $connection->send(json_encode($x)); yield $connection->close(); \Amp\Loop::stop(); > ); > > 

3) The onMessage() event, in the handler class of the webSocket server, looks like this:

 /** * @method onMessage * @param ConnectionInterface $conn * @param string $msg */ public function onMessage(ConnectionInterface $from, $msg) < $data = json_decode($msg); // The following line is for debugging purposes only echo " Incoming message: " . $msg . PHP_EOL; if (isset($data->username)) < // Register the name of the just connected user. if ($data->username != '') < $this->names[$from->resourceId] = $data->username; > > else < if (isset($data->to)) < // The "to" field contains the name of the users the message should be sent to. if (str_contains($data->to, ',')) < // It is a comma separated list of names. $arrayUsers = explode(",", $data->to); foreach($arrayUsers as $name) < $key = array_search($name, $this->names); if ($key !== false) < $this->clients[$key]->send($data->message); > > > else < // Find a single user name in the $names array to get the key. $key = array_search($data->to, $this->names); if ($key !== false) < $this->clients[$key]->send($data->message); > else < echo " User: " . $data->to . " not found"; > > > > echo " Connected users:\n"; foreach($this->names as $key => $data) < echo " " . $key . '->' . $data . PHP_EOL; > > 

As you can see the user(s), you want the websocket server to send the message to, are specified as a string ($data->to) in the $msg parameter along with the message itself ($data->message). Those two things are JSON encoded so that the parameter $msg can be treated as an object.

4) On the client side (javascript in a layout blade file) I send the user name to the websocket server when the client connects:

var currentUser = "name >>"; socket = new WebSocket("ws://ssa:8090"); socket.onopen = function(e) < console.log(currentUser + " has connected to websocket server"); socket.send(JSON.stringify(< username: currentUser >)); >; socket.onmessage = function(event) < console.log('Data received from server: ' + event.data); >; 

So, the user name and its connection number are saved in the websocket server.

5) The onOpen() method in the handler class looks like this:

 public function onOpen(ConnectionInterface $conn) < // Store the new connection to send messages to later $this->clients[$conn->resourceId] = $conn; echo " \n"; echo " New connection (resourceId>) " . date('Y/m/d h:i:sa') . "\n"; > 

Every time a client gets connected to the websocket server, its connection number or resourceId is stored in a array. So that, the user names are stored in an array ($names), and the keys are stored in another array ($clients).

6) Finally, I can create an instance of the PHP Websocket Client (wsclient) anywhere in my project to send whatever data to any user(s):

public function handle(NotificationSent $event) < $clientSocket = new wsClient(); $clientSocket->connect(array('to'=>'Anatoly,Joachim,Caralampio', 'message'=>$event->notification->data)); > 

In this case I am using the handle() method of a notification event Listener.

Alright, this is for anyone wondering how to send messages from the PHP websocket server (AKA echo server) to one specific client or to a group of clients.

Laravel send notification Code Example, how can we send attached file with notification in gmail in laravel 8; send multiple db notification same function in laravel; Laravel: customize or extend …

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to mark mail as read #272

How to mark mail as read #272

Comments

Hi, I’ve been on this for a day.
It seems that using the example provided, my emails aren’t marked as read, even when I switch the flag to ‘true’. Also, I’ve tried to use the ‘markAsread’ and it seems not working either.
Something is preventing this or what?
Code::

foreach($mailsIds as $num) < // Show header with subject and data on this email $head = $mailbox->getMailHeader($num); echo '
'; echo $head->subject.'  ('; if (isset($head->fromName)) echo 'by '.$head->fromName.' on '; elseif (isset($head->fromAddress)) echo 'by '.$head->fromAddress.' on '; echo $head->date.')'; echo '
'; // Show the main body message text // Do not mark email as seen $markAsSeen = false ; ---> Tried with true also $mail = $mailbox->getMail($num, $markAsSeen); if ($mail->textHtml) echo $mail->textHtml; else echo $mail->textPlain; echo '

'; $mail->markMailAsRead($num) // tried also as $mailbox-> // Load eventual attachment into attachments directory $mail->getAttachments(); >

The text was updated successfully, but these errors were encountered:

Источник

Оцените статью