I can’t pass values from one page to another with Laravel

Asked

Viewed 88 times

1

I have a problem with Laravel that I can’t solve, and I’ve tried many things: What I want is to take the values that are on one page and send to another on a form.

The edit button in a list has the following code:

<a class="dropdown-item" href="{{ route('people.edit', $people) }}">{{ __('Editar') }}</a> 

That is the route:

Route::put('peoples', ['as' => 'peoples.edit', 'uses' => 'PeopleController@edit']);

That’s the code on the controller

public function edit(People $people)
{
    return view('profilePeople.edit', compact('people'));
}

This is the class code

<?php namespace App; use Illuminate\Database\Eloquent\Model; class People extends Model { protected $table = 'people'; protected $fillable = ['name', 'email',]; protected $guarded = ['id']; }

This is the code where an input should retrieve past values.

<input type="text" name="name" id="input-name" class="form-control form-control-alternative{{ $errors->has('name') ? ' is-invalid' : '' }}" placeholder="{{ __('Nome') }}" value="{{ old('name', $people->name) }}" required autofocus>

There is no error but the values are not passed.

I do not know if with this information someone can help me, but just send me the information you need and I will add.

From now on, thank you!

1 answer

0


Their routes:

// exibe a view com o formulario para editar a pessoa
Route::get('/peoples/{id}/edit', 'PeopleController@edit');
// atualiza a pessoa no banco de dados
Route::put('/peoples/{id}', 'PeopleController@update');

Your button:

<a href="{{ url('/peoples/' . $people->id . '/edit') }}">
    {{ __('Editar') }}
</a>

Your controller:

public function edit($id)
{
    $people = People::findOrFail($id);

    return view('profilePeople.edit', compact('people'));
}

public function update(Request $request, $id)
{
    $data = $request->all();
    $people = People::findOrFail($id);

    try {
        DB::transaction(function() use ($data) {
            $people->update($data);
        });
    }catch(\Exception $exception){
        abort(500);
    }

    return redirect('/peoples/' . $id);
}

profilePeople > edit.blade.php

<form action="{{ url('/peoples/' . $people->id) }}" method="post">
    @csrf
    @method('put')

    <!-- Seus campos -->
</form>
  • It worked super well! I broke my head so much and you solved it so fast. Thank you so much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.