put method not supported in laravel - php

I tried to update a post using edit route but when I send the form and use the update function give me an error
my code is
<form action="/posts{{$posts->id}}" method="POST">
#method('PUT')
#csrf
<label for="">title</label>
<input type="text" name="title" class="form-control" >
<label for="">body</label>
<textarea type="text" name="body" class="form-control">{{$post->body}}</textarea>
<input type="submit" class="btn btn-primary" value="edit">

You have to use like this
<form action="{{url('')}}/posts/{{$post->id}}" method="POST">
#csrf
<label for="">title</label>
<input type="text" name="title" class="form-control" >
<label for="">body</label>
<textarea type="text" name="body" class="form-control">{{$post->body}}</textarea>
<input type="submit" class="btn btn-primary" value="edit">
And in your route use like this
Route::post('/posts/{id}', ...)

You are missing a / in your action
action="/posts/{{ $posts->id }}"

You could do the following :
<form action="{{ route('route.name', $post->id) }}" method="POST">
#csrf
<label for="">title</label>
<input type="text" name="title" class="form-control" >
<label for="">body</label>
<textarea type="text" name="body" class="form-control">{{$post->body}}</textarea>
<input type="submit" class="btn btn-primary" value="edit">
And for the route :
Route::post('/posts/{id}', 'Controller#function')->name('route.name');

I put a hidden method that I found on laravel documents and worked fine
<form action="/posts/{{$post->id}}" method="POST">
#csrf
<label for="">title</label>
<input type="text" name="title" class="form-control" >
<label for="">body</label>
<textarea type="text" name="body" class="form-control">{{$post->body}}.
</textarea>
<input type="submit" class="btn btn-primary" value="edit">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

Related

How to send multiple variable from blade to controller

<input type="date" class="form-control" name="from" value="{{ date('Y-m-d') }}">
<input type="date" class="form-control" name="to" value="{{ date('Y-m-d') }}">
i want to send 'from' and 'to' to controller from blade. how to write that in the tag. the necessary code is mentioned above. Thanks for your help in advance.
<form method="post" action="{{ URL("/totalsales/") }}">
<input type="datetime-local" class="form-control" name="from" >
<input type="datetime-local" class="form-control" name="to" >
<input type="submit" class = "btn btn-primary">
this is the method to send data in controller
your route should be post .
Use form:
<form method="post" action="{{ url("/totalsales/{{ }}") }}">
<input type="date" class="form-control" name="from" value="{{ date('Y-m-d') }}">
<input type="date" class="form-control" name="to" value="{{ date('Y-m-d') }}">
<input type="submit" value="Send">
</form>

Can't find the error in my route in Laravel

I'm using Laravel 5.6, and the following is my view (leads/show.blade.php):
<form method="post" id="student_form">
{{csrf_field()}}
<span id="form_output"></span>
<div class="form-group">
<label>Choose Group for Your Lead</label>
<select name="group_id" id="group_id" class="form-control">
#foreach($groups as $group)
<option value="{{$group->id}}"> {{$group->name}}</option>
#endforeach
</select>
<input type="hidden" name="customer_id" id="customer_id" value="{{$lead->id}}">
</div>
<div class="modal-footer">
<input type="hidden" name="student_id" id="student_id" value="" />
<input type="hidden" name="button_action" id="button_action" value="insert" />
<input type="submit" name="submit" id="action" value="Add" class="btn btn-info" />
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
and the route is :
Route::post('leads/savegroup', 'LeadsController#savegroup')->name('leads.savegroup');
Please help me to find the error in this route.
Add <form method="post" id="student_form" action="{{ url('/leads/savegroup') }}">
to your code.As you are posting data to empty route .You need to define some action.

Laravel update not working or not routing

I made an update form in laravel project to update some entries in my database but when i press submit button nothing is happening.i don't know why.tried everything.
here is the form:
<form action="{{ route('loans.update', $loan->id)}}" method="patch">
<p class="al-left">
{{csrf_field()}}
<label for="date">Date:</label>
<input type="date" name="date" value="{{$loan->data}}" id="date">
</p>
<p class="al-left">
<label for="name">Name:</label>
<input type="text" name="name" value="{{$loan->nume}}" id="name">
</p>
<p class="al-left">
<label for="period">Period(months):</label>
<input type="number" name="period" value="{{$loan->durata}}" id="period">
</p>
<p class="al-left">
<label for="month">Month Rate(euro):</label>
<input type="number" name="month" value="{{$loan->valoare_rata_luna}}" id="month">
</p>
<p class="al-left">
<label for="amount">Amount:</label>
<input type="number" name="amount" value="{{$loan->valoare_totala}}" id="amount">
</p>
<input type="submit" class="btn btn-sm btn-primary" name="submit" value="Edit">
<br>
<br>
</form>
here is the delete function in controller:
public function update(Request $request, $id){
$id = Auth::id();
$loan =loan::find($id);
$loan->cod_user=$id;
$loan->nume = $request->name;
$loan->data=$request->date;
$loan->durata=$request->period;
$loan->valoare_rata_luna=$request->month;
$loan->valoare_totala=$request->amount;
$loan->save();
return view("loans")->with('loans', $loans);
}
the routes:
Route::resource('/finance/loans','loanController');
To say that form action is in loans.blade.php
Form only supports GET and POST method. You have spoof the 'PATCH` method like:
<form action="{{ route('loans.update', $loan->id)}}" method="POST"> //**Here method = POST
<p class="al-left">
{{ csrf_field() }}
{{ method_field('PATCH') }} //**Here give PATCH by spoofing method
<label for="date">Date:</label>
<input type="date" name="date" value="{{$loan->data}}" id="date">
</p>
..
..
..
</form>
Hope it's helpful.
Route web.php
Route::resource('loans', 'LoansController');
<form action="{{ route('loans.update', $loan->id)}}" method="POST">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<input ... />
<input ... />
<input ... />
<input ... />
<input ... />
</form>

