Function to get all the required data:
$auditResults = Audit::where('audit_id', $id)
->with('questionDetail')
->with('questionDetail.auditQuestion')
->get();
Which returns (streamlined):
Audit {#427 ▼
#relations: array:1 [▼
"questionDetail" => AuditQuestionDetail {#449 ▼
#relations: array:1 [▼
"auditQuestion" => AuditQuestion {#471 ▼
#original: array:5 [▶]
}
]
}
]
}
How can I loop within the view, to reach the auditQuestion relationship, for each Audit?
I have tried:
#foreach($auditResults->questionDetail->auditQuestion as $answer)
But I get:
Undefined property:
Illuminate\Database\Eloquent\Collection::$questionDetail
Many thanks.
** Issue with first relationship: **
Collection {#470 ▼
#items: array:18 [▼
0 => Audit {#427 ▼
#fillable: array:4 [▶]
#attributes: array:7 [▶]
#original: array:7 [▶]
#relations: array:1 [▼
"questionDetail" => AuditQuestionDetail {#449 ▼
#table: "audit_questions_details"
#fillable: array:3 [▶]
#attributes: array:7 [▶]
#original: array:7 [▼
"id" => 2
"audit_question_id" => 2
"question_number" => 1
"comment" => 1
"header" => 0
"created_at" => "2017-03-26 13:40:18"
"updated_at" => "2017-03-26 13:40:18"
]
In Laravel 5.4, you can use higher order messages to do this cleanly:
#foreach($auditResults->map->questionDetail->map->auditQuestion as $answer)
Use nested loops:
#foreach ($auditResults as $result)
#foreach ($result->questionDetail as $detail)
#foreach ($detail->auditQuestion as $question)
{{ $question->id }}
#endforeach
#endforeach
#endforeach
Related
getting data from database by this code:
$allTickets = Ticket::with(['user','replies','supporters'])->whereUserId(auth()->user()->id)->get();
return this result for me which that have 2 index in array and all of this items i have supporters relationship which both are same, now my question is how can i use unique in collections
Illuminate\Database\Eloquent\Collection {#1550 ▼
#items: array:2 [▼
0 => Modules\Ticket\Entities\Ticket {#1548 ▼
#guarded: array:1 [▶]
#connection: "mysql"
#table: "tickets"
...
#relations: array:3 [▼
"user" => App\User {#1575 ▶}
"replies" => Illuminate\Database\Eloquent\Collection {#1565 ▶}
"supporters" => Illuminate\Database\Eloquent\Collection {#1572 ▼
#items: array:1 [▼
0 => App\User {#1585 ▼
...
#attributes: array:19 [▼
"id" => 1
"user_id" => null
...
"api_token" => "a"
"remember_token" => null
"deleted_at" => null
"created_at" => "2020-06-14 13:56:45"
"updated_at" => "2020-06-14 13:56:45"
]
...
}
]
}
]
...
}
1 => Modules\Ticket\Entities\Ticket {#1567 ▼
#guarded: array:1 [▶]
#connection: "mysql"
#table: "tickets"
...
#relations: array:3 [▼
"user" => App\User {#1575 ▶}
"replies" => Illuminate\Database\Eloquent\Collection {#1551 ▶}
"supporters" => Illuminate\Database\Eloquent\Collection {#1559 ▼
#items: array:1 [▼
0 => App\User {#1585 ▼
...
#attributes: array:19 [▼
"id" => 1
"user_id" => null
...
"api_token" => "a"
"remember_token" => null
"deleted_at" => null
"created_at" => "2020-06-14 13:56:45"
"updated_at" => "2020-06-14 13:56:45"
]
...
}
]
}
]
...
}
]
}
my tested code:
#foreach($allTickets as $supporter)
#php
$unique = $supporter->supporters->unique();
#endphp
#foreach($unique->values()->all() as $user)
...
...
...
#endforeach
#endforeach
Do not try to do unique on the collection, try to get your data already filtered from your database. You could group by any of your relationships attributes, just pass in a closure to handle the query for the eager-loaded content, something like:
$allTickets = Ticket::with(['user','replies'])->with([
'supporters' => function($query) {
$query->groupBy('id');
}
])->whereUserId(auth()->user()->id)->get();
Or you can query like this:
$allTickets = collect(Ticket::with(['user','replies','supporters'])->whereUserId(auth()->user()->id)->get());
$allUniqueTickets = $allTickets->unique();
$tickets = $allUniqueTickets->values()->all();
That's how you can easily get all the unique values from your controller. I hope this answers your question.
For more information, you can check https://laravel.com/docs/7.x/collections#method-unique
LengthAwarePaginator {#997 ▼
#total: 575
#lastPage: 58
#items: Collection {#1007 ▼
#items: array:10 [▼
0 => WorkableInterviews {#1008 ▶}
1 => WorkableInterviews {#1009 ▶}
2 => WorkableInterviews {#1010 ▶}
]
}
#perPage: 10
#currentPage: 1
#path: "https://localhost/employer/interviews/MQ=="
#query: array:1 [▼
"subscription" => array:1 [▼
0 => "prepaidxxxx"
]
]
#fragment: null
#pageName: "page"
}
my object is like this, I want to print this section query: array:1 subscription, How can do this
I have the following models and their relationships:
Product - (name, price, qty)
Product Attribute (qty, price - when there is a combination, the qty and price here is the one getting)
Attribute - (size, color)
Attribute Values - (small, medium, large, red, blue, green etc)
// One to Many Relationship
$product->productAttributes (returns a collection)
// Many to Many Relationship
$productAttribute->attributeValues (returns a collection)
// One to Many Relationship
$attribute->attributeValues
I need to format it in such a way like:
array:2 [
"color" => array:2 [
0 => "red"
1 => "blue"
]
"size" => array:3 [
0 => "small"
1 => "medium"
2 => "large"
]
]
This is what I get when I iterate on the collections:
$attributes = $productAttributes->map(function (ProductAttribute $pa){
return $pa->attributesValues->map(function (AttributeValue $av) {
return [$av->attribute->name => $av->value];
});
})->all();
it obviously returns:
array:6 [▼
0 => Collection {#508 ▼
#items: array:2 [▼
0 => array:1 [▼
"Color" => "red"
]
1 => array:1 [▼
"Size" => "small"
]
]
}
1 => Collection {#525 ▼
#items: array:2 [▼
0 => array:1 [▼
"Color" => "red"
]
1 => array:1 [▼
"Size" => "medium"
]
]
}
2 => Collection {#535 ▼
#items: array:2 [▼
0 => array:1 [▼
"Color" => "red"
]
1 => array:1 [▼
"Size" => "large"
]
]
}
3 => Collection {#545 ▼
#items: array:2 [▼
0 => array:1 [▼
"Color" => "red"
]
1 => array:1 [▼
"Size" => "small"
]
]
}
]
Again, my intent is to format it like this:
array:2 [
"color" => array:2 [
0 => "red"
1 => "blue"
]
"size" => array:3 [
0 => "small"
1 => "medium"
2 => "large"
]
]
TIA!
Edit:
$product->productAttributes
Collection {#513 ▼
#items: array:6 [▼
0 => ProductAttribute {#514 ▼
#fillable: array:2 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:6 [▶]
#original: array:6 [▼
"id" => 7
"quantity" => 3
"price" => "100.00"
"product_id" => 25
"created_at" => "2018-03-11 16:56:36"
"updated_at" => "2018-03-11 16:56:36"
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
1 => ProductAttribute {#515 ▶}
2 => ProductAttribute {#516 ▶}
3 => ProductAttribute {#517 ▶}
4 => ProductAttribute {#518 ▶}
5 => ProductAttribute {#519 ▶}
]
}
$productAttribute->attributeValues
array:6 [▼
0 => Collection {#507 ▼
#items: array:2 [▼
0 => AttributeValue {#521 ▼
#fillable: array:1 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:5 [▶]
#original: array:7 [▼
"id" => 1
"value" => "red"
"attribute_id" => 1
"created_at" => "2018-03-11 16:49:15"
"updated_at" => "2018-03-11 16:49:15"
"pivot_product_attribute_id" => 7
"pivot_attribute_value_id" => 1
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▶]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
1 => AttributeValue {#506 ▼
#fillable: array:1 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:5 [▶]
#original: array:7 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▶]
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
]
}
1 => Collection {#523 ▶}
2 => Collection {#530 ▶}
3 => Collection {#537 ▶}
4 => Collection {#544 ▶}
5 => Collection {#547 ▶}
]
I think, what you're looking for is something like the following;
$attributes = $productAttributes->pluck('attributesValue')
->flatten()
->unique()
->groupBy(function (AttributeValue $av) {
return $av->attribute->name;
})
->each(function (Collection $avs) {
$avs->map(function (AttributeValue $av) {
return $av->value;
});
});
Here's what the above code does;
Pull the relationship attributesValue for all results.
Flatten it out so we have one collection containing all attributesValue
Make sure we don't have any duplicates
We group everything so that we have something like colour => (collection of colour attributesValue)
Now we cycle through our multi level collection, and map the instances of AttributeValue so that each is replaced with their value
I've not ran this code myself, but I believe this will work.
In my Laravel project I have the following problem. (I have reduced the output a little for easier readability)
I fetch all the posts from a table named Newsposts. I store this as a collection called $all_posts, which looks like this:
Collection {#216 ▼
#items: array:7 [▼
0 => NewsPost {#227 ▶}
1 => NewsPost {#228 ▶}
2 => NewsPost {#229 ▼
#attributes: array:6 [▼
"id" => 6
"title" => "Sample title"
"body" => "<p>Some html</p>"
"user_id" => 15
]
#original: array:6 [▶]
#casts: []
.
.
#guarded: array:1 [▶]
}
3 => NewsPost {#230 ▶}
4 => NewsPost {#231 ▶}
]
}
I then fetch all entries from another table called user_read witch contains all the posts the current user have read. This table contains a reference to the user with a 'user_id'-column, and a reference to the newsposts with a 'post_id'-column. I also store this in a collection, which looks like this:
Collection {#219 ▼
#items: array:2 [▼
0 => UserRead {#210 ▼
#attributes: array:4 [▼
"user_id" => 15
"post_id" => 6
]
#original: array:4 [▶]
#casts: []
.
.
#guarded: array:1 [▶]
}
1 => UserRead {#217 ▶}
]
}
So what I want to do now is take the $all_posts-collection and remove all the posts from $read_posts-collection, and store it in a new collection. Lets call it $unread_posts. How can i achieve this?
get all readed post id first, then use filter function to filter unread posts
$readedPosts = $user_read->pluck('post_id');
$unread_posts = $all_posts->filter(function($value) use ($readedPosts) {
return !in_array($value->id, $readedPosts);
})
Entity {#848 ▼
#attributes: array:3 [▼
0 => array:4 [▶]
1 => array:4 [▶]
2 => array:4 [▶]
]
}
is_array($dump) // False
is_array($dump[0]) // True
Why the first result is false?
How can i handle this as an array?