Laravel patch method not supported (even on update method) - php

HTML PATCHING FORM
{{ Form::model($model , [ 'route' => ['admin.friendship.update', $model->id] , 'class'=>'needs-validation ajax-form', 'method' => 'post' ]) }}
#method('PATCH')
{{ Form::select('user_id' , $users , null , ['id' => 'sender', 'class' => 'form-control']) }}
{{ Form::select('friend_id' , $users , null , ['id' => 'reciever', 'class' => 'form-control']) }}
{{ Form::select('status' , ['-2' => -2 , '-1' => -1 , '0' => 0 , '1' => 1] , null , ['id' => 'status', 'class' => 'form-control']) }}
{{ Form::close() }}
Update method:
public function update(FriendshipsAdminForm $request, Friendship $friendship)
{
$friendship->update($request->validated());
return redirect()->route('admin.friendship.index');
}
Request form
class FriendshipsAdminForm extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'user_id' => 'required',
'friend_id' => 'required',
'status' => 'required',
];
}
}
Route:
Route::resource('friendship', \App\Http\Controllers\Admin\FriendshipController::class);
This is my form and it's really strange. maybe I can not see a thing that is blocking me from doing this. there is not typo or syntax mistake. Is there anything about patch method I should consider even when trying update method?
UPDATE
ERROR MESSAGE , ERROR STATUS 405
UPDATE2
I'm using ajax for it.
I'm using ajax the way I use above with near 30 models and all are more complex than this one but all are updating and working nicely but this one is strange

Since you using ajax, #method is not working in that. You need to activate PATCH in your webserver, or change your ajax code to submit form in post and add _method in params.
For example in jQuery
$.ajax({
...
method: 'POST',
data:{
...
'_method': 'PATCH',
}
});

Related

Having hard time understanding Forms from laravel collectives

I'm new to Laravel. I've created a form and trying to delete one of the post. I just want to know how opening a form from Laravel collectives works.
Currently, this is how my form looks like in show.blade.php
{!! Form::open(['action' => ['PostsController#destroy', $post->id], 'method' => 'POST', 'class' => 'pull-right']) !!}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete', ['class' => 'btn btn-danger']) }}
{!! Form::close() !!}
The above form gives me the error Action PostsController#destroy not defined.
But when I add 'url' => 'posts/' in Form i.e.
{!! Form::open(['url' => 'posts/', 'action' => ['PostsController#destroy', $post->id], 'method' => 'POST', 'class' => 'pull-right']) !!}
The above error disappears.
Below is the destroy() function in PostsController
class PostsController extends Controller
{
public function destroy($id)
{
$post = Post::find($id);
$post->delete();
return redirect('/posts')->with('success','Post removed');
}
}
web.php
// --- Posts Routing
Route::get('/posts', [App\Http\Controllers\PostsController::class, 'index'])->name('posts');
MY QUESTIONS
I'm redirecting in the destroy() function in PostsController, why url is necessary with action in Form from Laravel
collectives to avoid the above error?
When to use 'url' and when to use action?
I've laravel 4.2.10.

Laravel Action App\Http\Controllers\Admin\ConcursoController#store not defined

I want to save some form data, and I'm Getting error.
The Error
Action App\Http\Controllers\Admin\ConcursoController#store not defined. (0)
My Form
{!! Form::open(['action'=>'Admin\ConcursoController#store', 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('company','Entidade')}}
{{Form::text('company','',['class' => 'form-control', 'placeholder' => 'Nome da entidade aquĆ­..'])}}
</div>
{{Form::submit('submeter', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
My Route
$this->group(['middleware' => ['auth:admin'], 'namespace' => 'Admin', 'prefix' => 'admin'], function(){
$this->get('/', 'AdminController#index')->name('admin.home');
$this->resource('concursos', 'ConcursoController');
});
Controller index Method
public function index()
{
$concursos = Concurso::all();
$title = 'Concursos';
return view('admin.concursos.index',compact('title'))->with('concursos',$concursos);
}
Controller Create method
public function create()
{
return view('admin.concursos.create');
}
Controller Store Method
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required',
]);
//Criar concurso
$concurso = new Concurso;
$concurso->title = $request->input('title');
$concurso->body = $request->input('body');
$concurso->save();
return redirect('/admin/concursos')->with('Success', 'Concurso Adicionado');
}
Laravel version 5.7.14
Probably check this file: App\Http\Controllers\Admin\ConcursoController and see if you have a function/method called 'store'. The error is pretty straightforward that script can't find that function.
replace your form
['route' => ['concursos.store']
like
{!! Form::model($transactions, ['route' => ['transaction.store'], 'method' => 'POST','class'=>"form-horizontal"]) !!}

"MethodNotAllowedHttpException" retuned from Laravel 5.2 update

I am getting MethodNotAllowedHttpException retuned when updating a form in Laravel 5.2. I understand there could be an issue with the put method.
The form sending from the index:
{!! Form::model('Customers', ['route'=>['products.update', Auth::user()->id]]) !!}
{{ Form::hidden('business', Auth::user()->name, array('class' => 'form-control', 'required' => '','maxlength'=>'255'))}}
{{ Form::label('post', 'Mailbox')}}
{{ Form::checkbox('post',1, null, array('class' => 'form-control'))}}
The Controller is:
public function update(Request $request, $id)
{
$this->validate($request, array (
'post' => '',
'mailbox' => '',
'conum' => '',
'prefix' => '',
'telans' => '',
'TC' => 'required',
));
//store
$post = Customers::find($id);
$post->post = $request->input('post');
$post->postpro = $request->input('mailbox');
$post->telans = $request->input('telans');
$post->conum = $request->input('conum');
$post->prefix = $request->inut('prefix');
$post->tc = $request->input('TC');
//save
$post->save();
//session flash message
//Session::flash('success','This customer has now been added');
//redirect
return redirect('/home');}
And the route is as follows:
Route::resource('products', 'ProductsController');
Thank you
Your forgot the quotes, replace this :
PHP
$post->post = $request->input(post);
with this :
$post->post = $request->input('post');
do not forget to set the _method as put.

Laravel 4 form post issue

my problem is that when i submit my form it pass through the get method.
The workflow is form submit -> get -> post and it should be form submit -> post.
I need to have a condition in my get method to validate de array is not null
My code:
Routes
Route::get('/pre-register-birth',array('as'=>'pre-register-birth', 'uses'=>'UserController#preRegisterBirthData'));
Route::post('/pre-register-birth', 'UserController#preRegisterBirthDataPost');
View
{{ Form::open(array('method' => 'POST', 'action' => 'UserController#preRegisterBirthDataPost',
'class'=>'form-horizontal', 'id'=>'regist-form')) }}
Controller
public function preRegisterBirthData()
{
$user = Session::get('user');
if ($user)
return View::make('user/pre-register-birth')->with('tempUser', $user);
else
return Redirect::Route('pre-register-get');
}
public function preRegisterBirthDataPost()
{
$validator = Validator::make(Input::all(),
array(
'birthplace' => 'required|max:100',
'birthdate' => 'required|max:100|date|date_format:Y-m-d'
)
);
if ($validator->fails()) {
return Redirect::Route('pre-register-birth')
->withErrors($validator)
->withInput();
} else {
$user = array(
'email' => Input::get('email'),
'pass' => Input::get('pass'),
'name' => Input::get('name'),
'surname' => Input::get('surname'),
'birthplace' => Input::get('birthplace'),
'birthdate' => Input::get('birthdate'),
'hourKnow' => Input::get('hourKnow'),
'dataCorrect' => Input::get('dataCorrect'),
'news' => Input::get('news'),
);
return Redirect::Route('pre-register-terms')->with('user', $user);
}
}
I think I've seen this issue before. Laravel for whatever odd reason doesn't like the action => ... So, change your form declaration from a to b:
// A
{{ Form::open(array('method' => 'POST', 'action' => 'UserController#preRegisterBirthDataPost',
'class'=>'form-horizontal', 'id'=>'regist-form')) }}
// B
{{ Form::open(array('method' => 'POST', 'url' => 'pre-register-birth',
'class'=>'form-horizontal', 'id'=>'regist-form')) }}
Notice I changed action => ... to url => ... It's a small change, but it might solve it. However, you may need to add in:
Route::post('/pre-register-birth', array('as'=> 'pre-register-birth', 'uses' => 'UserController#preRegisterBirthDataPost'));
So it recognizes the named route.
Hope this helps!

Laravel 4 Form passing 2 parameters

Currently i'm working on a project to manage my cost on fuel.
Now i try to pass 2 parameters in a Form::open() which sadly doesn't work.
The reason why i think i need to pass 2 parameters at once is because my url is Sitename/car/{id}/tank/{id}
What am i doing wrong?
edit.blade.php
Form::open(array('class' => 'form-horizontal', 'method' => 'put', 'action' => array('TankController#update', array($aid, $id))))
Problem Code
'action' => array('TankController#update', array($aid, $id)
-Results in the following error:
Parameter "tank" for route "car.{id}.tank.update" must match "[^/]++" ("" given) to generate a corresponding URL.
TankController.php
public function edit($id, $tid)
{
$tank = Tank::find($tid);
if(!$tank) return Redirect::action('TankController#index');
return View::make('Tank.edit', $tank)->with('aid', $id);
}
public function update($id, $tid)
{
$validation = Validator::make(Input::all(), Tank::$rules);
if($validation->passes()){
$tank = Tank::find($tid);
$tank->kmstand = Input::get('kmstand');
$tank->volume = Input::get('volume');
$tank->prijstankbeurt = Input::get('prijstankbeurt');
$tank->datumtank = Input::get('datumtank');
$tank->save();
return Redirect::action('TankController#index', $id)->with('success', 'Tankbeurt succesvol aangepast');
} else return Redirect::action('TankController#edit', $id)->withErrors($validation);
}
Route.php
Route::resource('car', 'CarController');
Route::resource('car/{id}/tank', 'TankController');
Route::controller('/', 'UserController');
-Url Structure
SITENAME/car/2/tank/2/edit
I've also looked into the api documents but found nothing.
http://laravel.com/api/source-class-Illuminate.Html.FormBuilder.html
Thanks in advance
Try this:
Form::open(array('class' => 'form-horizontal', 'method' => 'put', 'action' => array('TankController#update', $aid, $id)))
This is the best solution
Form::open (array ('method'=>'put', 'route'=>array($routeName [,params...]))
An example:
{{ Form::open(array('route' => array('admin.myroute', $object->id))) }}
https://github.com/laravel/framework/issues/491
Try to use in your blade:
For Laravel 5.1
{!! Form::open([
'route' => ['car.tank.update', $car->id, $tank->id],
'method' => 'put',
'class' => 'form-horizontal'
])
!!}

Categories