How to make two rows insert with one button Laravel using ajax

i want to make two inserts in same table.
The table is based on this fields (locale, project_id(foreign key), title, caption).
And the controller looks like here:
public function storeTranslation(Request $request)
{
$projecttranslation = new ProjectTranslation();
$projecttranslation->locale = $request->input("locale");
$projecttranslation->project_id = $request->input("project");
$projecttranslation->title = $request->input("title");
$projecttranslation->caption = $request->input("caption");
$projecttranslation->save();
}
The form for the moment looks like here:
<div id="form2" style="display:none;" class="col-md-6">
<div class="col-md-">
<h3>Crear nueva traduccion</h3>
<form enctype="multipart/form-data" id="myFormTraduccion" name="myFormTraduccion"><!--FIRST FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="Language">Language:</label>
<input type="text" id="locale" name="locale" value="en" class="form-control form-control-sm">
<label name="Project">Project id:</label>
<input type="number" id="project" name="project" class="form-control form-control-sm">
<label name="Title">Title:</label>
<input type="text" id="title" name="title" class="form-control form-control-sm">
<label name="Caption">Caption:</label>
<input type="text" id="caption" name="caption" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form> <!-- FIRST FORM TO TRANSLATE END HERE -->
<form enctype="multipart/form-data" id="myFormTraduccion2" name="myFormTraduccion2"> <!--SECOND FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="title">Language:</label>
<input type="text" id="locale" name="locale" value="es" disabled class="form-control form-control-sm">
<label name="order">Project id:</label>
<input type="number" id="project" name="project" class="form-control form-control-sm">
<label name="public">Title:</label>
<input type="text" id="title" name="title" class="form-control form-control-sm">
<label name="caption">Caption:</label>
<input type="text" id="caption" name="caption" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit2" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form> <!--SECOND FORM TO TRANSLATE END HERE -->
</div>
</div>
And the ajax look like this:
$("#createtranslatesubmit").click(function(){
$("#myFormTraduccion").submit();
});
$("#myFormTraduccion").submit(function(e){
e.preventDefault();
$.ajax({
url:'/admin/projects/postUploadTranslation',
type:'POST',
data:$('#myFormTraduccion').serializeArray(),
success: function(){
$("#form2").fadeOut(1000);
$("#form3").fadeIn(2000);
}
});
});
This create only with the first form, the first translation.
I think i should change the view code to this (Two same inputs for each field of database):
<div id="form2" style="display:none;" class="col-md-6">
<div class="col-md-">
<h3>Crear nueva traduccion</h3>
<form enctype="multipart/form-data" id="myFormTraduccion" name="myFormTraduccion"><!--FIRST FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="Language">Language:</label>
<input type="text" id="locale" name="locale" value="en" class="form-control form-control-sm">
<label name="Project">Project id:</label>
<input type="number" id="project" name="project" class="form-control form-control-sm">
<label name="Title">Title:</label>
<input type="text" id="title" name="title" class="form-control form-control-sm">
<label name="Caption">Caption:</label>
<input type="text" id="caption" name="caption" class="form-control form-control-sm">
<label name="title">Language:</label>
<input type="text" id="locale" name="locale" value="es" class="form-control form-control-sm">
<label name="order">Project id:</label>
<input type="number" id="project" name="project" class="form-control form-control-sm">
<label name="public">Title:</label>
<input type="text" id="title" name="title" class="form-control form-control-sm">
<label name="caption">Caption:</label>
<input type="text" id="caption" name="caption" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form> <!-- FIRST FORM TO TRANSLATE END HERE -->
</div>
</div>
That's correct? The problem to "store" the data, i think will be a foreach in controller.
And finally, i don't have any idea, how to pass the data in the ajax, with a formdata maybe?
Thanks a lot, any help will be appreciated!
When you submit a form, you send to the server the data of that specific form. Your approach of using multiple forms doesn't work here, because you want to send all data with only 1 specific form submit.
So you have to only create 1 form and separate the different translations with a numeric reference.
Your HTML (note the -0 and -1 used to separate id and name of each input element) :
<form enctype="multipart/form-data" id="myFormTraduccion" name="myFormTraduccion"><!--FIRST FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="Language">Language:</label>
<input type="text" id="locale-0" name="locale-0" value="en" class="form-control form-control-sm">
<label name="Project">Project id:</label>
<input type="number" id="project-0" name="project-0" class="form-control form-control-sm">
<label name="Title">Title:</label>
<input type="text" id="title-0" name="title-0" class="form-control form-control-sm">
<label name="Caption">Caption:</label>
<input type="text" id="caption-0" name="caption-0" class="form-control form-control-sm">
<label name="title">Language:</label>
<input type="text" id="locale-1" name="locale-1" value="es" class="form-control form-control-sm">
<label name="order">Project id:</label>
<input type="number" id="project-1" name="project-1" class="form-control form-control-sm">
<label name="public">Title:</label>
<input type="text" id="title-1" name="title-1" class="form-control form-control-sm">
<label name="caption">Caption:</label>
<input type="text" id="caption-1" name="caption-1" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form>
The controller:
public function storeTranslation(Request $request)
{
$projecttranslation0 = new ProjectTranslation();
$projecttranslation0->locale = $request->input("locale-0");
$projecttranslation0->project_id = $request->input("project-0");
$projecttranslation0->title = $request->input("title-0");
$projecttranslation0->caption = $request->input("caption-0");
$projecttranslation0->save();
$projecttranslation1 = new ProjectTranslation();
$projecttranslation1->locale = $request->input("locale-1");
$projecttranslation1->project_id = $request->input("project-1");
$projecttranslation1->title = $request->input("title-1");
$projecttranslation1->caption = $request->input("caption-1");
$projecttranslation1->save();
}
Of course, it can be easily generalized for N multiple transations and not only 2.
try this out:
<div id="form2" style="display:none;" class="col-md-6">
<div class="col-md-">
<h3>Crear nueva traduccion</h3>
<form enctype="multipart/form-data" id="myFormTraduccion" name="myFormTraduccion"><!--FIRST FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="Language">Language:</label>
<input type="text" id="locale" name="ProjectTranslation[0][locale]" value="en" class="form-control form-control-sm">
<label name="Project">Project id:</label>
<input type="number" id="project" name="ProjectTranslation[0][project]" class="form-control form-control-sm">
<label name="Title">Title:</label>
<input type="text" id="title" name="ProjectTranslation[0][title]" class="form-control form-control-sm">
<label name="Caption">Caption:</label>
<input type="text" id="caption" name="ProjectTranslation[0][caption]" class="form-control form-control-sm">
<label name="title">Language:</label>
<input type="text" id="locale" name="ProjectTranslation[1][locale]" value="es" class="form-control form-control-sm">
<label name="order">Project id:</label>
<input type="number" id="project" name="ProjectTranslation[1][project]" class="form-control form-control-sm">
<label name="public">Title:</label>
<input type="text" id="title" name="ProjectTranslation[1][title]" class="form-control form-control-sm">
<label name="caption">Caption:</label>
<input type="text" id="caption" name="ProjectTranslation[1][caption]" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form> <!-- FIRST FORM TO TRANSLATE END HERE -->
</div>
So you will get array of ProjectTranslation at controller side
Now at controller side
public function storeTranslation(Request $request)
{
$form_data = $request->get('ProjectTranslation');
foreach($form_data as $form){
$projecttranslation = ProjectTranslation::create($form);
$projecttranslation->save();
}
}

TokenMismatchException in VerifyCsrfToken.php line 53 in Laravel 5

I am using HTML pages for UI purpose in laravel and when I try to login then it shows me an error page "TokenMismatchException in VerifyCsrfToken.php line 53:".
I am getting this error while login as well as registration.
I have the login.html page as follows:
<div class="container">
<form method="POST" action="login_display" accept-charset="UTF-8">
<h1>Login</h1>
<hr>
<div class="form-group">
<label for="email">Email ID :</label>
<input class="form-control" name="email" type="text" id="email" placeholder="Please enter email here">
</div>
<div class="form-group">
<label for="password">Password :</label>
<input class="form-control" name="password" type="password" value="" id="password" placeholder="Please enter password here">
</div>
<div class="form-group">
<input class="btn btn-primary form-control" type="submit" value="Login">
</div>
</form>
</div>
Try adding, <input type="hidden" name="_token" value="{{ csrf_token() }}"> to your form. Laravel uses this token to check that the form submission was valid.
I tried by adding following line to the form:
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
and this time it works well and I didn't get the TokenMismatchException error afterwards as well.

Categories