UPSERT with Push - laravel

I am trying to achieve an upsert with push in Laravel using MongoDB.
Basically, I am saving the number of likes from a YouTube post. If the record already exists, I would like to push to an array called 'history'; otherwise, I would like to create the record with all the post details.
Does anyone have any idea how I can achieve this?
I am using this package for the connection with MongoDB.
https://github.com/jenssegers/laravel-mongodb/
Thanks.
This is what I want to achieve more precisely:
https://user-images.githubusercontent.com/44676430/154502252-0c3dcef4-9bdd-49ae-86fd-3076bc37bbc7.png

here is an example, the name and author are the key and the quantity is the value.
DB::table('books')->upsert([
[
'name' => 'J.K. Rowling',
'author' => 'Harry Potter',
'quantity' => 15
],
[
'name' => 'Cal Newport',
'author' => 'Deep Work',
'quantity' => 20
]
], ['name', 'author'], ['quantity']);

Related

How to start using Slug URLs without breaking old in Yii2

i want to rewrite my URLs from
/view?id=100
to
/view/100-article-title
But the site already has several thousand search pages. As far as I know, such a change can be bad for SEO. Is there a way to keep the old URLs working when switching to the new routing.
*I am creating links as follows:
Url::to(['view', 'id' => $item->id])
Is it possible to create links further through ID?
you can create getLink() function on your model and use it on. Then, when function runs you can check id if id <= 100 then return Url::to(['view', 'id' => $item->id]) else return Url::to(['view', 'id' => $item->id, 'slug' => $this->slug])
And add route with slug on your main.php
Look at my example.
First config urlManager in config.php in 'app/config' folder. In my case look like:
'components' => [
...
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
''=>'home/index',
'<slugm>-<id:\d+>-' => 'home/view',
],
],
.
...
],
and create link as fallow:
Url::to(['/home/view', 'id' => $model->home_id, 'slugm' =>$model->title_sr_latn])
and finaly url look like:
https://primer.izrada-sajta.rs/usluge-izrade-sajtova-1-

Guzzle Post Null/Empty Values in Laravel

I've been trying to work with Guzzle and learn my way around it, but I'm a bit confused about using a request in conjunction with empty or null values.
For example:
$response = $client->request('POST',
'https://www.testsite.com/coep/public/api/donations', [
'form_params' => [
'client' => [
'web_id' => NULL,
'name' => 'Test Name',
'address1' => '123 E 45th Avenue',
'address2' => 'Ste. 1',
'city' => 'Nowhere',
'state' => 'CO',
'zip' => '80002'
],
'contact' => [],
'donation' => [
'status_id' => 1,
'amount' => $donation->amount,
'balance' => $donation->balance,
'date' => $donation->date,
'group_id' => $group->id,
],
]
]);
After running a test, I found out that 'web_id' completely disappears from my request if set to NULL. My question is how do I ensure that it is kept around on the request to work with my conditionals?
At this point, if I dd the $request->client, all I get back is everything but the web_id. Thanks!
I ran into this issue yesterday and your question is very well ranked on Google. Shame that it has no answer.
The problem here is that form_params uses http_build_query() under the hood and as stated in this user contributed note, null params are not present in the function's output.
I suggest that you pass this information via a JSON body (by using json as key instead of form_params) or via multipart (by using multipart instead of form_params).
Note: those 2 keys are available as constants, respectively GuzzleHttp\RequestOptions::JSON and GuzzleHttp\RequestOptions::MULTIPART
Try to define anything like form_params, headers or base_uri before creating a client, so:
// set options, data, params BEFORE...
$settings = [
'base_uri' => 'api.test',
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'ajustneedatokenheredontworry'
],
'form_params' => [
'cash' => $request->cash,
'reason' => 'I have a good soul'
]
];
// ...THEN create your client,
$client = new GuzzleClient($settings);
// ...and finally check your response.
$response = $client->request('POST', 'donations');
If you check $request->all() at the controller function that you are calling, you should see that were sent successfully.
For those using laravel, use this:
Http::asJson()

Laravel 5.6. How to test JSON/JSONb columns

