Laravel - Upsert with Case Sensitive Search Field - laravel

I'm using Laravel to synchronise a set of third-party data that has an ID of a case-sensitive column.
I'm using Laravel's Model::upsert($data, ['third_party_id'], ['field1', 'field2']) method to update this all and it's working except for the fact that it's case insensive, so it's not putting in a good portion of the data.
Is there any way around this, using MySQL?

Related

Laravel - Automatically store history in column

Can anyone help me to automatically save the history of all changed columns in a model to a history column in the same table as JSON format with column(s) name, original value, changed to, changed by?
I am looking for Traits like centralized function to use in all models.
I am using Laravel 8 with PostgreSQL.
This answer is maybe outdated since it is Laravel 4. But if it works, it is quickest way to insert log in DB wherever save() method is called for that model.
https://stackoverflow.com/a/20694395/13893004

Laravel Backpack Crud Search encrypted data fields

I have a encrypted user table and need to find a solution for search from encrypted data fields using Laravel Backpack crud 4.1.
How do we filter data from encrypted fields?
I know this is old, but for anybody stumbling upon this, I believe the best option would be to define a custom searchLogic for each encrypted column you have. That way you take full control of how search is done for that column.
Alternatively you can overwrite the search() method of the ListOperation, but that’s more invasive and I don’t think it’s necessary.

Persistent Laravel data filters across multiple views

I am writing a multi-language dictionary app. When the user selects a language to use data from, that language should apply to every page until they select a different language. Ideally, the language should be part of the URL so that the address for the English word "double" and the French word "double" is different. It should also be possible to specify no language, so that "double" would display both the English and the French word. I will also want to filter the data on multiple fields at the same time, e.g. the word itself and the language.
I'm trying to fit this into the Laravel resource concept. The index view of Word should show all words filtered by the language, or not filtered if no language is specified. create should keep the language from index. store should just use the form data. The language can be included as a hidden field in the create view if it's been specified. show doesn't strict speaking need a language filter, but if the user then goes back to index, the filter will still need to be applied.
I started using routes, but that means I'll have to hard-code a route for every filter. I've also thought of using session data, but that means the URLs wouldn't include the filter. If the filters are appended as a query string how would Laravel access them? Is this a good solution?
I'm using Laravel 5.8. What's the best Laravel way to persist this type of data filter across views?
I have similar issues in many areas of our apps. We occasionally use Session for this, but generally find the most efficient and easiest way to solve this is to attach a database field to the user object.
If you are using any type of auth, Laravel is already going to boot the user object on every page view, thus the filter can be pulled with no extra calls to the database. If no language is specified, the \Auth::user()->current_lang_id will be null, and thus no filter would be applied. We typically use a relationship (e.g. 'currentLang()') which makes it easy for the user to see the language and can automate binding on the form.
The nice part of this, we've found, is that it individualizes it per user, and it 'remembers' the user's preferences between sessions in a simple way - no need to make dozens of routes or a special variable + logic on multiple routes because those routes include your filter. Instead, you can put your logic at the top of a base controller and be done with it.
Lastly - changing the language when it is a db field on the user is just standard, simple CRUD.
HTH

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.

Using two different slugs on a route

I'm upgrading an old procedural site to laravel 5.2, and I'm struggling with the old routes I made.
On this website, the routes were made like this : {user_slug}/{content_slug}.html. For the moment, I use cviebrock/eloquent-sluggable to generate the slugs, but I'm open to another one if this one cannot meet my needs.
I have two questions :
Can I make the content-slug unique, but per user ?
How can I write the route and the controller in order to match the correct user slug ad the correct content slug ?
I have not done this myself but I believe there would be a way in the validation rules to do this. Here is an untested rough draft to check content_slug in the posts table but only check uniqueness where the user_id field equals a variable:
'content_slug' => "unique:posts,content_slug,NULL,id,user_id,$user->id"
Depending on who you ask, they may advise you (either instead of or as well as doing the above) to set up a key in the database based on the user_id and content_slug fields. This way the database returns an error if an insert is attempted as well as gives a performance boost when running a query off that index. Queries off of an index can literally give an exponential performance increase.

Resources