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)
Related
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);
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 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;
I want my $row->responses object to look like so:
"responses": [
{
"id": 1381822,
"user_id": 45313,
"respondent_name": "JP Pullen",
"response_time": 1424127673,
"is_root_response": true,
"response_tree_id": 1377018,
"project_users_id": 74311,
"display_name": "Mobile 2",
"lft": 1,
"rgt": 2,
"response_read": true,
"body": "Sure it really is was great I purchased choclate at my local grocery store.",
"answers": [ ],
"avatar_url": null,
"mtime": null,
"media_list": [ ]
},
Yet my $row->responses object looks like:
"responses": {
"1": {
"id": 1381825,
"user_id": 45167,
"respondent_name": "First Name Last Name",
"response_time": 1424128177,
"is_root_response": true,
"response_tree_id": 1377021,
"project_users_id": 74312,
"display_name": "SimonSays",
"lft": 3,
"rgt": 4,
"response_read": false,
"body": "What's up!",
"answers": [ ],
"avatar_url": "https:\/\/portalvhds5kcv8nfhdz8zn.blob.core.windows.net\/user-45167\/avatar-50x50",
"mtime": 1420206441,
"media_list": [ ]
}
}
How do you get rid of the stupid {1} in the object. I didn't get it until I had to loop through the objects and unset one of them. I am not a PHP expert. Strongly Typed FTW lol.
foreach($row->responses as $elementKey => $element)
^ That is how I looped through the responses object to unset one:
unset($row->responses{$elementKey});
When arrays are encoded using json_encode() PHP will determine whether it should use array syntax (square brackets) or object syntax (curly brackets). In this case, the first array index is 1 instead of 0 so it can't reliably be encoded as a JavaScript array.
To prevent this from happening, you can use array_values() to renumber the array to be 0-based again:
$row->responses = array_values($row->responses);