Optimized query in laravel with relationship - php

I have 3 model: Tag, Post, User, and map them with TagPost, TagUser model.
When User search Tag in input search, I using $http.post (VUE) and send array tag selected to Server.
Example tags print in server:
array:2 [
0 => array:10 [
"id" => 197
"name" => "nisi"
"slugify" => "nisi"
"thumbnail" => "http://lorempixel.com/400/400/?85397"
"seo_title" => null
"seo_description" => null
"seo_keyword" => null
"cover" => null
"created_at" => "2017-06-13 07:19:30"
"updated_at" => "2017-06-04 06:01:14"
]
1 => array:10 [
"id" => 184
"name" => "dolores"
"slugify" => "dolores"
"thumbnail" => "http://lorempixel.com/400/400/?45793"
"seo_title" => null
"seo_description" => null
"seo_keyword" => null
"cover" => null
"created_at" => "2017-06-08 15:37:59"
"updated_at" => "2017-05-27 13:34:02"
]
]
I want when user search tags then view update users and posts have tags user search.
View here:
In server (laravel) how to code with relationship model using least query?

Assuming that you have correctly set your relationship this should work
Tag::with('posts','users')->whereIn('id', $your_ids_send_by_vue)->get();
And then in your controller do the logic to get your desired data

Related

Laravel: Redirect with post route

Been trying to find a way to call to pass a multidimensional array to a Post route with no success.
The array looks like this:
"order" => array:16 [
"id" => "1"
"total" => "4825"
"neighborhood" => "Barrio Bravo"
]
"products" => array:2 [
4 => array:4 [
"id" => "4"
"name" => "Maestro Dobel 750ml"
"price" => "530"
"quantity" => "1"
]
1 => array:4 [
"id" => "1"
"name" => "Don Julio 70 700ml"
"price" => "650"
"quantity" => "1"
]
]
"grandTotal" => "1180"
"balanceToPay" => "354"
"cartTotal" => "826"
I don't have any problem asserting the route in the unit test calling the route like so:
$this->post(route('order.success', $orderInfo));
But when it comes to the controller I can't find the way to redirect to order.success with its orderInfo array.
This won't work since redirect only works with GET:
return redirect(route('order.success', $orderInfo));
Ideas?
It's not going to work with a simple redirection because you cannot choose the HTTP method. It's always GET when you make a redirection. It works in your test because you make a POST request manually.
I can see 2 solutions:
You can send data using GET method (they are added as URL parameters). If they are not confidential it can be a solution.
If you don't want to send those data in the URL, you have to save them somewhere and get them from the storage when you're on the order.success page. You can save them, for instance, in the session storage or in the local storage of your browser.
Also, your test isn't good if it tests a behavior that does not happen in your app (post request instead of redirection).

Is there a way to push multiple inputs into the same Db column in laravel?

I have tried searching for something similar to the question above but no luck.
I have a one to many relationship between a series and videos table. Inside the videos table I have title and url to the video.
The issue i am having is that, i am using jquery to add multiple rows containing title and url.
I tried pushing the title and url to the videos table on the database. There's no problem when pushing one input but when i have many title and url, the code breaks.
I have also tried using createMany but it can only add an array of one input. I can't get it to add both title and url.
$data = \request()->validate([
'title' => 'required',
'vimeo_id' => 'required',
'episode_number' => 'required'
]);
$pricourse->videos()->createMany($data);
After i dd the data in the browser i got the following
array:4 [▼
"_token" => "82k5RnVcdzSkPoOnDYeYipuRqAcJqhXzcWAx3RwW"
"title" => array:2 [▼
0 => "Angles of Elevation"
1 => "English 2"
]
"vimeo_id" => array:2 [▼
0 => "12343"
1 => "12343"
]
"episode_number" => array:2 [▼
0 => "1"
1 => "2"
]
]
Any help will be appreciated..
Thanks
createMany() is the way to go as far as I can understand. You are probably doing it wrong.
Here is an example :
$videosToCreate = [
[
'title' => 'title number 1',
'url' => 'some url number 1'
],
[
'title' => 'title number 2',
'url' => 'some url number 2'
],
[
'title' => 'title etc...',
'url' => 'some url etc...'
]
];
$this->videos()->createMany($videosToCreate);
We cannot help you if you do not share any code though.
I discovered i was not passing the data from the hmtl page properly. I made a few changes to it.
<input type="text" name="videos[0][title]">
<input type="text" name="videos[0][vimeo_id]">
<input type="text" name="videos[0][episode_number]">
<input type="text" name="videos[1][title]">
<input type="text" name="videos[1][vimeo_id]">
<input type="text" name="videos[1][episode_number]">
My controller code goes below
$data = \request()->validate([
'videos.*.title' => 'required',
'videos.*.vimeo_id' => 'required',
'videos.*.episode_number' => 'required'
]);
$pricourse->videos()->createMany($data['videos']);
All i had to do was pass $data['videos'] inside createMany
Thank you all for your help. It served as a pointer

