简体   繁体   中英

Laravel edit route not defined

Here route code :

Route::resource('ruangjns', 'RuanganjnsController');
Route::get('ruangjns/{ruangjn}/edit', 'RuanganjnsController@edit');

Here controller :

public function edit(Ruanganjns $ruangjn)
    {
        $ruangjn = Ruanganjns::findOrFail($ruangjn->id);
        return view('ruanganjns.edit', compact('ruangjn'));
    }

Here view index :

<tbody>
@php
$no = 1;
@endphp
@foreach ($ruanganjns as $data)
<tr>
<td>{{ $no++ }}</td>
<td>{{ $data->kode_jenis_ruangan }}</td>
<td>{{ $data->jenis_ruangan }}</td>
<td>{{ $data->tgl_berlaku }}</td>
<td>{{ $data->status }}</td>
<td>
<a href="{{ route('ruangjns.edit', $data->id) }}" class="btn btn-warning">Edit</a>
</td>
<td>
<form action="{{ route('ruangjns.destroy', $data->id) }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="btn btn-danger" onClick="return confirm('Are You Absolutely Sure You Want to Delete the Data?')">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>

Here result form browser

在此处输入图片说明

Lately before i got error code from edit. This My Post Before So i put this code

Route::get('ruangjns/{ruangjn}/edit', 'RuanganjnsController@edit');

after that work now error going to index can't read an EDIT button like in the picture. But when i search error on browser it have to replace with URL

<a href="{{ route('ruangjns.edit', $data->id) }}" class="btn btn-warning">Edit</a>

<a href="{{ url('ruangjns.edit', $data->id) }}" class="btn btn-warning">Edit</a>

but the main problem is how to access edit if i using URL or not using it ?

Thank You.

Just delete 'Route::get('ruangjns/{ruangjn}/edit', 'RuanganjnsController@edit');'

Laravel Resource controller already has ' edit ' action.

https://laravel.com/docs/5.7/controllers#resource-controllers

If you would like to use route inside your view, you should give the route a name, like so:

Route::get('ruangjns/{ruangjn}/edit', 'RuanganjnsController@edit')->name('ruangjn-edit');

Then just use

{{route('ruangjn-edit', ['ruangjn' => $data->id])}}

For more information look at the Laravel Routing Documentation https://laravel.com/docs/5.7/routing

it is because you have not specified any name for the routes.

Route::resource('ruangjns', 'RuanganjnsController');
Route::get('ruangjns/{ruangjn}/edit', 'RuanganjnsController@edit')->name('ruangjns.edit');

Or the more decent way you can try

<a href="{{ url('ruangjns.update', $data->id) }}" class="btn btn-warning">Edit</a>

Moreover only one route Route::resource('ruangjns', 'RuanganjnsController'); will do the trick you dont have to specify the separate route because it will be handled by the resource it self.

use this

<a href="{{ route('ruangjns/'.$data->id.'/edit') }}" class="btn btn-warning">Edit</a>

public function edit($ruangjn)
{
    $ruangjn = Ruanganjns::findOrFail($ruangjn);
    return view('ruanganjns.edit', compact('ruangjn'));
}

According to this Laravel's documentation , you need write parameters with brackets. Can you please try with this syntax on the link in your view ?

{{ route('ruangjns.edit', ['ruangjns' => $data->id]) }}

Before you use described solution bellow, try to use default route - remove your edit route line. In case you made controller with artisan console command passing flag -r to be resourceful, you already have edit method defined. What you do here is overriding default edit method ( check table ) writing new edit method that has no route name defined. Solutions are to use default method or to use set custom edit method, but importantly, before resource routes.

Route::get('ruangjns/{ruangjn}/edit', 'RuanganjnsController@edit')->name('ruangjns.custom-edit');
Route::resource('ruangjns', 'RuanganjnsController');

which you would call with {{ $route('ruangjns.custom-edit', $ruangjnsId) }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM