0

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.

4
  • Are you trying to load one Line or multiple? Also, why are you using the Line model twice in your controller method? Commented Mar 15, 2019 at 14:33
  • 2
    Your code is redundant; you're querying for a Line 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 a Model. 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 single Line, why would you send it to the view in a Collection? Just remove the @foreach() loop and controls for editing $line... Commented Mar 15, 2019 at 14:39
  • @TimLewis I need my user to see the current data and pass the id for updating the line. I removed the useless query and leave de find::($id) and the forEach. I need my view to be able to recieve the query result, and pass back to the controller the id and new values. Commented Mar 18, 2019 at 11:33
  • @TimLewis thank you so much for the help, besides the problems you said, I had a typo when passing the id to the controller. Now it's working perfectly Commented Mar 18, 2019 at 12:59

3 Answers 3

2

In your controller:

public function formEdit($id){
    $line = Line::find($id);
    return view('lineEdit', compact('line'));
}

I use the compact method to send the $line variable to the view.

Also, if you want to get the Line model to edit you dont need this:

$lineUp = Line::select('*')->where('id', $line)->get();

you only need this to find your model:

$line = Line::find($id);
Sign up to request clarification or add additional context in comments.

3 Comments

$line is model or null not an id.
Using view(...)->with('line', $lineUp); is essentially the same as view(..., compact('line')), so yes, they are passing the line to the view.
the $lineUp variable dont make much sense to me, what i understand of his question is taht he only wants to edit a model. He's comparing a id to a model in his query
0

Try this in your controller:

public function formEdit($id){
    $line = Line::find($id);

    return View::make('lineEdit', compact('line'));
}

This should pass a variable $line to your view

Comments

-1

Looks like the problem is in the controller code.

$line = Line::find($id);

returns you an object (just one object!) of class Line if a) $id is an integer b) $id is the primary key c) this key exists in the DB

So, either $line is null or a model object. The next query

$lineUp = Line::select('*')->where('id', $line)->get();

cannot be successful in either case.

As I understand your intent $id is just an attribute (because you expect to have a collection of objects). So, try

$lineUp = Line::select('*')->where('id', $id)->get();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.