Fill custom CRM fields in Bitrix via REST API

I have the need to fill custom fields in Bitrix24 CRM via REST API.
When it comes to working on standard fields (like TITLE, TEL, etc) it works, but if I try to valorize a custom field by putting it's property name in the request the endpoint simply ignores it.
Here you have a snippet:
$company = CRest::call(
'crm.company.add',
[
'fields' => [
"TITLE" => "Company 1"
, "COMPANY_TYPE" => "Customer"
, "CURRENCY_ID" => 'EUR'
, "REVENUE" => "123000"
, "ADDRESS" => "st. some address"
, "ADDRESS_CITY" => "some city"
, "ADDRESS_POSTAL_CODE" => "12345"
, "ADDRESS_PROVINCE" => "some province"
, "PHONE" => [
["VALUE" => "1230000000", "VALUE_TYPE" => "WORK"]
,["VALUE" => "1234000000", "VALUE_TYPE" => "FAX"]
]
, "EMAIL" => [ ["VALUE" => "me#company1.com", "VALUE_TYPE" => "WORK"] ]
, "INDUSTRY" => "MANUFACTURING"
, "CUSTOM_FIELD" => "Hey I'm not a standard field and I'm going to be ignored"
]
]
);
Did anyone have the same need? How can I solve it?
I found the solution.
Simply write the Field ID instead of the Field Name. The Field ID looks like "UF_CRM_5AERER454DD4". By putting this as key and the value after " => " the property gets valorized.

Firebase PHP Data Submission - arrayValue, mapValue

I am trying to push data to a firestore DB using PHP and the Google apis.
Inside the documentation and examples I have seen around the web, I am able to use mapValue and arrayValue when sending data.
The example I am using is as follows:-
[
"orderName" => [
"stringValue" => "Gbeila Aliu Wahab"
],
"orderLocationName" => [
"stringValue" => "Accra Mall Limited"
],
"orderTotalAmount" => [
"doubleValue" => 150.5
],
"orderDescription" => [
"stringValue" => "Lorem Ipsum is simply dummy text of the printing and typesetting industry"
],
"orderLocationGeoPoints" => [
"geoPointValue" => (object) [
"latitude" => 5.5557,
"longitude" => -0.1963
]
],
"orderStatus" => [
"stringValue" => "NotAssigned"
],
]
This works perfectly fine, but when I attempt to send an object or an array I get the following error returned to me:-
"message": "Invalid JSON payload received. Unknown name \"map_value\" at 'document.fields[0].value': Proto field is not repeating, cannot start list.",
when attempting to map the value using the following code:-
"orderName" => [
"mapValue" => ["Gbeila Aliu Wahab", 123]
]
// or
"orderName" => [
"arrayValue" => [
"first" => [
"stringValue" => "test"
],
"second" => [
"stringValue" => "test123"
]
]
]
I have tried many variations to try to get this to work.
How am I supposed to be using the mapValue and arrayValue I can see a lot of mentions regarding the value option but I cannot see any examples on how to use the.
Any help would be greatly appreciated.
Payload to your array or map you're generating is incorrect as per the documentation. You need to wrap your actual data (to store) under values key, your final array should be:
["orderName" => ["arrayValue" => ["values" => [["stringValue" => "test"], ["stringValue" => "test123"]]]]]
Similarly your mapValue should be
["orderName" => ["mapValue" => ["fields" => ["field1" => ["stringValue" => "Gbeila Aliu Wahab"]]]]]
Also, you can play with other data mapper via this package.

Which Image value need to add to Source field to upload FB photo

I'm trying to upload a photo to facebook via my laravel application. I have some questions.
I uploaded photo and the got image properties with dd($_FILES) and the result is
array:1 [
"file" => array:5 [
"name" => "shailene_woodley_divergent-wide.jpg"
"type" => "image/jpeg"
"tmp_name" => "/private/var/folders/b0/l7j5d8tj2ynflwt8srr5lywm0000gn/T/phpxHoc6s"
"error" => 0
"size" => 573957
]
]
and my controller is
try {
$response = $this->fb->post('/me/photos',
array(
'source' => ?????????,
'caption' => 'nothing',
'published' => 'false'
)
);
Please let me know which value of photo do i need to add in source field.
thanks

Categories