Skip to content
Advertisement

How to get saved value from dropdown in edit page laravel?

inside EmployeeController in the edit function, i have this code

JavaScript

and inside edit.blade.php to display the dropdown i have this code

JavaScript

This is the employees table and this is departments table

Now, the goal is i want the dropdown on edit page to display department name of the employee belongs to, while the dropdown still have all of the department name. so i change can it, but when i run this code it gives me this error.

Trying to get property ‘id’ of non-object (View:

C:xampphtdocsims-it-laravel7resourcesviewsemployeesedit.blade.php)

i have read other threads here but those code still doesn’t solve the problem

Advertisement

Answer

$departmentlists = Department::pluck('id', 'name');

Later You use

@foreach($departmentlists as $dl) and $dl->id

$dl is NOT an object, it is an array because of the pluck() function.

More precisely it looks like this

JavaScript

Note: To see it Yourself try using dd($departmentlists) or dump($departmentlists)

Please see Laravel Eloquent Pluck

In this case You might want to use Department::select(['id', 'name'])->get() as it will return collection of objects with specified properties.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement