Algolia + Laravel -- Do not update index at a specific instance of save() - laravel

I am using Laravel Scout + Algolia. We have reached our usage limit because every time someone visits a content page on our site I update the entry in the database to increment the page views counter. This then triggers Algolia to update, even though the page_views variable isn't even stored in Algolia. I am wondering if there is a way to update the database entry without triggering the Algolia update.

In the documentation there's a paragraph about that here.
Basically you can run a closure without syncing to Algolia in this way:
$currentInstance = 'your-model'; // This is the variable you want to inject
App\YourModel::withoutSyncingToSearch(function () use ($currentInstance) {
// Do the increment, and algolia indexing will not be updated
});

Related

when using laravel scout, searchable() isn't updating related models

I am having an issue getting related models to update with scout elastic search.
$event->priceranges()->delete();
$event->priceranges()->Create([
'price' => $ticket['ticket_price']
]);
$event->update([
'show_times' => $request->showtimes,
]);
$event->searchable();
In my database I see the event and price range tables update. However when I look at my elastic search data, only data on the event has been updated. Any related models are not updated.
If I make a second update to the model and price range model, then the elastic search data shows my data from the first update I made (it is always one update behind for related models)
I tried doing
$event->pricerange->searchable()
but gives me an error because I don't have a searchable index for pricerange, I am just using my event model and its relationship to index. Is there a way to force the update other than searchable()?
Looks like your relationship is being indexed in the Event model index, correct?
Probably it's not being updated because the relationships are already loaded, and Laravel doesn't update the relationship data that is already loaded, for example:
$event = Event::with('priceranges')->first()
var_dump($event->priceranges->count()): // outputs for example 5
$event->priceranges()->create([...]);
var_dump($event->priceranges->count()): // still outputs 5, meaning that the created pricerange is not loaded
So, to fix this issue you can reload the model before calling searchable():
$event = $event->fresh();
$event->searchable();
But, note that everytime you update, the searchable method is already being called, so you will be indexing it two times on every update.
So, alternatively you can update your toSearchableArray() method in Event model to get a fresh model before returning the data (I'm assuming that you are using the babenkoivan/scout-elasticsearch-driver).

Laravel query updates multiple rows unexpectedly

In my web app I am trying to update one of many notes that are on one work order.
If I write query like this (Case 1):
// NOTE UPDATE WORKING FINE
Model::where('RN_key', $RN_key)->where('anNo', $anNo)->update(['acNote' => $request['note']]);
I get what I want, it's working fine - only one note on work order is updated as it should be.
If I write query update using first() and save() functions like this (Case 2):
// NOTE UPDATE NOT WORKING - Updates EVERY note on one work order but should update ONLY one
$getWorkSpecification = __vVX_ServisniNalog_Radovi::where('RN_key', $RN_key)->where('anNo', $anNo)->first();
$getWorkSpecification->acNote = $request['note'];
$getWorkSpecification->save();
Then it updates and overwrites every note on that same order thus every other note on order is lost. Why is that happening? I need to make it work with first() and save() functions because then it fires event so Laravel Auditing can save audit so I can have logs on every model change any user make.
With Case 1, every note updates fine but no audits are logged and thats the problem.

How to display in blade which rows are soft deleted?

I have created a polling system and in the backend (CMS area) I want the ability for admins to be able to remove polls. When an admin removes a poll, it should soft delete the poll. This is working as intended, however I also want to have the ability for the admin to be able to restore a poll. To do this I am displaying all of the polls (including the soft deleted polls) in the admin area.
PollController index() to get all polls
$polls = Poll::withTrashed()->get();
In the blade I want to have two different buttons for each poll. One of restoring and one for deleting but I only want to display 1 button for each poll depending on whether it can be restored or deleted.
To do this, I have put this inside the foreach in the blade:
#if($poll->trashed())
// Restore button
#else
// Delete button
#endif
However the issue is, trashed() keeps returning true for all the polls when only 1 out of the 3 polls I have are actually soft deleted. I am unsure as to why trashed() returns all of these are true?
How would I get this method working correctly? Thanks.
PART 1
It depends on your query. When using soft deletes, Laravel will query all models that are not soft-deleted by default. When you also want to get the soft-deleted models, you need to call the withTrashed() method on your query. Read more here:
https://laravel.com/docs/5.5/eloquent#querying-soft-deleted-models
To understand what withTrashed() does, you need to understand how soft-deleting works. Soft-deleting models works by adding a new column to your database tables called deleted_at. It's value defaults to null. When you soft-delete a model, Laravel will put the current timestamp into that column. Therefore, this field doesn't contain a null value anymore.
When querying models when using soft-deletes, Laravel appends a deleted_at is null condition to the query. Calling the withTrashed() method, removes that condition from the query.
Have a look on the source of the default query modifier and the withTrashed method.
PART 2
That are events. You can call that to tell Laravel, that it should execute that specific closure when this event happens. In your example, it is listening for the "deleting" event. See more on that here:
https://laravel.com/docs/5.5/eloquent#events
PART 3
You can entirely delete soft-deletable models with the forceDelete() method. See "Permanently Deleting Models" here:
https://laravel.com/docs/5.5/eloquent#querying-soft-deleted-models
FOR EXAMPLE
$items = App\Model::withTrashed()->get();
If you want to restore a single item, just find it by ID:
$item = App\Model::find($id);

Algolia Update Index On Relational Database change with Laravel Scout

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.

Update, Insert, Delete records Joomla Table

I'm able to fetch information for whatever table I need the problem here is the update, insert, and delete records, is not working..
I have read the Joomla doc's but even doing the simplest update queries are not working... so here is my code:
UPDATE:
// I'm getting the data from an array
if (!empty($_POST['data'])) {
$getData = json_decode($_POST['data'], true);
}
// after this line I have a foreach for the array
// in the foreach I have a few IF's
// ether if the result from IF is True or False
// in both I have similar queries
// So let say IF return true;
// Lets prepare the Data using OBJECT's
$object = new stdClass();
$object->product_id = $v['product_id'];
$object->product_name = $v['product_name'];
$object->product_price = $v['product_price'];
$object->product_number = $v['product_number'];
$result = JFactory::getDbo()->updateObject('#__tienda_product', $object, 'product_number');
// that should Update my tables but it doesn't ... now my table has about 21 columns
// but I only need to update 4 columns base on the column product_number
You might have notice the $v['product_id'] where $v is from the foreach, the foreach and the first query are working fine, I did a few echo's before moving to the Update part just to make sure I'm getting the correct data in a format that it should be... any way... the Update part is not working... so I thought it may be because of my table I when to use one table from the regular instalation of joomla... but it still the same no result on the update...
Does any one know how to "update" a table using Joomla CMS as framework?...
Remember I want to use the updateObject() method...
Why don't I use Joomla Framework instead of the CMS's library?
Well, I can give you a few examples, let say:
a business man want a simple basic report, he doesn't care about managing the site he has people to do that, he also gets a sales report, but he doesn't trust the crew just yet and he want to able to see and compare the result, and he needs and standalone without all the fancy tools that joomla has and for that he need an standalone app that can give that kind of report... ok I might have gone a bit far, but the idea is to have basic, simple, easy to read reports and updates, and that is why I have started this project.
Again, I already went to joomla and read the docs but following their examples it just not working... now, am I have to declear all of the columns even if they don't need to be update? or am I missing an execution for the query which in joomla doesn't mention any executions when using $object() only when using regular queries SQL... the idea is to use $object() ...
Thank you for taking the time.
You have defined the updateobject as a variable, and are not calling it. try changing this:
$result = JFactory::getDbo()->updateObject('#__tienda_product', $object, 'product_number');
to this:
JFactory::getDbo()->updateObject('#__tienda_product', $object, 'product_number');
Also, on a side note, you should not use $_POST and instead should use JInput

Resources