I have a memory based session table containing the following:
id
sessnhash
userid
created_at
updated_at
The idea is that when a user first logs in a random sessnhash is created and passed back so that all further actions have to send that sessionhash for server requests. These go through a loggedin? method on the application controller which simply checks that a row in the table exists for that sessionhash.
I now want to extend this functionality to keep a tab of the last time any activity happened for that user and I thought one of way of doing this would be to immediately do a save after finding the sessionhash, hoping that this would then update the 'updated_at' attribute.
However Rails is too clever and doesn't update anything as in reality nothing has been updated.
Is there any way of forcing Rails to update the updated_at attribute without having to make any data changes to any of the other attributes?
I've just learnt that I need to do.
sessn.touch
which will update the updated_at attribute.
It's all in here: touch.
Related
I would like to add to all models on my Laravel project two custom fields called updated_by and created_by, automatically filled and saved on db with the name of the logged user (I'm using JetStream), exactly like the fields updated_at and created_at.
I wonder if there is a clean procedure, before starting to add the fields on model one-by-one, and handle them in the controllers, in every store/update function I have. Extending Model and using the new class for every models seems a good solution, but I'm not really sure. And I do not know how and where to fill one-time the fields with logger user data.
Thanks, I'm newbie in Laravel and hope this is not a basic question.
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 implemented Aloglia for my Movies table with actors as relational table and it works fine.
Problem:
When I update any movie its also updating algolia index (its good). But how can I update index if I made any change in relational table (for example update an actor of movie).
How to push a specific record manually with laravel scout.
Thanks
The issue itself lies in laravel's events. Whats happening is scout is listening for an 'updated' event which only occurs in laravel when the model object is saved and is dirty (aka value differ from that in the db).
There are two ways you can do this.
The bad lazy way would simply be to add ->touch() to the model prior to save - this will force the updated_at field to update and ultimately trigger the updated event. This is bad as you're wasting a DB query.
The second and preferable way is to register another observer on 'saved' which triggers regardless of whether or not the object is dirty. Likely you either want to check if the model is dirty and only index when its not (to prevent double indexing from the updated event) or just de-register the 'updated' listener that comes in Scout.
When I save the records, I need the uid properties for the initial rows; because of I can assign a value each of them through the uid.
However, toJson overriding forces to ignore some properties like dirty, uid, etc...
How can I prevent this? Or How can I find a way to solve this?
You should not persist the uid on the backend or override it.
The uid is an internal mechanism used by Kendo UI to track its widgets and widget components to allow user interaction and component functionality. Each uid is generated dynamically every time the widget is rendered, so, attempting to do what your thinking of doing won't work.
The proper solution is you should assign your own unique id property to your records and persist it on the server/client. Then, when a change is made client-side, you post the record data along with its unique id, find the associated record by id in the backend database, and update the associated record data in the database.
I am assuming I cannot do this using sessions but rather the DATABASE. So the user would sign in, it would set their TIMESTAMP and I display that from the database. Then it becomes deleted when the user logs out or their session is terminated. How would the code look for this?
The better question is, is my logic correct? Would this work? Does this make sense?
By default application servers store session data in temporary files on the server.
By storing session data in a database table you are able to create an interface that will show information about the users that are logged in. Apart from that, using this (database) approach is a serious advantage if you need to scale your application by adding more than one server.
One of the most popular ways to implement such a functionality is to create a session table containing your users' session data. This may look like:
create table session (
id number primary key,
data varchar(240),
timestamp date
);
The data column stores all the session data in a serialized form this is deserialized each time a user requests the data.
Serialization and deserialization may have inbuilt support depending on the platform you are using. For example, if you are using PHP, the functions session_encode and session_decode may be found useful.
You can't find out when a user logs out in PHP and the Javascript workarounds are a bit far from a stable solution.
A couple of things you need to do: Create a column in your user table called last_activity and update their last_activity to the current time whenever a user loads a page.
For a list of who's online, query the db for users with last_activity values more recent than 10 or 20 or whatever minutes ago.
To update the last_activity column use:
UPDATE users SET last_activity=CURRENT_TIMESTAMP() WHERE id=2
For a list of users online
SELECT * FROM users where last_activity >= (CURRENT_TIMESTAMP()-(60*20))