can't using foreach for collection Laravel 5.3 - php

I'm creating an edit page of a group-making of an organization. each group consists of a mentor and some mentees.
The problem is i can't iterate using the result because it is a collection of a collection and a user, instead of giving a (directly) collection of user.
the error is this
ErrorException in dd3280396f5fc9a234b8faac1b17b55bd5c30ae2.php line 77:
Undefined property: Illuminate\Database\Eloquent\Collection::$id (View: E:\Documents\Karya\simentor\resources\views\group\edit.blade.php)
Here's the result of dd($mentees)
Collection {#260 ▼
#items: array:2 [▼
0 => Collection {#286 ▼
#items: array:1 [▼
0 => User {#284 ▶}
]
}
1 => User {#261 ▶}
]
}
To get all mentors and mentees who don't have any group yet and to get the mentor and mentees of the to-be-edited group, i did this in GroupController.php
public function edit($id)
{
$group = Group::find($id);
//get mentor and mentee who don't have group yet
$unassignedMentors = User::whereHas(
'roles', function ($q) {
$q->where('name', 'mentor');
})
->doesntHave('groups')
->get();
$unassignedMentees = User::whereHas(
'roles', function ($q) {
$q->where('name', 'mentee');
})
->doesntHave('groups')
->get();
//get mentor and mentee of the to-be-edited group
$assignedMentor = $group->mentor();
$assignedMentee = $group->mentee();
//combine those mentors and mentees into a collection
$mentors = $unassignedMentors->prepend($assignedMentor);
$mentees = $unassignedMentees->prepend($assignedMentee);
return view('group.edit', compact('group', 'mentees', 'mentors', 'assignedMentor', 'assignedMentee'));
}
The mentor() and mentee() method defined in Group.php like this
public function mentor()
{
return $this->users()->whereHas(
'roles', function($q){
$q->where('name', 'mentor');
})
->first();
}
public function mentee()
{
return $this->users()->whereHas(
'roles', function($q){
$q->where('name', 'mentee');
})
->get();
}
Here's the code to display the checkboxes and names in edit.blade.php
#foreach ($mentees as $mentee)
<tr>
<td><input type="checkbox" name="mentee_id[]" value="{{$mentee->id}}" {{ array_has($assignedMentee, $mentee) ? 'checked' : '' }}></td> --> line 77
<td>{{ucwords($mentee->profile->name)}}</td>
</tr>
#endforeach

Related

How to display the last attribute order by 'created_at' in laravel view

