I have in the MySql table column that called "more_data" and its contain json array:
[{
"notes": null,
"status": 0,
"type_id": 1,
"start_id": 1001
},
{
"notes": null,
"status": 1,
"type_id": 2,
},
{
"notes": null,
"status": 1,
"type_id": 3,
},
{
"notes": null,
"status": 1,
"type_id": 4,
}]
I need to get only this JSON from the query but just where status is 1.
So I don't want to get the first element.
There is way to do that?
Thanks.
If the "more_data" column is a type of JSON, You can get the values by whereJsonContains() function. https://laravel.com/docs/5.8/queries#json-where-clauses
Otherwise, you have to decode the JSON and filter the array.
Try this
$a=YourModel::find($id);
$b=$a->more_data;
$data=[];
foreach($b as $c)
{
if(($c['status'])==1)
{
$data[]=$c;
}
}
dd($data);
Related
This is Array I want to display questions with foreche inside blade
{
"data": [
{
"time": 20,
"questions": [
{
"id": 1,
"type_id": 2,
"difficulty_id": 1,
"category_id": 1,
"second_category_id": 1,
"third_category_id": 0,
"question": "{\"ru\":\"sadsdasdaasd\"}",
"a": "{\"ru\":null}",
"b": "{\"ru\":null}",
"c": "{\"ru\":null}",
"d": "{\"ru\":null}",
"answer_written": null,
"answer": null,
LIke this
#foreach($questions->questions as $link)
#endforeach
But I can not display there some errors, How I can display give advise???
You have to use like below
#foreach(json_decode($questions['data']['questions']) as $link)
Assuming you have decoded the JSON to a PHP data structure, the first part of the JSON String denotes an object owning an object owning an array, so
foreach($questions->data[0]->questions as $link)
I have a project in laravel which has API. I'm asking the API for posts(I call them recommendation).
Now my api response look like this -
{
"success": true,
"data": {
"current_page": 1,
"data": [
{
"id": 3,
"course_id": "20",
"title": "Dormouse followed.",
"description": "Alice aloud, addres
"file": "https://example.com/storage/images/2019/10/01/phTJ.png",
"created_at": null,
"updated_at": "2019-10-01 14:21:46",
"recommendation_likes": 0,
"is_bookmarked": "true",
"is_liked": "false",
"likes_count": []
}
...
...
...
All is good but I don't wanna likes_count to be in the result. It is a relation method. I get from it all I need. It is if the user liked this post. And it is is_liked in the result . but likes_count automatically added to the response .
if($item->likesCount->contains($user->id)){
$item['is_liked']='true';
}
I tried delete it with
foreach ($recommendations as $item) {
unset($item['likes_count']);
}
But it doesn't do it.
I think the problem is how you are referencing the object data structure. You are attempting to unset() something that doesn't exist so no error is thrown but the likes_count isn't getting removed either.
Here's your code example (fixed and modified for demonstration):
<?php
$apiResult = <<<eod
{
"success": true,
"data": {
"current_page": 1,
"data": [
{
"id": 3,
"course_id": "20",
"title": "Dormouse followed.",
"description": "Alice aloud, addres",
"file": "https://example.com/storage/images/2019/10/01/phTJ.png",
"created_at": null,
"updated_at": "2019-10-01 14:21:46",
"recommendation_likes": 0,
"is_bookmarked": "true",
"is_liked": "false",
"likes_count": []
},
{
"id": 4,
"course_id": "20",
"title": "Dormouse followed.",
"description": "Alice aloud, addres",
"file": "https://example.com/storage/images/2019/10/01/phTJ.png",
"created_at": null,
"updated_at": "2019-10-01 14:21:46",
"recommendation_likes": 0,
"is_bookmarked": "true",
"is_liked": "false",
"likes_count": []
}
]
}
}
eod;
$result = json_decode($apiResult);
$data = $result->data->data;
var_dump($data);
// This should remove the likes_count array from the $result structure.
foreach ($data as &$item) {
unset($item->likes_count);
}
var_dump($data);
A quick way would be to make that 'attribute' (relation) hidden:
$recommendations->makeHidden('likes_count');
Though I am not sure how you are building your response.
I get data from database using this query
$cats = Category::with(['cat_trans' => function($q) use($lang){
$q->where('lang_code', $lang);
}])->with(['cat_prod' => function($query) use ($lang,$currency){
$query->with(['pro_trans' => function ($q) use ($lang){
$q->where('lang_code', $lang);
}]);
////////////
$query->with(['pro_price' => function ($q) use ($currency){
$q->with('currency_code')->where('cur_code', $currency);
}]);
///////////
}])->whereHas('account_type', function($qr) use ($account_type){
$qr->where('account_type_id', $account_type);
})->get();
I'm trying to remove the empty objects from the result, when I tried this I got the following response
{
"1": {
"id": 1,
"parent_id": null,
"order": 1,
"name": "Moblie",
"slug": "mobile-1",
"created_at": "2018-07-08 09:41:08",
"updated_at": "2018-07-08 10:30:17",
"cat_trans": [
{
"id": 1,
"category_id": 1,
"field": "title",
"value": "Mobile",
"lang_code": "en",
"created_at": "2018-07-08 09:51:59",
"updated_at": "2018-07-08 09:51:59"
},
{
"id": 2,
"category_id": 1,
"field": "desc",
"value": "smart",
"lang_code": "en",
"created_at": "2018-07-08 09:52:41",
"updated_at": "2018-07-08 09:52:41"
},
{
"id": 12,
"category_id": 1,
"field": "slug",
"value": "mobile-1",
"lang_code": "en",
"created_at": null,
"updated_at": null
}
],
}
}
I want to remove the key "1" from all the responses.
I used unset to get this value using this code
foreach ($cats as $k) {
if (count($k->cat_prod) == 0) {
unset($cats[$va]);
}
$va++;
}
Then I tried using array_values but it displays that I can just use array_values on an array not an object.
You can remove key "1" by simply doing
array_values((array)$cats)
You were receiving error for this function like "Can Only be used on array" was due to Laravel Query returns object , so you just need to do type conversion on it.
$cats->toArray() if u need either to convert object to array or convert with some specific conditions
Your output looks like JSON so you can do this to eliminate the 1:
json_encode( $cats->{1} );
You can use laravel simple method mapWithKeys()
$cats = $cats->mapWithKeys(function ($item) {
return $item > 2;
});
$keyed->all();
I have a single array of data, I want to add a key and it's value in same array . Here in addedPost I want to add key favouritePost and it's value is $favouritePost after product key. How can i do this ?
Here is my query:
$addedPost = Post::with(['product','postattribute.attribute.category','user.userDetails'])
->whereId($postData['post_id'])
->first();
$favouritePost = PostFavourite::isAlreadyAdded($postData['post_id'], Auth::id());
return [
'status_code' => $status_code,
'message' => $message,
'PostDetails' => $addedPost
];
What I get in response :
{
"PostDetails": {
"id": 289,
"user_id": 12,
"product_id": 2,
"demand_or_supply": "Demand",
"description": "edited1",
"status": "Expired",
"created_at": "2018-06-22 07:35:27",
"updated_at": "2018-07-05 06:42:56",
"product": {
"id": 2,
"title": "Diamond",
"icon": null,
"status": "Active"
}
}
}
EXPECTED RESULT:
{
"PostDetails": {
"id": 289,
"user_id": 12,
"product_id": 2,
"demand_or_supply": "Demand",
"description": "edited1",
"status": "Expired",
"created_at": "2018-06-22 07:35:27",
"updated_at": "2018-07-05 06:42:56",
"product": {
"id": 2,
"title": "Diamond",
"icon": null,
"status": "Active"
},
"favouritepost": {
"id": 8,
"post_id": 289,
"user_id": 12
}
}
}
First: Your $addedPost is not an array but a Eloquent Collection. There are multiple possibilites to do this. The easiest one is to union an Array with the Collection.
$union = $addedPost->union($favouritePost->toArray());
For every other solution please take a look at the Laravel Documentation. It's pretty easy to understand.
https://laravel.com/docs/5.6/collections
Edit: Though I missed the ->first() inside the question just use the solution already mentioned. ->first() returns a StdClass Object, so you can handle it like it:
$addedPost->favouritepost = $favouritePost;
That property favouritePost is added to $addedPost object in that case. There's no need for any method call again.
first you store all the values which you are returning in one associative array then use array_push() to add new key/value in that array.After all you just return that array.
Assuming that $addedPost is a laravel collection you can simply do:
$addedPost = Post::with(['product','postattribute.attribute.category','user.userDetails'])
->whereId($postData['post_id'])
->first();
$favouritePost = PostFavourite::isAlreadyAdded($postData['post_id'], Auth::id());
$addedPost->put('favouritepost', $favouritePost);
return [
'status_code' => $status_code,
'message' => $message,
'PostDetails' => $addedPost
];
for adding key value in object
$object->new_key="value";
I need to merge 2 objects using PHP (Laravel) one of them is in an array, the other is a plain object, but also contains a nested object, is there any simple way to do it?
Edit: The result should be a single object
[
{
"id": 1,
"release_date": "1998",
"license_plate": "098HBV",
"state_id": 1,
"type_id": 7,
"created_at": "2016-11-18 11:39:19",
"updated_at": "2016-11-18 11:39:19",
"deleted_at": null
}
]
and
{
"id": 1,
"parent_id": null,
"name": "Chrysler",
"state_id": 1,
"type_id": 1,
"created_at": "2016-11-18 11:39:10",
"updated_at": "2016-11-18 11:39:10",
"deleted_at": null,
"pivot": {
"vehicle_id": 1,
"brand_id": 1
}
}
Desired output should be:
{
"id": 1,
"release_date": "1998",
"license_plate": "098HBV",
"name": "Chrysler",
}
It has to be something similar, otherwise it would break a lot of JavaScript code connected to the first object.
<?php
//Merge the two as an array
$merged = array_merge($array,(array)$object);
//As an object
$merged = (object)$merged;