I need to create to send a message to all new users registered on my website. I created a table called messages that admins can store (insert) the messages from the admin panel to this table and these messages are simply shown to all users with a foreach.
I don't know if this the best way to do something like that!
Anyway, the problem is that when any new user register and open his dashboard he just found the old messages
This is the table :
image
And this is the simple code for foreach
$msgForAll = Message::latest()->get();
I'm not sure how to display new messages to the users.
Again the way I made this idea is wrong, I know that ):
You can use eloquent's where spefically the whereDate queries in your case to get certain rows past or before date.
As an example relating to what you want to do, it can be along the lines of this:
// Given that you are using Auth to get the user's data...
// Get messages that were created after the user's creation date
$messages = Message::whereDate('created_at','>',Auth::User()->created_at)->get();
Related
Actually I am facing a problem. I need to send a notification to the admin whenever any update happens in any modal, in any user, and in any table.
To clarify more :
I have a client, a company, and an employee.
Under each part, I have many tables ( e.g: under client I have client's profile table, under company I have many tables corresponding to company's lawyer, company's files, etc.., similarly to the employee)
So correspondingly , I have many controllers thus many update methods.
So In order to send a notification to the admin whenever any update happens in any situation under any user. It's too annoying that I must include the notify statement $admin->notify(new UpdateIsMade()); under ALL update METHODS!
It's kind of redundancy.
What I want to actually approach is to find a solution that let me automatically send a notification to the admin whenever any update happens by any user under any table.
I tried to make a middleware function and tried to send a notification whenever any update happens under the client user (kind of sampling) :
public function handle(Request $request, Closure $next)
{
$clients = \App\Client::all();
foreach ($clients as $client) {
if($client->user->wasChanged()){
$arr_of_changes = $client->user->getChanges();
$admin=User::where('profile_type' , 'App\Admin')->first();
$admin->notify(new UpdateIsMade($arr_of_changes,$original_arr));
}
}
return $next($request);
}
Unfortunately this middleware didn't work.
I guess the problem is that $client->user->wasChanged() is not giving a true value whenever an update happens. It looks like wasChanged only works after the save process in the update method.
Any suggestion for my problem? Thank you for your time and sorry for the long text!
It might be less of a headache if you consider using database triggers so monitoring changes is completely separate.
You could create a new table called "change_log" with fields "action" and "created_at" then set up the trigger to insert a new record upon update of any tables you want to monitor.
Then you just set up a CRON to run at intervals that would call a Laravel controller to query the change_log and see if there are any records in it. If there are, the code would take the appropriate action to send an alert then empty the table. The change_log would remain empty until the DB trigger fires due to changes made.
I'm building an app in Laravel, and using the eloquent ORM.
i want to create a simple private messaging system within my application. Nothing mad complex. It will basicaly be like email. It has a recepient, title, content and timestamps.
I want the ability to show if it's read/unread. Would having a column as a boolean called 'read' which has a default value of 0 work. When the user clicks on it, the read attirbute changes to 1, and the message is marked as read.
How would I update the attribute, when the message is opened.
Laravelish way, is to use a column named read_at, and set it to NULL if unread, and the current timestamp when read.
Migration part of read_at may look like:
$table->timestamp('read_at')->nullable()->default(null);
Note: do not forget to add read_at to $dates array so working with read_at is easy - carbon way.
If you are not using front-end framework like Angular or react, make ajax request to your controller function. In controller function update the db table using ORM or Query Builder. Using Ajax you can do it without affecting to the user view.
if you are making simple messaging system, store a flag with read. When the user clicks on a message, take that message's id and update that records read with 1 and unread 0 using an update action.
I have entity "Work Order" for which I have defined many custom views. Work Orders can have records with statuses as "active ,cancelled, closed, inprogress, submitted" etc. My requirement is - currently logged in user who belongs to a specific team "sales representative" should be able to see all records on view.This can be done easily, but If current logged in user does not belongs to "sales representative" team, she should not be able to see "cancelled" records on view but all other record should be visible to her. How can I achieve this using custom filters if it is possible? Or by code changes?
It is possible to do this with custom code. Without questioning the "why" you'd like to do this (possibly it's sensitive information or something?), you can achieve it using a RetrieveMultiple plugin registered on the pre-operation event. Within this plugin one of the input parameters passed in is called "Query" and will have a QueryExpression. You can simply add a filter to this query in the plugin and the relevant rows will be filtered out. Something like this:
var query = (QueryExpression)context.InputParameters["Query"];
var condition= new ConditionExpression()
{
AttributeName = "statuscode",
Operator = ConditionOperator.NotIn,
Values = { 2, 3 } // Or whatever codes you want to filter!
};
query.Criteria.AddCondition(condition);
To check the current user you can grab the user id from the plugin context and retrieve the necessary info you would like to check.
Doesn't sound like this is possible with advanced find alright. You may be able to achieve it using security roles though. If you could assign cancelled work orders to a specific team, and then organise your security setup so that users who are not sales representatives can't see work orders from that specific team, then it might work. Unfortunately you would have to reassign the cancelled work orders which is not always an option.
Otherwise, you might have to go with a separate view for cancelled work orders, out of the box advanced find should allow you present a blank grid of you are not on the right team. But now obviously you are not presenting a whole view of the work orders.
In general I would go with the security option, and just make it work. Any other option is just a stop-gap. Users can always create custom views, so if you don't lock down access using security roles, the data is still accessible in indirect ways.
I am referring to this thread
Laravel 4 Relationship: messaging system
I created the tables and also made the relations inside both model "Message" and "User".
Now i can fetch the list of messages sent by me through this command
user::find($myid)->sent_messages()->get();
MY QUESTIONS IS
How can we get list of users that has been messaging to me (logged in user) ?
I mean in the messages table "from" can be logged in user (1) or "to" can be logged in user (1) . I want to get list of other users that are chatting with the (logged in user) .
Hope you understand my question and help. I am new to laravel and trying to learn things and best practises. If you have suggestion to use any messaging system already there in github. I found one name "Laravel-messenger" but not suited to me.
Your suggestions and answer are highly appreciated. THANKS
May be instead of using table user_messages (just drop it) you just modify table Messages to below form:
Messages: id | from_user_id | to_user_id | content
Then you can make query:
$received = Messges::where('to_user_id','=',my_user_id);
$send = Messges::where('from_user_id','=',my_user_id);
$result = $received->union($send)->get();
(BONUS: you can also add column replay_from_message_id (null for first message) to save backward sequence of messages (thread). And then you can also add column first_message_id to simplify getting thread query)
I have a relation on user in Parse 'messages'
I want to issue a query via cloudcoud that retrieves all friends of user (another relation, friendship) AND messages if there are any messages where the current user is the recipient.
I got this far:
var relation = current.relation("friendship");
var queryRel = relation.query();
queryRel.select("username","color","count");
queryRel.ascending("username");
var innerQuery = new Parse.Query("Message");
innerQuery.equalTo("recipient",current);
queryRel.matchesQuery("messages", innerQuery);
...but that only retrieves friends of the current user who have messages, not those who are friends but don't have messages. I need both!
If at all possible, I also want to retrieve the messages themselves, not just pointers. This is all so I can return properly organised data to the client so my apps don't have to do lots of looping to sort things.
I would use the include method here. The way it works is you would do queryRel.include("message"); so you won't just get the pointers. I believe you should also remove the second query for this to work.
The above code is assuming your column pointing to the Message table is called "message"