Custom function in Controller with resource wont work - laravel

I have created my own custom function in my RoomsController
public function join($id){
return $id;
}
Then I want to pass variable to it and it says MethodNotAllowedHttpException
And my Form looks like this
{{Form::open(['action'=> ['RoomsController#join', $room->id], 'method' => 'POST' ])}}
{{Form::submit('Join', ['class' => 'btn btn-danger'])}}
{{Form::close()}}
Also have these routes
Route::get('/','PagesController#index');
Route::get('/about', 'PagesController#about');
Route::get('/services', 'PagesController#services');
Route::get('/register', 'PagesController#register');
Route::get('/logout', 'PagesController#logout');
Route::get('/rooms/join', 'RoomsController#join');
Route::resource('posts','PostsController');
Route::resource('rooms','RoomsController');
Auth::routes();
Route::get('/dashboard', 'DashboardController#index');
I have tried in many different ways i dont know why it is not working. All update edit destroy resource functions are working. Thank's for helping :)

You're submitting a POST request but the route is expecting a GET request. If you change your route to Route::post('/rooms/join', 'RoomsController#join'); it should work

change the method to post and put the route below the resource route
Route::resource('rooms','RoomsController');
Route::post('/rooms/join', 'RoomsController#join');

Related

why i see Call to undefined method Error?

im learning laravel so if im not well as you are accept my apologies...
my problem is when i try to define a new method in web.php i got error!some times phpstorm sets problem on 'Route'word so i can run my blade pages but sometimes it sets problem on 'get','post','group' ,...and i cant run my app
ill show you how i defined my routes
use Illuminate\Routing\Route;
Route::get('/', function () {
return view('welcome');
});
Route::group(['prefix' => 'admin'] ,function (){
Route::get('/users','UsersController#index');
});
after all this my point is making a controller so i got this error to fix so i can move on...
Named groups in laravel
Route::group(['prefix'=>'admins','as'=>'admin.'], function(){
Route::get('users', ['as' => 'user', 'uses' = > 'UsersController#index']);
});
Also make sure you have index method in your UsersController.
FYR :- https://laraveldaily.com/laravel-5-1-names-for-route-groups/

laravel pathing in link

I am using same thing but result is different. In posts.blade.php i can call like this
<a href = "posts/{{post->id}}"edit>Edit</a>
But when i use same thing in rooms.blade.php indisde rooms folder i had to use like this.
<a href = "{{$room->id}}/edit" class = 'btn btn-primary'> Edit</a>
It is really confusing me. Any solution? i want to add link for editing post inside room. I have tried many things but i dont understand.
My Route
Route::get('/','PagesController#index');
Route::get('/about', 'PagesController#about');
Route::get('/services', 'PagesController#services');
Route::get('/register', 'PagesController#register');
Route::get('/logout', 'PagesController#logout');
Route::get('/posts', 'PostsController#posts');
Route::resource('posts','PostsController');
Route::resource('rooms','RoomsController');
Auth::routes();
Route::get('/dashboard', 'DashboardController#index');
You can give the name of the route
Foe example:
Route::get('/post/{id}', 'PostController#showPost')->name('post.show');
And then call the blade
Link to Resource {{ $id }}

Laravel not reaching update method and returns edit view again – route wrong

When I click Save on my edit view, my routing brings back my edit view instead of my index view and my update method is never reached.
I noticed that I reach the update method if I remove “UsersRequest $request” from the method parameters. Not sure why, and if it’s related, but I need $request to do my update (see controller code below):
Routes:
Route::get('/users', 'UsersController#index')->name('users.index');
Route::patch('/users/{id}',
[
'as' => 'users.update',
'uses' => 'UsersController#update'
]);
Route::get('/users/{id}/edit', 'UsersController#edit');
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\UsersRequest;
//public function update($id, UsersRequest $request)
public function update($id) //- with $request removed, the index view is displayed
{
$user = \Auth::user();
$user->update($request->all());
return view('users.index');
}
Edit view:
{!! Form::model($user, ['method' => 'PATCH', 'action' => [ 'UsersController#update', 'user' => $user->id ] ]) !!}
{!! Form::submit('Save', ['class'=>'btn primary']) !!}
{!! Form::close() !!}
Network after save button clicked
URL Protocol Method Result
/myapp/public/users/1 HTTP POST 302 Goes for the update route
http://000.000.000.000/myapp/public/users/1/edit HTTP POST 200 Redirects to the edit route??
.env
APP_URL=http://000.000.000.000/myapp/public
You're failing whatever validation is present in your UsersRequest form request. When the validation fails, it redirects you back to where you came from, which is your edit view. Your edit view should be updated to show the validation errors so that your users know what fields need to be fixed.
The reason it works when you remove the UsersRequest $request parameter is that the validation is no longer being performed.

Call to undefined method Illuminate\Routing\ResourceRegistrar::addResourceEmployee()

I have this route:
Route::get('/', function () {
return view('index');
});
Route::resource('admin', 'EmployeeController');
I have model Employee and EmployeeController( with empty resource methods)
Error : Call to undefined method Illuminate\Routing\ResourceRegistrar::addResourceEmployee()
What is wrong with my code? I have used the same approach in other project and it worked.
Route::resource('admin', 'EmployeeController');
is attempting to bind to a model named Admin.
Route::resource('employees', 'EmployeeController');
should work with the model you have. To make it work with admin, name the resource parameter.
Route::resource('admin', 'EmployeeController', ['parameters' => [
'admin' => 'employee'
]]);
edit
Did you reference something outside the Laravel docs to use AddResourceEmployee(). Seems like a custom solution to me.
https://stackoverflow.com/a/16661564/320487

How call route resource in Laravel

I have problem. My route definition contains:
route::resource('admin/settings/basic','admin\settings\BasicController');
but I don't know how can I call the edit action from basiccontroller in my a href link.
href='{{ link_to_route('admin/settings/basic/edit') }}'
Please give me some advice.
Given a route like the following:
app/Http/routes.php
Route::resource('profile', 'ProfileController');
Your controller could look something like this:
app/Http/Controllers/ProfileController
public function edit($id)
{
$profile = Profile::all(); // Grab some data
return view('profile.edit', [$profile]); // Pass some data to the Edit view
}
In the view, you might have a form for editing like so:
resources/views/profile/edit.blade.php
<?= Form::model($profile, ['route' => ['profile.update', $profile->id], 'method' => 'PUT', 'class' => 'form-horizontal']) ?>
That form routes to ProfileController#update
For other routes, such as an index, it is handled all for you. You just have to make sure you return the correct view in your ProfileController#index, and hitting the route for /profile will be passed through that method
You can always refer to the documentation as well - RESTful Resource Controllers

Resources