I do an ajax call and I have this code in the controller:
$menu = view('partials.nav_reserved_area', $data)->render();
return response()->json(['reserved_menu' => $menu]);
If I print $data variable before the render, this is what I get:
array:12 [▼
"request_url" => "https://www.example.com"
"theatre_events" => Collection {#515 ▼
#items: []
}
"music_events" => Collection {#498 ▼
#items: []
}
"others_events" => []
]
As You can see "theatre_events" and "music_events" are empty collection.
I do not why, but in the view, "theatre_events" and "music_events" have some items, as if it gets data from another ajax call or from another part of code.
How is it possible?
UPDATED
This should be the code incriminated (the other part of the code where maybe view gets data):
Model::whereHas('event', function ($query) {
$query
->where('end', '>=', \Carbon\Carbon::today())
->where('user_id', Auth::id())
->where('event_type', 'theatre');
})->paginate(20);
Related
I have a collection grouped (multiple times) like this:
Illuminate\Support\Collection {#1883 ▼
#items: array:1 [▼
57082 => Illuminate\Support\Collection {#1885 ▼
#items: array:1 [▼
"07-2021" => Illuminate\Database\Eloquent\Collection {#1863 ▼
#items: array:1 [▼
343 => Illuminate\Database\Eloquent\Collection {#1864 ▼
#items: array:1 [▶]
}
]
}
]
}
]
}
I have another collection that could have same values or not, and I need to merge these 2 collections with merged elements.
I tried with this, without success (I lose first "group"):
$fstCollection = $fstCollection->mapWithKeys(function ($items, $key) use ($sndCollection) {
return $sndCollection->get($key) ? $items->merge($sndCollection->get($key)) : $items;
});
I'm expecting your are loosing the key, by using mapWithKeys() wrong, in general that method is used, by returning an associative array, representing the key to map to and the value.
So changing the closure logic to the following, will keep the first group.
$collection = $sndCollection->get($key) ? $items->merge($sndCollection->get($key)) : $items;
return [$key => $collection];
Alternatively, just using map() will solve your problem, map() preserves the keys.
$fstCollection = $fstCollection->map(function ($items, $key) use ($sndCollection) {
return $sndCollection->get($key) ? $items->merge($sndCollection->get($key)) : $items;
});
I have datatable with a categories table.
I have two categories, Category A and Category B.
I also have a post table with post assigned to a category.
Now I can get all post and category
$posts = PostGroup::whereIn('name', ['A', 'B'])
PostGroup file has
public function posts() {
return $this->hasMany('App\Dict');
}
Ok now when I call code above and I get the following:
Collection {#301 ▼
#items: array:2 [▼
0 => PostGroup {#289 ▶}
1 => PostGroup {#302 ▶}
]
}
I can do foreach() or I can get posts
$posts = PostGroup::whereIn('name', ['A', 'B'])
$posts[0]->posts->where('group_id', 'A');
How can I avoid foreach and search the $posts collection to find group A something like:
$posts->where('group_id', 'A')->posts;
I'm trying to get if user following or not the page. I'm trying like this:
public function get_following()
{
return $this->belongsTo(Followers::class, 'user_id')->where('type', '=', 1)->where('follower_id', '=', auth()->user()->id);
}
But this return null and I follow this page (got the item on table)...
dd($page)
Page {#305 ▼
#relations: array:3 [▼
"ptag" => Tag {#309 ▶}
"get_following" => null
"links" => Collection {#318 ▶}
]
DB:
followers.user_id = (pages.id), follower_id = (auth user), type = (1)
I'm trying to get the comments of the links with laravel relationship, the comments of links return normally, but I don't know how to get it in view or dd(), how can I do it?
Controller:
$page = Page::where('friendly_url', $id)
->select('id', 'photo', 'friendly_url', 'name', 'description', 'followers', 'links', 'tag_id', 'links_clicks')
->with('ptag', 'links', 'links.comments', 'links.tag')
->with(['links.comments.user' => function($query) {
$query->select('id', 'name', 'lastname');
}])->with(['links.comments.userProfile' => function($query) {
$query->select('id', 'photo');
}])->first();
dd($page->getRelation('links')
Collection {#301 ▼
#items: array:2 [▼
0 => Link {#307 ▼
#relations: array:2 [▼
"comments" => Collection {#316 ▼
#items: array:1 [▶]
"tag" => Tag {#322 ▶}
]
dd($page->getRelation('links')->getRelation('comments'))
BadMethodCallException
Method getRelation does not exist.
dd($page->getRelation('links')->comments)
Exception
Property [comments] does not exist on this collection instance.
EDIT:
I want to show the attributes of comment and comment.user, how I can do it?
foreach($page->getRelation('links')->pluck('comments') as $comment) {
dd($comment);
}
Collection {#327 ▼
#items: array:1 [▼
0 => Comment {#325 ▼
#attributes: array:5 [▼
"id" => 3
"content" => "teste"
"link_id" => 1
"user_id" => 1
"created_at" => "2018-01-11 00:47:32"
]
#relations: array:2 [▼
"user" => User {#330 ▼
#attributes: array:3 [▼
"id" => 1
"name" => "Halysson"
"lastname" => "Teves dos Santos"
]
}
"userProfile" => UserProfile {#326 ▶}
]
Try this-
foreach($page->links as $link){
$comments = $link->comments;
}
You should define relations first on relevent models and then using 'with' function can load models with relations OR a relation can be called directly by single model see documentation for further details
eg: you have defined a relation for user to phone, as user has a phone, one to one relation. in user model define a relation like
public function phone()
{
return $this->hasOne('App\Phone');
}
now to call this relation on user model instance.
$user = User::find(1);
dd($user->phone);
Because you're using the select() method, you also need to include all the foreign keys in it to allow Eloquent to be able to load these relationships.
But since you just start using Laravel, I'd recommend you to just remove select() method from this complex query.
If you want the links of a page :
$page->links
if you want all the comments of a page :
$page->links->pluck('comments')
that's it :)
I get a Collection from a Query in Laravel.
Actually, I'm processing collection like that:
foreach ($cts as $ct) {
$array[$ct->id] = $ct->category->name;
}
It outputs this:
array:2 [▼
1158 => "Junior por Equipo"
1160 => "Varonil por Equipo"
]
Now, I would like to user map() function over muy collection as I like it better:
MyModel::get()
->get()
->map(function ($item, $value){
return [ $item->id => $item->category->name ];
})->toArray();
But now, I get :
array:2 [▼
0 => array:1 [▼
1158 => "Junior por Equipo"
]
1 => array:1 [▼
1160 => "Varonil por Equipo"
]
]
How should I do to output it like the first case?
You don't have to map the collection, just use MyModel::lists('name', 'id' ) ;
There is a collection method for that. pluck() and eloquent lists()
no its preferred to use pluck for getting some columns only. previously lists() was used.
see https://laravel.com/docs/5.3/collections#method-pluck
as the document says
The pluck method retrieves all of the values for a given key/keys only
so it would be something like this.
Model::get()->pluck('name','id);
Than you can do toArray() on the result if you want to get an array instead of a collection.