I'm new at Laravel, and I'm having some problems setting up an update for my application. I'm trying to pass an id from a view to a controller through routing to select an specific line, and after that I need to pass from the controller to another view. Idk where I'm doing wrong.
Here's my view code that passes de id:
@forEach($line as $data)
<tr>
<td><a href="{{route('edit.line', ['id', $data->id])}}"><i class="icon ion-md-create"></i></a></td>
<td>{{$data->name}}</td>
<td>{{$data->time}}</td>
</tr>
@endforEach
Here's the route:
Route::get('/lineEdit/{id}', 'LineController@formEdit')->name('edit.line')->middleware('auth');
Here's the controller function from route:
public function formEdit($id){
$line = Line::find($id);
$lineUp = Line::select('*')
->where('id', $line)->get();
return view('lineEdit')->with('line', $lineUp);
}
And here's the piece of the view that will recieve the array:
<div class="card-body">
@forEach($line as $data)
<form method="POST" action="{{route('update.line', $data->id)}}">
@csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">
{{__('Name')}}
</label>
<div class="col-md-8">
<input type="text" name="name" class="form-control {{$errors->has('name') ? 'is-invalid' : ''}}" value={{$data->name}} required autofocus >
@if($errors->has('name'))
<span class="invalid-feedback" role="alert">
<strong>{{$errors->first('name')}}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row">
<label for="time" class="col-md-4 col-form-label text-md-right">
{{__('Time')}}
</label>
<div class="col-md-8">
<input type="number" name="time" class="form-control {{$errors->has('time') ? 'is-invalid' : ''}}" value={{$data->time}} required >
@if($errors->has('time'))
<span class="invalid-feedback" role="alert">
<strong>{{$errors->first('time')}}</strong>
</span>
@endif
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Save') }}
</button>
</div>
</div>
</form>
@endforEach
</div>
Everything inside the forEach doesn't render. I can't see the problem.
Line
or multiple? Also, why are you using theLine
model twice in your controller method? – RwdLine
by id (via::find($id)
), but then calling what is essentially the same query, except you can't use$line
in the query, as it's aModel
. You'd have to use$line->id
, but again, that's redundant as you already have$id
, which would be the same thing... What are you trying to do? Also, if you're trying to edit a singleLine
, why would you send it to the view in aCollection
? Just remove the@foreach()
loop and controls for editing$line
... – Tim Lewis