Undefined variable in Laravel 5.7

0

partners I am having the following problem and I can not find the solution, I have a view with a form to register a user, which has the following dropdownlist

<select class="form-control" name="department_id" id="department_id">
@foreach($departments as $department)
   <option value="{{$department->id}}">{{ $department->descripcion}}</option>
 @endforeach
 </select>

code that works, since the content is loaded to the select and I call the view in the following way:

public function create(){
    $departments = Department::all();
    $locations = Location::all();
    return view('create')->with(compact('departments','locations'));
}

Now, when I submit to submit, I get Undefined variable: departments (View: ... \ resources \ views \ create.blade.php)

Why does it tell me that DEPARTMENTS is not defined in that view if the data is loaded to the select correctly? the problem arises when I click on the send button and I want to do the POST to register. I can not find the problem.

Edit: add store function in the controller

public function store(UserRequest $request)
{
    $user = $request->all();
    $user['password'] = Hash::make($user['password']);
    User::create($user);
    return view('create')->with('exito','Usuario dado de alta correctamente');
}
    
asked by Marcelo 02.11.2018 в 20:00
source

2 answers

0

The view create.blade.php is clear that it expects at least the variable 'departments' and 'locations'. When it is called from the create function you do not get an error because in the call to the view, you pass these variables with 'compact'. But when submitting, it's the store function that calls the create view, and in this case you just pass the variable 'success', that's why you get that error .

You must make sure that when the create view is loaded, if it comes from the store function, do not do the @foreach

Greetings.

    
answered by 05.11.2018 в 16:48
0

You are using the same view for both Create and Store, so it is normal for you to wait for the values you would send from the Create when you return from Store.

Now, I do not know how is your view, or logic of why that, but I see 2 basic options and another a little more worked.

  • You create a view for each function (one for Create and one for Store).
  • Conditional areas to check if there is $ success, and if you do not do the filling part of the select (something like that).

    @isset($departments)
     @foreach($departments as $department)
       id}}">{{ $department->descripcion}}
     @endforeach
    @endisset
  • Shipments: $ departments and $ locations in the Store.

Now, it depends on if you want to allow modifying (which would not be the best since these are in store response) what you can do is send those two variables with all the values and make a conditional, that tells you if the variable $ success then the form would be sent to update (I would not recommend this much, because then you'll take these problems with you to the normal update, but for programming with ajax if it makes much more sense and use).

If you want to use the select to show the value you entered and it is saved in Database, you can send these values in the variables and use $ success to return disable or onlyread the select, something like:

 <select class="form-control" name="department_id" id="department_id" {{ isset($exito) ? 'disabled' : '' }}>
    
answered by 05.11.2018 в 18:27