$this->assertDatabaseHas() not working with JSON/JSONb columns.
So how can I tests these types of columns in Laravel?
Currently, I have a store action. How can I perform an assertion, that a specific column with pre-defined values was saved.
Something like
['options->language', 'en']
is NOT an option, cause I have an extensive JSON with meta stuff.
How can I check the JSON in DB at once?
UPD
Now can be done like that.
I have solved it with this one-liner (adjust it to your models/fields)
$this->assertEquals($store->settings, Store::find($store->id)->settings);
Laravel 7+
Not sure how far back this solution works.
I found out the solution. Ignore some of the data label, Everything is accessible, i was just play around with my tests to figure it out.
/**
* #test
*/
public function canUpdate()
{
$authUser = UserFactory::createDefault();
$this->actingAs($authUser);
$generator = GeneratorFactory::createDefault();
$request = [
'json_field_one' => [
'array-data',
['more-data' => 'cool'],
'data' => 'some-data',
'collection' => [
['key' => 'value'],
'data' => 'some-more-data'
],
],
'json_field_two' => [],
];
$response = $this->putJson("/api/generators/{$generator->id}", $request);
$response->assertOk();
$this->assertDatabaseHas('generators', [
'id' => $generator->id,
'generator_set_id' => $generator->generatorSet->id,
// Testing for json requires arrows for accessing the data
// For Collection data, you should use numbers to access the indexes
// Note: Mysql dose not guarantee array order if i recall. Dont quote me on that but i'm pretty sure i read that somewhere. But for testing this works
'json_field_one->0' => 'array-data',
'json_field_one->1->more-data' => 'cool',
// to access properties just arrow over to the property name
'json_field_one->data' => 'some-data',
'json_field_one->collection->data' => 'some-more-data',
// Nested Collection
'json_field_one->collection->0->key' => 'value',
// Janky way to test for empty array
// Not really testing for empty
// only that the 0 index is not set
'json_field_two->0' => null,
]);
}
Note: The below solution is tested on Laravel Version: 9.x and Postgres version: 12.x
and the solution might not work on lower version of laravel
There would be two condition to assert json column into database.
1. Object
Consider Object is in json column in database as shown below:
"properties" => "{"attributes":{"id":1}}"
It can assert as
$this->assertDatabaseHas("table_name",[
"properties->attributes->id"=>1
]);
2. Array
Consider array is in json column as shown below:
"properties" => "[{"id":1},{"id":2}]"
It can assert as
$this->assertDatabaseHas("table_name",[
"properties->0->id"=>1,
"properties->1->id"=>2,
]);
Using json_encode on the value worked for me:
$this->assertDatabaseHas('users', [
'name' => 'Gaurav',
'attributes' => json_encode([
'gender' => 'Male',
'nationality' => 'Indian',
]),
]);

Elasticsearch searching in different results

Im using Elasticsearch with laravel and Elasticquent https://github.com/elasticquent/Elasticquent over the Eloquent model.
Each user of my project can search in a different result of objects.
I have for example
$user_books->addToIndex();
Books::search($search);
And the problem is that everytime when someone go to the current step the elasticsearch is filled with his results.And at the end I have a combined result for each user with their books together.. which is a problem.
I need one user to search only into his results.
How can I achieve that? I think that I probably must delete the indexes after search because I dont need them anymore.
And then
There is no need to delete your indexes,
instead you should index the books added by users by building a custom index
of their user_id like this :-
$data = [
'body' => [
'book' => '$book->name',
'user_id' => Auth::user()->id,
],
'index' => 'your application name',
'type' => 'books',
'id' => $book->id,
];
and then you can use custom queries to search for book belonging to that user only like this
$books = Book::searchByQuery(array('match' => array('user_id' => Auth::user()->id)));
Refer to Query based search and Indexing and Mapping section here

Updating a member's group interests using MailChimp API V3

I am running the following code (I've hidden ID's) to add/update a subscriber's interest groups in a MailChimp list:
$mailchimp->patch('lists/1234567/members/' . md5('test#test.com'), [
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => 'Ben',
'LNAME' => 'Sinclair',
),
'interests' => array(
'abcd1234' => true,
'defg6789' => true,
),
]);
The interests key is what I'm having issues with.
I presumed whatever you put in this key will overwrite what currently exists.
It doesn't seem to be the case. It only adds new interests but does not remove any if the ID's are not in the array. I am not getting any errors.
Does anyone know how to overwrite interest groups? Or if that's not possible, is there a way to remove interest groups?
For completion I wanted to add this answer so people stumbling upon this post can find a quick solution.
$mailchimp->patch('lists/1234567/members/' . md5('test#test.com'), [
'status' => 'subscribed',
'merge_fields' => array(
'FNAME' => 'Ben',
'LNAME' => 'Sinclair',
),
'interests' => array(
'abcd1234' => true, // Attached
'defg6789' => false, // Detached
)
]);
In this example the interest 'abcd1234' will be attached and the interest 'defg6789' will be detached.
Other interests that are not listed will remain on their original value.

Resources