Laravel update not working or not routing - php

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>

Related

Laravel: Too few arguments to function 0 passed and 1 expected

I am currently working on a 1 page crud for a laravel project. I had a lot of trouble to begin with, but now the last error i have is
Too few arguments to function 0 passed and 1 expected
this happens on loading the main page of the crud. this file gives the error:
edit.blade.php
<form method="post" action="{{ route('admin.user.update', $userEdit->id) }}">
#method('PATCH')
#csrf
<div class="form-group">
<label for="name"> Name:</label>
<input type="text" class="form-control" name="name" value="{{ $userEdit->name }}" />
</div>
<div class="form-group">
<label for="email">email </label>
<input type="email" class="form-control" name="email" value="{{ $userEdit->email }}" />
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
controller index:
public function index($id)
{
$users = User::all();
$userEdit = User::find($id);
return view('admin.user.index', compact('users', 'userEdit'));
}
yes i need $users and $userEdit, but i cant reach $userEdit on the first load of the page. normally there is also no $id in index() but i added it to try to fix it, this did not work
You need to pass the parameter in as an array with a key specifying the route's parameter name. See https://laravel.com/docs/9.x/routing#generating-urls-to-named-routes
<form method="post" action="{{ route('admin.user.update', ['id' => $userEdit->id]) }}">
you have a error in your form:
<form method="post" action="{{ route('admin.user.update', $userEdit->id) }}">
#csrf
#method('PUT')
<div class="form-group">
<label for="name"> Name:</label>
<input type="text" class="form-control" name="name" value="{{ $userEdit->name }}" />
</div>
<div class="form-group">
<label for="email">email </label>
<input type="email" class="form-control" name="email" value="{{ $userEdit->email }}" />
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
method not it´s PATCH it´s PUT and firts send your CSRF

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>

put method not supported in laravel

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>

laravel throwing MethodNotAllowedHttpException for validation

I tried to create a form with post method. When there was validation in the method, I faced with "Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException" , but when i deleted validation it worked correctly. I don't know why!
HTML
#if(count($errors)>0)
<ul>
#foreach($errors->all() as $error)
<li style="color: #cc5200;"><i class="fas fa-times"></i>{{$error}}</li>
#endforeach
</ul>
#endif
<form method="post" action="{{route('test.added')}}">
#csrf
<input type="hidden" name="grade" value="{{$grade}}">
<input type="hidden" name="course" value="{{$course}}">
<input type="hidden" name="numbers" value="{{$numbers}}">
#for($i=0;$i<$numbers;$i++)
<div>
<span style="color: #cc5200">question{{$i+1}} :</span>
<textarea name="question[]" rows="2" style="width: 50vw">{{old('question')[$i]}}</textarea>
</div>
<div class="d-flex flex-row">
<div class="ml-1 mr-2">
<span>1-</span>
<input type="text" name="firstOption[]" style="width: 10vw" value="{{old('firstOption')[$i]}}">
<input type="radio" name="trueOption[{{$i}}]" value="1">
</div>
<div class="ml-1 mr-2">
<span>2-</span>
<input type="text" name="secondOption[]" style="width: 10vw" value="{{old('secondOption')[$i]}}">
<input type="radio" name="trueOption[{{$i}}]" value="2">
</div>
<div class="ml-1 mr-2">
<span>3-</span>
<input type="text" name="thirdOption[]" style="width: 10vw" value="{{old('thirdOption')[$i]}}">
<input type="radio" name="trueOption[{{$i}}]" value="3">
</div>
<div class="ml-1 mr-2">
<span>4-</span>
<input type="text" name="forthOption[]" style="width: 10vw" value="{{old('forthOption')[$i]}}">
<input type="radio" name="trueOption[{{$i}}]" value="4">
</div>
</div>
<br>
#endfor
<input type="submit" value="ok" style="float: left;color: white;background: #cc5200">
</form>
web.php
Route::post('/td-admin/test/add-finalStage','TestController#added')->name('test.added');
TestController
public function added(Request $request){
$request->validate([
'question.*'=>'required',
'firstOption.*'=>'required',
'secondOption.*'=>'required',
'thirdOption.*'=>'required',
'forthOption.*'=>'required',
'trueOption.*'=>'required',
]);
Test::create( ...
It made me crazy. please help me.

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.

Categories