Laravel doesn't return edit view - php

im using laravel 5.5 and i created a view for edit categories inside views/back/categories/edit
here my edit function in CategoriesController
public function edit($id)
{
$category = Categories::getall();
$categories = Categories::find($id);
return view('back.categories.edit', ['categories' => $categories, 'category' => $category]);
}
and here is my route
Route::group(['middleware'=>'admin'],function(){
Route::get('/dashboard','BackendController#index')->name('backend');
Route::group(['prefix' => 'categories'], function () {
Route::any('/show/{id}', ['as' => 'backend.categories.show', 'uses' => 'backend\CategoriesController#show']);
Route::get('/index', ['as' => 'back.categories.index', 'uses' => 'backend\CategoriesController#index']);
Route::any('/store', ['as' => 'back.categories.store', 'uses' => 'backend\CategoriesController#store']);
Route::any('/create', ['as' => 'back.categories.create', 'uses' => 'backend\CategoriesController#create']);
Route::any('/edit/{id}', ['as' => 'back.categories.edit', 'uses' => 'backend\CategoriesController#edit']);
Route::any('/update', ['as' => 'back.categories.update', 'uses' => 'backend\CategoriesController#update']);
Route::any('/destroy/{id}', ['as' => 'back.categories.destroy', 'uses' => 'backend\CategoriesController#destroy']);
});
});
my edit button
<a href="{{ url('back/categories/edit/'.$category->cat_id) }}" class="btn btn-success btn-sm">
<span class="fa fa-edit"></span> edit</a>
when i click on the edit button it return page not found with "Sorry, the page you are looking for could not be found." text and the URL :"back/categories/edit/4"

Since your route are named, you can use the route() helper to build working URL:
{{ route('back.categories.edit', ['id' => $category->cat_id]) }}

From your routes definition it looks like your url is /categories/edit/{id}
For links in templates I suggest to use the route helper function which takes the route name or the action method which takes controller#method
Also have a look at resource controllers
Route::resource('categories', 'CategoryController'); could replace your entire routes definition
https://laravel.com/docs/5.5/controllers#resource-controllers

You're missing the back portion of the prefix on your categories group.
Route::group(['prefix' => 'back/categories'], function () {
That would match your routes.

Related

Passing data in laravel NotFoundHttpException

I have this view:
<div class="window-options">
<form class="form-horizontal" method="POST" action="/api/b/sendInsert/{{$id}}">
<label for="inputEmail" class="col-lg-2 conrol label">Comment</label>
<input type="text" name="inputEmail" class="form-control" id="inputEmail" placeholder="Comment">
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
My route:
Route::post('b/sendInsert/{$id},
Mejili\Core\Controllers\BoardController#insert');
My Controller:
public function insert($id)
{
$data['id'] = $id;
$user = Auth::user()->id;
$comment = Input::get('inputEmail');
$comments=new CardComment;
$comments->commenter_id = $user;
$comments->card_id = $id;
$comments->save();
return Redirect::route('board', $data);
}
It throws NotFoundHttpException. Can someone help me? Thanks in advance.
Your dynamic route uri $id is being ignored by Laravel as the compiler only accepts \w strings. It's defined in Illuminate\Routing\RouteCompiler class. So, removing $ would make it recognized as usual.
Route::post('b/sendInsert/{id}', 'Mejili\Core\Controllers\BoardController#insert');
There is mismatch in your form's action path and route
action="/api/b/sendInsert/{{$id}}" change route to
Route::post('api/b/sendInsert/{$id}', 'Mejili\Core\Controllers\BoardController#insert');
Or you can use url() function in action attribute
Instead of using plain routing, you might want to consider giving aliases to each if your routes.
Route::get('sample_route',['as' => "sampleroute",'uses' => "SomeController#somemethood"]);
Route::post('sample_route',['as' => "post_sampleroute",'uses' => "SomeController#submitmethod"]);
Given route above are in the same URL by in different HTTP protocols, you can just simply use the route in your form that may look like this.
<form method="POST" action="{{route('post_sampleroute')}}">
...some inputs here.
</form>
A good naming convention to your routes might be a big help for your project. :)
$this->group(['as' => "user.", 'prefix' => "user"], function() {
$this->get('/', ['as' => "index", 'uses' => "UserController#index"]);
$this->get('create', ['as' => "create", 'uses' => "UserController#create"]);
$this->post('create', ['as' => "store", 'uses' => "UserController#store"]);
$this->get('edit/{id?}', ['as' => "edit", 'uses' => "UserController#edit"]);
$this->post('edit/{id?}', ['as' => "update", 'uses' => "UserController#update"]);
$this->get('trash', ['as' => "trash", 'uses' => "UserController#trash"]);
$this->any('restore/{id?}', ['as' => "restore", 'uses' => "UserController#restore"]);
$this->any('delete/{id?}', ['as' => "destroy", 'uses' => "UserController#destroy"]);
});

Trigger controller function when link is pressed laravel

I am trying to trigger a controller function when a link is pressed.
So.., this is the HTML
<a class="hollow button primary" href="{{ route('hire.worker', ['id' => $id]) }}"><i class="fa fa-user-plus"></i> Hire as Worker</a>
This is the route:
Route::post('/profile/{id}', [
'as' => 'hire.worker',
'uses' => 'ProfileController#hire'
]);
And this is the controller:
public function hire($id)
{
$worker = New Worker;
$worker->workerID = $id;
$worker->save();
}
When I press the button, the page only refreshes, nothing is stored in the database, I also have few other routes so that may be the issue.
// Profile
Route::get('/profile/{id}', [
'as' => 'display.profile',
'uses' => 'ProfileController#index'
]);
Route::post('/profile/{id}', [
'as' => 'display.profile',
'uses' => 'ProfileController#store'
]);
Route::post('/profile/{id}', [
'as' => 'hire.worker',
'uses' => 'ProfileController#hire'
]);
Links use GET as the request method so Route::post is not correct.
Create a new route like the one below and at the end of the controller method redirect to whatever page you originally wanted to go.
Route::get('/profile/link/{id}', [ 'as' => 'hire.worker', 'uses' => 'ProfileController#hire' ]);

Dynamic Route name Laravel 5.2

I want to create dynamic route name for my app. Here is my route file
Route::group(['prefix' => '{team}/dashboard', 'middleware' => 'isMember'], function() {
Route::get('/user', array('uses' => 'UserController#index', 'as' => 'user.index'));
Route::get('/user/edit/{id}', array('uses' => 'UserController#edit', 'as' => 'user.edit'));
Route::patch('/user/{id}', array('uses' => 'UserController#update', 'as' => 'user.update'));
Route::delete('/user/{id}', array('uses' => 'UserController#destroy', 'as' => 'user.delete'));
it's not simple if i have to define route like this
'route' => ['user.delete', $team, $user->id]
or
public function destroy($team,$id) {
// do something
return redirect()->route('user.index', $team);
}
I want to generate route name like "$myteam.user.delete" or something more simplier like when i define "user.delete" it includes my team name.
How i can do that? is it possible?
You could do that by setting as. Also using resource routes will be handy.
$routeName = 'team.';
Route::group(['as' => $routeName], function(){
Route::resource('user', 'UserController');
});
Now you can call like
route('team.user.index');
More on resource routes here https://laravel.com/docs/5.3/controllers#resource-controllers
try this:
Route::delete('/user/{team}/{id}', array('uses' => 'UserController#deleteTeamMember', 'as' => 'myteam.user.delete'));
Now call the route as:
route('myteam.user.delete', [$team, $id]);

Laravel Is it possible to pass a variable in route group?

I have following routes:
Route::group(['prefix' => 'group1'], function () {
Route::get('view1', ['as' => 'group1_view1', 'uses' => 'group1Controller#get_view1']);
Route::get('view2', ['as' => 'group1_view2', 'uses' => 'group1Controller#get_view2']);
});
Route::group(['prefix' => 'group2'], function () {
Route::get('view1', ['as' => 'group2_view1', 'uses' => 'group2Controller#get_view1']);
Route::get('view2', ['as' => 'group2_view2', 'uses' => 'group2Controller#get_view2']);
});
I wish to pass variables, for example, $title = 'group-one' to all views in group1 and $title = 'group-two' to all views in group2. Instead of adding variable $title in all methods in each group controller, is it possible to pass variables groupwise in routes?
NO you cannot pass variables into route groups, but if in case you need one piece of view/code in multiple views, title for example, you can use view composers instead.

Laravel Routes returning a 404 with route parameters

This is not the first time that I have used route parameters in laravel, however I cannot seem to get this to work.
Route:
Route::group(['prefix' => 'admin', 'before' => 'auth|beta|admin'], function()
{
Route::post('remove/{$id}', ['uses' => 'AdminController#postRemoveID', 'as' => 'admin.postremoveid']);
});
Controller:
public function postRemoveID($id)
{
$remove = ServiceProvider::where('id','=',$id)->first();
$remove->delete();
return Redirect::route('admin.manage'); //This just redirects to the page the user is currently on
}
Blade:
<a href="{{ route('admin.postremoveid', $id) }}">
<i class="fa fa-times"></i>
</a>
What would be causing my site to be redirecting to a 404?
Thanks for all your help!!
-Patrick
Use Route:get();
Route::get('remove/{id}', ['uses' => 'AdminController#getRemoveID', 'as' => 'admin.postremoveid']);
Controller:
public function getRemoveID($id)
{
$remove = ServiceProvider::where('id','=',$id)->first();
$remove->delete();
return Redirect::route('admin.manage'); //This just redirects to the page the user is currently on
}
You don't need that $ on the wild card
Route::group(['prefix' => 'admin', 'before' => 'auth|beta|admin'], function()
{
Route::post('remove/{id}', ['uses' => 'AdminController#postRemoveID', 'as' => 'admin.postremoveid']);
});

Categories