I have been succesful in uploading my data to algolia using laravel. My create method looks like this:
Beer::firstOrCreate([
'name' => $beer['title'],
'description' => $beer['description'],
'path' => '/uploads/gall/' . $filename,
'brewery_id' => '',
'abv' => $abv
]);
It gathers data from an api and works perfectly, except for the abv value. It needs to be a numeric type in algolia but whenever I check it in algolia it is a string.
Even when I force it to a float or int by using:
(float)$abv
I still end up with a string in my Algolia database. Funny enough the primary id for this record that is being auto incremented is not a string and looks fine in the algolia database.
To implement algolia in laravel I used the laravel helper in my model:
// Send new records to the algolia database
use AlgoliaEloquentTrait;
I hope somebody can maybe give me slight hint of what I could be doing wrong.
I am assuming that the data type of 'abv' inside your database is float and not string, if this is the case then try using floatval($abv). Your code would be something like:
Beer::firstOrCreate([
'name' => $beer['title'],
'description' => $beer['description'],
'path' => '/uploads/gall/' . $filename,
'brewery_id' => '',
'abv' => floatval($abv)
]);
Cheers,,
Related
I'm using an api that has a "range" parameter that can apply to several different parameter items. The range i'm focusing on is "price". I'm using guzzle in laravel and according to the api documentation, the query for this particular parameter should be written like this "&range_facet=price|500|2500|250"...this is broken down into the minimum, maximum, and interval values of the price range parameter. That's not necessarily important to this question. When i try and run this query as is, i get nothing returned. When I remove that particular parameter, i get values but obviously they're not filtered the way i want them to be. When i run this in Insomnia, the pipes are replaced by "%7C", which is obviously (obviously?) not interpreted by the api as it's not how it's waiting for the GET request to be made. How can I insert the pipes into the query so that it calls the correct way?
I've tried to create an additional nested array with the price value being broken up into key value pairs but that didn't work either.
'range_facets' => ['price'['start'=>'500', end=>'2500', 'interval'=>'250']],
$client = new Client();
$result = $client->request('GET', "http://api.example.com", [
'headers' => [
'Host' => 'example-host',
'Content-Type' => 'application/json'
],
'query' => [
'api_key' => 'my_api_key',
'range_facets' => 'price|500|2500|250',
'year' => $year,
'latitude' => '30.170222',
'longitude' => '92.01320199',
'radius' => 500,
'start' => 0,
'rows' => 50
]
]);
I'd like to filter my prices but I need the pipe to be able to do it.
This is exactly how it should be. %7C should be decoded on the server side to | automatically (about query string encoding).
I bet the issue is in different place.
$locations[] = [
'id' => $r['id'],
'city' => $loc['city'],
'state' => $loc['state'],
'country' => $loc['country'],
'long_lat' => "POINT(".$loc['longitude']." ".$loc['latitude'].")"
];
$this->db->update_batch('locations', $locations, 'id');
My table has a POINT datatype column for long_lat. The above snippet does not work with the long_lat insertion entry, without it it works fine. For some reason, update_batch cannot handle the POINT() datatype.
Codeigniter doesn't seem to like the POINT type, or its escaping something. Is there a work around for this?
i can't comment so.. try it like this and it will help if you echo the query and post it
'long_lat' => "'POINT(".$loc['longitude']." ".$loc['latitude'].")'"
Could someone please provide a simple example of the usage for dealing with Odoo's one2many, many2many and selection fields when using Laradoo (or ripcord)?
Specifically how one would use them with create() and update(). In Python, it seems as if these are dealt with using special tuple commands however for PHP documentation seems very hard to find for these types of things and it would be extremely helpful.
For illustrative purposes in my particular project, I haven't been able to figure out how to relate a CRM lead tag to a lead during the creation process using Laradoo:
$id = $odoo->create('crm.lead', [
'type' => 'lead',
'priority' => 0, <-- what do we pass here for this selection field?
'name' => 'Example',
'contact_name' => 'John Doe',
'phone' => '555-555-5555',
'email_from' => 'example#domain.com',
'description' => 'Just some text.',
'tag_ids' => [1], <-- What do we pass here for this one2many field?
]);
In the example above when trying to set the priority selection field to an int other than 0 fails and when trying to pass an array of tag_ids (1 is valid tag id in my project), the lead remains untagged.
First of all selection field values are just string values that need to be part of the field defined selection values.
The values for relational fields like Onetomany and Many2many are ruled by the command formated values that you could read at:
https://github.com/odoo/odoo/blob/11.0/odoo/models.py#L3020-L3055
For the php api usage with ripcord you could set the tag_ids field value like:
$id = $odoo->create('crm.lead', [
'type' => 'lead',
'priority' => '0',
'name' => 'Example',
'contact_name' => 'John Doe',
'phone' => '555-555-5555',
'email_from' => 'example#domain.com',
'description' => 'Just some text.',
'tag_ids' => array(array(4,1)),
]);
This translate as that 1 is the id of a known and already existing crm.lead.tag that you could link to the m2m tag_ids field using the command 4. This could also be expressed using command 6 to link multiple ids on the same command value:
'tag_ids' => array(array(6,0,array(1,2,3))),
where using command 4 it will be:
'tag_ids' => array(array(4,1), array(4,2), array(4,3)),
I have a quite complicated form, which has a lot of fields - everything works fine, but I also need to store a slug and path to thumbnail. The problem is, that when I am using store method and create a post, I need to dynamically create that slug and path based on the post ID (which is not created yet and therefore I don't have an ID).
This is the chunk of the code I use to store the main post data:
Post::forceCreate([
'title' => $title,
'slug' => create_url_slug($title, $id),
'thumbnail' => thumbPath($id),
'description' => request('description'),
'password' => bcrypt(request('password')),
'user_id' => get_user_id()
]);
Here I passed two functions -> create_url_slug and thumbPath. If I put these functions above this chunk of code, the error will be thrown because the ID does not exist yet. On the other hand, if I put these functions under this code, the error will also appear, because those functions would be undefined. Can I somehow use closures or divide this method to two parts?
Thanks anybody in advance.
A way to do this is to create the model and right after assigning the values for example
$post = Post::forceCreate([
'title' => $title,
'description' => request('description'),
'password' => bcrypt(request('password')),
'user_id' => get_user_id()
]);
$post->slug = create_url_slug($title, $post->id);
$post->thumbnail = thumbPath($post->id);
$post->save();
I do not recall if make creates an id for the Model else you could use Post::make and save a call to the database. Worth a try.
This function is not in the Laravel documentation, I have found it in the source code however I am not completely sure how it should be used. For example, if I am working with products, I want to either insert or update a product in the database based on its UPC. If a product with the same UPC exists, update it with the new values. If not, insert the new values as a new product. How should this be done using this function?
Thank you!
Insert or update a record matching the attributes, and fill it with values.
updateOrInsert(array $attributes, array $values = [])
https://laravel.com/api/master/Illuminate/Database/Query/Builder.html#method_updateOrInsert
DB::table('products')->updateOrInsert(
[
'upc' => $request->get('upc'),
],
[
'upc' => $request->get('upc'),
'name' => $request->get('name'),
'vendor' => $request->get('vendor'),
'description' => $request->get('description')
]
);