Reputation:
I have setup a Laravel8 blog demo application to learn the ins/outs of Laravel8. However, after creating a new controller (copy of PostsController called BatchesController), adding the resource to the web.php, only the default routes work such as edit/create/destroy. I tried adding a new route, only to get the following error: "Uncaught Error: Ziggy error: route 'batch.run' is not in the route list".
batch.run is my new route and it pretty much is a copy of the "destroy" route that is already setup for deleting the posts. I do not understand what I am doing wrong when I am simply using something that is a near copy of the default routes. Where do these routes get "added" when using the cover-all "resource" in web.php. Shouldn't it just run any method in the controller based on the name of the route?
Here is my web.php:
Auth::routes(['verify' => true]);
Route::get('login', [LoginController::class, 'showLoginForm'])->name('showLoginForm')->middleware('guest');
Route::get('register', [RegisterController::class, 'showRegisterForm'])->name('showRegisterForm')->middleware('guest');
Route::post('login', [LoginController::class, 'authenticate'])->name('login');
Route::post('register', [RegisterController::class, 'register'])->name('register');
Route::post('logout', [LoginController::class, 'logout'])->name('logout');
Route::resource('post', PostsController::class);
Route::resource('batch', BatchesController::class);
Route::resource('token', TokensController::class);
Route::resource('home', HomeController::class);
Route::redirect('/', 'home');
Here is my controller method:
public function run(Request $request, $id) {
Batch::find($id)->run();
$request->session()->flash('success', 'Batch run successfully!');
return redirect()->route('batch.running');
}
Here is my .vue file (minus the template):
<script>
import AppHeader from "../../Partials/AppHeader";
import ErrorsAndMessages from "../../Partials/ErrorsAndMessages";
import {usePage} from "@inertiajs/inertia-vue3";
import {Inertia} from "@inertiajs/inertia";
import {computed, inject} from "vue";
export default {
name: "Batches",
components: {
ErrorsAndMessages,
AppHeader
},
props: {
errors: Object
},
setup() {
const route = inject('$route');
const deleteBatch = (id) => {
if (!confirm('Are you sure want to delete #' + id + '?')) return;
Inertia.delete(route('batch.destroy', {id}));
}
const runBatch = (id) => {
if (!confirm('Are you sure you want to run #' + id + '?')) return;
Inertia.post(route('batch.run', {id}));
}
const batches = computed(() => usePage().props.value.batches);
const numberLinks = batches.value.links.filter((v, i) => i > 0 && i < batches.value.links.length - 1);
return {
batches,
deleteBatch,
runBatch,
numberLinks
}
}
}
</script>
<style scoped>
.action-btn {
margin-left: 12px;
font-size: 13px;
}
.article {
margin-top: 20px;
}
</style>
Upvotes: 2
Views: 11684
Reputation: 21
This instruction:
Route::resource('batch', BatchesController::class);
Does not create a route named "batch.run" automatically. Only creates these: [batch.index, batch.create, batch.store, batch.edit, batch.update and batch.destruct or batch.delete].
If you want to use "batch.run" you need to create it manually like:
Route::post('batchRun', [BatchesController::class, 'run'])->name('batch.run');
Upvotes: 0
Reputation: 87
Try to give name to the route.
Route::get('gifts/list','GiftsController@list')->name("gifts.list");
and call the route using the route function
let listUrl = route("gifts.list");
The above solved my issue.
Upvotes: 3