I have liste.blade.phpa liste that contains all the candidats and their informations. For each candidat I need to display all the data of only his last candidature
Question :
How can I show all the columns of only the last candidature of each candidat ?
Candidat Model :
protected $fillable = [
'service_id',
'demande',
'nom',
'telephone',
'cin',...
];
public function candidatures()
{
return $this->hasMany(Candidature::class);
}
Candidature Model
protected $fillable = [
'candidat_id',
'doc',
'date_depot',
'etat',
'demande',
];
public function candidat()
{
return $this->belongsTo(Candidat::class, 'candidat_id');
}
Candidat controller :
public function index()
{
$candidat = Candidat::join('candidatures', 'candidatures.candidat_id', '=', 'candidats.id')
->whereNotIn('etat', ['Archivé', 'Refusé'])
->where('candidats.deleted','false')
->select('candidats.*)
->get();
return view('candidat.liste', compact('candidat'));
}
liste.blade.php:
#foreach($candidat as $key => $data)
<tr>
<th>$data->id</th>
...
#foreach($data->candidatures as $candidature)
<td>{{$candidature->demande }}</td>
<td>{{$candidature->date_depot}}</td>
<td>{{$candidature->etat}}</td>
#endforeach
#endforeach
Ps: I have to submit my project today
Thank you for your help .
Here You can use Order By clause and sort in dese to get last record
$candidat = Candidat::join('candidatures', 'candidatures.candidat_id', '=', 'candidats.id')
->whereNotIn('etat', ['Archivé', 'Refusé'])
->where('candidats.deleted','false')
->select('candidats.*)
->orderBy('candidatures.id','desc')
->limit(1)
->get();
Since you have relationships defined you can use
Candidat::with('candidatures') instead of Candidat::join('candidatures', 'candidatures.candidat_id', '=', 'candidats.id') or even ->candidatures like
$candidats = Candidat::all();
And in foreach ($candidats as $candidat) run $candidat->candidatures()->latest()->first()

Laravel paginate method not working with transform method

When I am using a transform method after calling paginate(20) it will return regular results, without paginating that is not supposed to be.
and when I remove transform() method paginate work as it supposes to be.
$users =
User::whereUser_role(1)
->join('user_types', 'users.user_type', '=', 'user_types.user_type_id')
->when(request('user-type'), function ($query) {
$query->where('user_types.name', '=', request('user-type'));
})
->addSelect(
'users.*',
'user_types.name as user_type',
)
->orderBy('users.created_at', 'desc')
->paginate(20)
->appends(request()->all())
->getCollection()
->transform(function ($user) {
$user->user_status = $user->status ? "Active" : "InActive";
$user->email_verified = $user->email_verified ? "Yes" : "No";
return $user;
});
$users->makeHidden(['user_role']);
return response()->json([
'message' => 'Success',
'status' => 200,
'requestLocation' => request()->path(),
'success' => true,
'data' => $users
], 200);
You can use getCollection method for the same. As per laravel API documentaion:
getCollection() : Get the paginator's underlying collection.
For e.g.
$users = User::whereStatus(1)
->paginate(20);
// iterates paginated items and applies a transformation
$users->getCollection()->transform(function ($user) {
$user->email_verified = $user->email_verified ? "Yes" : "No";
return $user;
})
Reference:Laravel API -> AbstractPaginator -> getCollection()

Laravel count issue

I am trying to search by check-boxes and conditions but i get this error in return:
count(): Parameter must be an array or an object that implements Countable
Logic
I can select 1 or more options
In back-end check what kind of options has selected
Depend of the kind return results
Code
controller
public function advancedsearch(Request $request) {
$options = Specification::whereHas('subspecifications')->with(['subspecifications' => function($query) {
$query->status('Active');
}])->get();
$brands = Brand::all();
$brandss = Input::has('brands') ? Input::get('brands') : [];
$suboption = Input::has('suboptions') ? (int)Input::get('suboptions') : [];
$min_price = Input::has('min_price') ? (int)Input::get('min_price') : null;
$max_price = Input::has('max_price') ? (int)Input::get('max_price') : null;
//codes
if(count($request['suboptions'])){
$products = DB::table('products')
->join('product_subspecification', function ($join) {
$suboption = Input::has('suboptions') ? Input::get('suboptions') : [];
$join->on('products.id', '=', 'product_subspecification.product_id')
->where('product_subspecification.subspecification_id', '=', $suboption);
})
->paginate(12);
}
elseif(count($request['brands'])){
$products = DB::table('products')
->whereIn('products.brand_id', $brandss)
->paginate(12);
}
elseif(count($request['min_price']) && count($request['max_price'])){
$products = DB::table('products')
->whereBetween('price', [$min_price, $max_price])
->paginate(12);
}
return view('front.advancesearch', compact('products', 'brands', 'options'));
}
Summary of code above
I am getting all brands from database and check if only 1 brand has selected or several
$brands = Brand::all();
$brandss = Input::has('brands') ? Input::get('brands') : [];
Then I check if any brand has selected in general or no by count and returning results
elseif(count($request['brands'])){
$products = DB::table('products')
->whereIn('products.brand_id', $brandss)
->paginate(12);
}
but I get error I share above.
Any idea?
Update
how my html look likes
how the result **dd** look like
array:4 [▼
"_token" => "Wqs9yzd5qwGtbv01asbCzsISeVQxHsCoWVQM1ifO"
"brands" => array:1 [▼
0 => "3"
]
"min_price" => null
"max_price" => null
]
Blade
<div class="checkbox">
#foreach($option->subspecifications as $suboption)
#if($option->title == 'Brand')
<label for="brands">
<input name="brands[]" type="checkbox" value="{{$suboption->id}}">
{{ucfirst($suboption->title)}}
</label>
#else
<label for="suboptions">
<input name="suboptions[]" type="checkbox" value="{{$suboption->id}}">
{{ucfirst($suboption->title)}}
</label>
#endif
#endforeach
</div>
Solved
Changed count with has solved the issue
elseif($request->has['brands']){
PS: Based on 3 different functions that I have, which returns data of my search I have mixed results but it doesn't related to this
question. count error solved so I'll mark it as solved.
Thanks to miken32

Getting data out of array column in laravel 5.5

I save my order data to notifications table as my notification using laravel Notification and I have problem with returning my data.
here is my notification method: App\Notifications\OrderSubmited.php
public function toArray($notifiable)
{
return [
'order_id' => $this->order->id,
'title' => $this->order->title,
'buyer' => $this->order->user_id,
'status' => $this->order->orderstatus_id,
];
}
My method to get data:
View::composer('admin.dashboard', function ($view) {
$notifications = DB::table('notifications')->orderby('id', 'desc')
->latest()
->take(5)
->get();
$notifs = $notifications;
$view->with('notifs', $notifs);
});
here is my blade code:
#foreach($notifs as $notif)
{{dd($notif)}}
#foreach($notif->data as $data)
{{$data['order_id']}} <br>
#endforeach
#endforeach
this is my {{dd($notif)}} output:
{#721 ▼
+"id": "46546547464415474884"
+"type": "App\Notifications\OrderSubmited"
+"notifiable_id": 2
+"notifiable_type": "App\Order"
+"data": "{"order_id":2,"title":"tsdfg", "user_id":"1", "orderstatus_id":"11"}"
+"read_at": null
+"created_at": "2018-01-18 00:00:00"
+"updated_at": "2018-01-18 00:00:00"
}
as you can see data i try to return are stored in data column.
If I remove my dd i will get this error:
Invalid argument supplied for foreach()
On this line:
#foreach($notif->data as $data)
Any idea?
Each notification is an object containing an array called data containing the values you return from your toArray method when creating the notification. You would normally iterate through your notifications and access their data like so:
#foreach ($notifications as $notification)
{{ $notification->data['order_id'] }}
#endforeach
Laravel converts the data property (which is stored in the database as JSON) into an array.
However because you are retrieving the notifications yourself from the database without making use of the built in methods you will need to convert that data to an array from JSON yourself, like so:
View::composer('admin.dashboard', function ($view) {
$notifications = DB::table('notifications')->orderby('id', 'desc')
->latest()
->take(5)
->get();
$view->with('notifications', $notifications);
});
Then from within your view:
#foreach ($notifications as $notification)
#php $data = json_decode($notification->data, true); #endphp
{{ $data['order_id'] }}
#endforeach
Regarding your follow up question, something like this:
View::composer('admin.dashboard', function ($view) {
$notifications = DB::table('notifications')->orderby('id', 'desc')
->latest()
->take(5)
->get()
->map(function ($item, $key) {
$item->data = json_decode($item->data);
$item->data->user = User::find($item->data->user_id);
return $item;
});
$view->with('notifications', $notifications);
});
Then from within your view:
#foreach ($notifications as $notification)
{{ $notification->data->user->name }}
#endforeach

UpdateExistingPivot method doesn't work

I'm trying to update a value in a pivot table. This is my method :
public function updateStatus(Event $event)
{
$this->authorize('updateStatus', $event);
$newStatus = Input::get('status');
$actualPivot = $event->guests()->where('user_id', Auth::id())->first()->pivot;
$id = $actualPivot['id'];
$status = $actualPivot['status'];
if ($newStatus != $status)
{
dd($event->guests()->updateExistingPivot($id, ['status' => $newStatus]));
}
return back();
}
I've checked with HeidiSQL, the row isn't updated how it should be. I've also tried this solution, but it doesn't update the row, it creates a new one. There is the dd() with this method:
array:3 [▼
"attached" => array:1 [▼
0 => 1
]
"detached" => []
"updated" => []
]
This is my guests() relation defined in the Event model:
public function guests()
{
return $this->belongsToMany('App\User')
->using('App\Invitation')
->withPivot('id', 'status')
->withTimestamps();
}
I don't know why the updateExistingPivot() method doesn't work. I hope you can help.
You must use the guest_id or whatever is the name for your foreign key of App\Invitation instead of your pivot id in order to update the existing record, otherwise you have not a relation for the current event that matches your pivot id.
$id = $actualPivot['guest_id']; // change guest_id for your foreig key name of \App\Invitation
$status = $actualPivot['status'];
if ($newStatus != $status)
{
dd($event->guests()->updateExistingPivot($id, ['status' => $newStatus]));
}

Categories