Skip to content
Advertisement

Undefined variable: users (View: /home/laravel/web/laravel.swt101.eu/public_html/abonamenty/resources/views/products/edit.blade.php) [closed]

Firstly i made laravel relations in models and foreign key. My foreign key is user_id.

This is part of my view witch generate error:

            <select class="form-controll" name="user_id">

                    @foreach($users as $user)

                    <option value="{{$user->id}}">{{$user->name}}</option>

                    @endforeach

            </select>

This is controller method for this view

    <?php
        
    namespace AppHttpControllers;
    use KyslikColumnSortableSortable;    
    use AppProduct;
    use IlluminateHttpRequest;
    use AppUser;
        ...
            
        public function edit(Product $product)
        {   
            $user = User::all('name','id');
            return view('products.edit',compact('product', 'user'));
        }

...

Advertisement

Answer

You are using ‘user’ not ‘users’ in your compact function. Also your variable is defined as $user not $users. It should be like this:

public function edit(Product $product)
{   
    $users = User::all('name','id');
    return view('products.edit',compact('product', 'users'));
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement