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
Related
I'm trying to update my data with Laravel. I'm able to create, read, delete the data but somehow i cannot update my data. I already checked my controller,model,route and view but i don't think there's any typo or anything. It only redirects to it's index page without being updated although i have entered new input. There's no error message at all so it makes me more confused of what's wrong because i really don't know why.
Route for edit and update
Route::get('contact/{contact}/edit', 'ContactController#edit');
Route::post('contact/update','ContactController#update');
Controller with edit and update function
use Illuminate\Http\Request;
use App\Contact;
use DB;
public function edit($kode_kontak){
$contact = DB::table('contact')->where('kode_kontak',$kode_kontak)->get();
return view('contact.edit',['contact' => $contact]);
}
public function update(Request $request){
DB::table('contact')->where('kode_kontak',$request->kode_kontak)->update([
'email' => $request->email,
'telepon' => $request->telepon,
]);
return redirect('contact');
}
Model
class Contact extends Model
{
public $timestamps = false;
protected $table = 'contact';
protected $fillable = [
'kode_kontak',
'kode_pegawai',
'email',
'telepon'
];
protected $primaryKey = 'kode_kontak';
}
View of edit.blade.php
<div id="contact">
<h2>Edit Contact</h2>
#foreach($contact as $p)
<form action="/contact/update" method="POST">
#csrf
<div class="form-group">
<label for="kode_contact" class="control-label">Kode Kontak</label>
<input type="text" name="kode_kontak" id="kode_kontak" class="form-control" value="{{ $p->kode_kontak}}" disabled>
</div>
<div class="form-group">
<label for="kode_pegawai" class="control-label">Kode Pegawai</label>
<input type="text" name="kode_pegawai" id="kode_pegawai" class="form-control" value="{{ $p->kode_pegawai}}" disabled>
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="text" name="email" id="email" class="form-control" value="{{ $p->email}}">
</div>
<div class="form-group">
<label for="telepon" class="control-label">Telepon</label>
<input type="text" name="telepon" id="telepon" class="form-control" value="{{ $p->telepon}}">
</div>
<div class="form-group">
<input class="btn btn-primary form-control" type="submit" value="Simpan">
</div>
</form>
#endforeach
</div>
Your update route is using the wrong "verb" and url. If you take a look at Laravel's Resource Controllers you can see the different actions and route names available for editing, updating, deleting etc. when creating a "CRUD" controller.
You can see the route for an "update" action and its "verb".
Change your routes to
Route::get('contact/{contact}/edit', 'ContactController#edit')->name('contact.edit');
Route::patch('contact/{contact}','ContactController#update')->name('contact.update');
Or if you want to add a complete CRUD controller use the short form:
Route::resource('contact', 'ContactController');
This will create all required routes in one convenient line of code. Use php artisan route:list to check all routes.
HTTP forms only support GET and POST methods, Laravel uses #method() in blade to add the other verbs (put,patch,delete):
Edit:
your form uses disabled attributes on some <input>s. Those values will not be sent along with your request. Here's an updated edit.blade.php:
disabled attributes have been swapped with readonly.
The action uses Laravel's RouteModelBinding
Removed the #foreach since you only have one item to be edited
edit.blade.php:
<div id="contact">
<h2>Edit Contact</h2>
<form action="{{ route('contact.update', ['contact' => $contact]) }}" method="POST">
#csrf
#method('patch')
<div class="form-group">
<label for="kode_contact" class="control-label">Kode Kontak</label>
<input type="text" name="kode_kontak" id="kode_kontak" class="form-control" value="{{ $contact->kode_kontak}}" readonly>
</div>
<div class="form-group">
<label for="kode_pegawai" class="control-label">Kode Pegawai</label>
<input type="text" name="kode_pegawai" id="kode_pegawai" class="form-control" value="{{ $contact->kode_pegawai}}" readonly>
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="text" name="email" id="email" class="form-control" value="{{ $contact->email}}">
</div>
<div class="form-group">
<label for="telepon" class="control-label">Telepon</label>
<input type="text" name="telepon" id="telepon" class="form-control" value="{{ $contact->telepon}}">
</div>
<div class="form-group">
<input class="btn btn-primary form-control" type="submit" value="Simpan">
</div>
</form>
</div>
Since it's using RouteModelBinding you can change your update() method to:
public function update(Request $request, Contact $contact)
{
$contact->update([
'email' => $request->email,
'telepon' => $request->telepon,
]);
return redirect('contact');
}
Laravel will know what Contact $contact is
you loop your contact to produce a form for each one of them with the same input name, id,... which is the wrong approach.
my suggestion to you :
Route for edit and update
Route::get('contact/{contact}/edit', 'ContactController#edit');
Route::post('contact/{contact}/edit', 'ContactController#update');
Controller with edit and update function
public function edit(Contact $contact){
return view('contact.edit',compact('contact'));
}
public function update(Request $request,Contact $contact){
$contact->update([
'email' => $request->email,
'telepon' => $request->telepon,
]);
return redirect('contact');
}
and of course, you will update edit.blade.php
<div id="contact">
<h2>Edit Contact</h2>
#foreach($contact as $p)
<form action="/contact/{{ $p->id }}/update" method="POST">
#csrf
<div class="form-group">
<label for="kode_contact" class="control-label">Kode Kontak</label>
<input type="text" name="kode_kontak" class="form-control" value="{{ $p->kode_kontak}}" disabled>
</div>
<div class="form-group">
<label for="kode_pegawai" class="control-label">Kode Pegawai</label>
<input type="text" name="kode_pegawai" class="form-control" value="{{ $p->kode_pegawai}}" disabled>
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="text" name="email" class="form-control" value="{{ $p->email}}">
</div>
<div class="form-group">
<label for="telepon" class="control-label">Telepon</label>
<input type="text" name="telepon" class="form-control" value="{{ $p->telepon}}">
</div>
<div class="form-group">
<input class="btn btn-primary form-control" type="submit" value="Simpan">
</div>
</form>
#endforeach
</div>
i'm working on a crud app to learn laravel i'm doing good so far , other than when i want to update a post it gives me this
put method is not supported for this route
#extends ('layouts.app')
#section('content')
<form action="{{route('update_blog_path',['blog'=>$blog->id])}}" method="POST">
#method('PUT')
#csrf
<div class="form-group">
<label for="title">Title </label>
<input type="text" name="title" class="form-control" value={{$blog->title}}>
</div>
<div class="form-group">
<label for="title">Content </label>
<input type="text" name="content" class="form-control" value={{$blog->content}}>
</div>
<div class="form-group">
<button type="submit" class="btn btn-outline-primary">Edit</button>
</div>
</form>
#endsection
<?php
Route::get('/', function () {
return view('welcome');
});
Route::name('blogs_path')->get('/blogs','BlogController#index');
Route::name('create_blog_path')->get('/blogs/create','BlogController#create');
Route::name('store_blog_path')->post('/blogs','BlogController#store');
Route::name('blogs_path1')->get('/blogs/{id}','BlogController#show');
Route::name('edit_blog_path')->get('/blogs/{id}/edit','BlogController#edit');
Route::name('update_blog_path')->put('/blogs/{id}','BlogController#updtae');
Try please;
#extends ('layouts.app')
#section('content')
<form action="{{ route('update_blog_path', ['blog' => $blog->id]) }}" method="POST">
#csrf
{{ mehod_field("PUT") }}
<div class="form-group">
<label for="title">Title </label>
<input type="text" name="title" class="form-control" value={{$blog->title}}>
</div>
<div class="form-group">
<label for="title">Content </label>
<input type="text" name="content" class="form-control" value={{$blog->content}}>
</div>
<div class="form-group">
<button type="submit" class="btn btn-outline-primary">Edit</button>
</div>
</form>
#endsection
and
Route
<?php
Route::put('update_blog_path/{blog}', 'BlogController#update')->name("update_blog_path");
And your'e code wrong update term
Route::name('update_blog_path')->put('/blogs/{id}','BlogController#updtae');
change update
Looks like you have a typo in your routes file, change
Route::name('update_blog_path')->put('/blogs/{id}','BlogController#updtae');
to
Route::name('update_blog_path')->put('/blogs/{id}','BlogController#update');
You misspelled the method name update.
I'm creating a laravel application that allows users to create a blog post.
I have created a PostsController as a resource with store function like this:
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required'
]);
return 123;
}
Also, I added a route in web.php
Route::resource('posts', 'PostsController');
If I list the routes with php artisan php artisan show:routes, POST method is listed:
The HTML form looks like this:
<form action="PostsController#store" method="POST">
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3"></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
When I submit the form, I get MethodNotAllowedHttpException:
The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE.
I used to use laravel collective for forms before. Haven't done any work in laravel for a while and now it seems to be deprecated (https://laravelcollective.com/), so I resorted to classic HTML form. How do I work around this?
Your action is incorrect within your form - you need to point the action to the URL of the route, and then the route will select the method, in this case, the 'store' method. Also add #csrf for more information CSRF Protection
<form action="{{ route('posts.store') }}" method="POST">
#csrf
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3" name="body"></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
add the name in textbox and textarea
form action="{{ route('posts.store') }}" method="POST">
#csrf
<div class="form-group">
<label for="title">Title</label>
<input class="form-control" type="text" id="title" name="title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" id="body" rows="3" name="body"></textarea>
</div>
<input type="submit" class="btn btn-primary">
</form>
i want to pre fill my html form field "Reference" from url value for referne is afer ref?=1234 how to do it with laravel
<form method="post" accept-charset="utf-8" class="block" enctype="multipart/form-data" action="{{ route('register') }}">
{!! csrf_field() !!}
<div class="row">
<div class="col-md-12 col-sm-12">
<div style="width: 100%" class="alert alert-warning">
Please Register With a Reference User.
</div>
</div>
</div>
<div class="input-group">
<input type="text" name="reference" value="HERE WANT TO SHOW REFEERENCE CODE FROM URL" required class="form-control" placeholder="Enter Reference ID *" aria-describedby="basic-addon1">
<span class="input-group-addon" id="basic-addon1"><i class="fa fa-handshake-o"></i></span>
</div>
You can use like:
<input type="text" name="reference" value="{{ app('request')->input('ref') }}" required class="form-control" placeholder="Enter Reference ID *" aria-describedby="basic-addon1">
Use request() helper:
$ref = request('ref'); // It will return 1234.
There isn't a $GET variable in PHP.
There is a $_GET.
In laravel, you may use the Request facade or Input facade
so here:
<input value="view{{ \Request::get('ref') }}" type="text" name="reference" required class="form-control" placeholder="Enter Reference ID *" aria-describedby="basic-addon1">
// http://localhost/codec/register?ref=g5M089zQeVzc
// will produce
// <input value="viewg5M089zQeVzc" ...
I'm just trying to insert title and body using eloquent on database and MethodNotAllowedHttpException appears wont let me do it cause of that error
Here's the controller
public function store(Request $request)
{
$this->validate($request, array(
'title' => 'required|max:255',
'body' => 'required'
));
$post = new Post;
$post->title = $request->title;
$post->body = $request->body;
$post->save();
return redirect()->route('posts.show', $post->id);
}
Here's my view create.blade.php
<form action="post.store" method="POST">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title" placeholder="Enter title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" name="body" rows="3"></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success btn pull-right" value="post">
</div>
</form>
i am assuming your are using resource route.
i think this <form action="post.store" method="POST"> should be <form action="{{ route('post.store') }}" method="POST"> and use this too {{ csrf_field() }} inside form otherwise you will get tokenmismatch error.
<form action="{{ route('posts.store') }}" method="POST">
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title" placeholder="Enter title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea class="form-control" name="body" rows="3"></textarea>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success btn pull-right" value="post">
</div>
</form>
You may use command php artisan route:list and in column Name you will see route name and you will be able to call in view by {{ route('routename') }}
Example:
In route list is shown posts.store, so you will call in view {{ route('posts.store') }}
I think you may call incorrect route by above route view is posts.show but when insert you called post.store maybe posts.store
use in form action {{ route('posts.store') }}