Laravel 5.1 url value not passing to controller PUT method - php

I have a simple form for updating book info.
<form action="{{ action('BookController#update') }}" method="POST" class="form-horizontal">
<input type="text" id="title" class="form-control" name="title" placeholder="title" value="{{ $book[0]->title }}">
<input type="text" id="author" class="form-control" name="author" placeholder="author" value="{{ $book[0]->author }}">
......................
<button type="submit" class="btn btn-primary">Save</button>
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
</form>
Controller:
public function update(Request $request, $id)
{
$book = new Book;
$title = $request->input('title');
$author = $request->input('author');
$category = $request->input('category');
$date = $request->input('date');
if ($book->updateBook($id, $title, $author, $category, $date)) {
return redirect('books')->with('status', 'Successfuly edited!');
}
else {
return dd($id);
}
}
The problem is, it doesn't pass the right $id. It pass a string {books}
Basically $id = "{books}"
It should be an integer (31) from url /books/31/edit
In routes is defined as a resource with all available default methods
What can i do?

You need to pass the book's ID in the form definition, as the second argument to action():
<form action="{{ action('BookController#update', ['id' => $book[0]->id]) }}" method="POST" class="form-horizontal">
See the definition of action() for more information.

The solution was very simple: i just needed to pass $id to the view and then pass it to form action as #EricMakesStuff suggested..

try this
<form action="{{ action('BookController#update', ['id'=> 1]) }}" method="POST" class="form-horizontal">

Related

Laravel 6 404 Not found but route exists

Getting 404 Not found though route exists, the following codes worked perfectly on Laravel 8 but on 6 produces 404.
Route:
// Content Packs
Route::delete('content-packs/destroy', 'ContentPacksController#massDestroy')->name('content-packs.massDestroy');
Route::patch('content-packs/{content-pack}/clone_pack', 'ContentPacksController#clone_pack')->name('content-packs.clone_pack');
Route::resource('content-packs', 'ContentPacksController');
Button:
<form action="{{ route('admin.content-packs.clone_pack', $contentPack->id) }}" method="POST" onsubmit="return confirm('{{ trans('cruds.contentPack.clone_confirmation') }}');" style="display: inline-block;">
<input type="hidden" name="_method" value="PATCH">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="btn btn-xs btn-warning" value="{{ trans('cruds.contentPack.clone') }}">
</form>
Controller method:
public function clone_pack(Request $request, ContentPack $contentPack)
{
$contentPack = ContentPack::where('id', $request->id)->first();
$newPack = $contentPack->replicate();
$newPack->created_at = Carbon::now();
$newPack->save();
return back();
}
What am I missing?
Changing route to this fixed the issue:
Route::get('content-packs/content-pack/{id}', 'ContentPacksController#clone_pack')->name('content-packs.clone_pack');

PUT method not supported for this route. Supported methods: GET, HEAD, POST Laravel 6

I am using Laravel 6 to make an edit form. Apparently, this is a prevalent problem, and I looked up here how can I solve it, I tried putting hidden csrf fields in 5 ways and I run with the same error every time, so IDK if those solutions are deprecated for Laravel 6 or I am doing something wrong.
edit.blade.php
<form method="POST" action="/posts/{{$post->edit}}" enctype="multipart/form-data">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label for="exampleFormControlInput1">Email address</label>
<input type="email" name="email" value="{{ $post->email }}" class="form-control"
id="exampleFormControlInput1">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Name</label>
<input type="text" name="name" value="{{ $post->name }}" class="form-control"
id="exampleFormControlInput2" placeholder="Name">
</div>
<label for="exampleFormControlInput1">Image</label>
<div class="form-group row">
<div class="col-sm-2">
#if($post->image)
<img class="img-fluid card-img-top" src="/images/{{ $post->image }}"/>
#endif
</div>
<input type="file" name="image" value="{{ $post->image }}"
id="exampleFormControlInput3">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
PostsController.php
public function edit(Post $post)
{
return view ('posts.edit', compact('post'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Post $post)
{
$post->update($request->all());
$post->name = $request->name;
$post->email = $request->email;
if(Input::hasFile('image')){
$file = Input::file('image');
$path = time().$file->getClientOriginalName();
$destinationPath = public_path(). '/images/';
$filename = time().$file->getClientOriginalName();
$file->move($destinationPath, $filename);
//then proceeded to save
$post->image = $destinationPath.$filename;
$post->save();
}
else
$post->save();
return redirect('posts.all');
}
My routes, in case it's necessary
Route::resource('posts', 'PostsController');
These are the other ways I tried to write the csrf field.
Way 1:
{{ csrf_field() }}
<input type="hidden" name="_method" value="PUT">
Way 2:
#csrf_field
{{ method_field('PUT') }}
Way 3:
#csrf
{{ method_field('PATCH') }}
Way 4:
#csrf
#method('PUT')
All of these lead me to the same error message.
try to replace this :
<form method="POST" action="/posts/{{$post->edit}}"
with this :
<form method="POST" action="{{ route('posts.update', [$post->id]) }}"
I think that you are trying to Post on the /posts/ route without any arguments as $post->edit might not return the id of the post :)

Can't update user in laravel

I want some help with my code.
I try to update user data but it's not update anything.
User Name, Email, Posisson, Image.
Any help please.
My Route :
I used URL because route didn't work.
Route::get('editusers/{id}','UsersController#update');
My Controller:
public function edit($id)
{
$editusers=User::findOrFail($id);
return view('admin.users.EditUser', compact('editusers'));
}
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required',
'posission' => 'required',
]);
$useredit = User::find($id);
$useredit->name = $request->input('name');
$useredit->email = $request->input('email');
$useredit->posission = $request->input('posission');
if($request->hasFile('file'))
{
$file = $request->file('file');
$filename = time().'.'.$file->getClientOriginalExtension();
Image::make($file)->resize(150, 150)->save(public_path('/admin/images/'.$filename));
$useredit->UserImg = $filename;
}
$useredit->save();
return redirect()->back();
}
HTML :
<form class="" action="{{url('editusers',Auth::user()->id)}}" role="form" enctype="multipart/form-data">
{!! csrf_field() !!}
<input type="hidden" name="_method" value="PUT">
<label>Edit Your Profile :</label>
<div class="form-group">
<label>Name :</label>
<input class="form-control" value="{{$editusers->name}}" name="Name">
</div>
<div class="form-group">
<label>Email :</label>
<input class="form-control" value="{{$editusers->email}}" name="email">
</div>
<div class="form-group">
<label>Posisson :</label>
<input class="form-control" value="{{$editusers->posission}}" name="posission">
</div>
<div class="form-group">
<label>Image :</label>
<img src="{{ asset('admin') }}/images/{{$editusers->UserImg}}" alt="avatar" class="img-circle" style="max-height: 100px;">
<input type="file" id="file" name="file"/>
</div>
<input class="btn btn-success btn-mini deleteRecord" type="submit" name="submit" value="Update">
What I expect is that it updates my database.
As your form has
<form class="" action="{{url('editusers',Auth::user()->id)}}" role="form" enctype="multipart/form-data" method="POST">
{!! csrf_field() !!}
<input type="hidden" name="_method" value="PUT">
so your route must have put(),
so it should be Route::put('editusers/{id}','UsersController#update');
also you can use #method('PUT') instead of <input type="hidden" name="_method" value="PUT"> and #csrf instead of {!! csrf_field() !!}
either change $useredit->name = $request->input('name'); to $useredit->name = $request->input('Name'); or in form
<input class="form-control" value="{{$editusers->name}}" name="Name"> to
<input class="form-control" value="{{$editusers->name}}" name="name">
Your route is wrong
Route::get('editusers/{id}','UsersController#update');
it supposed to be PUT
Route::put('editusers/{id}','UsersController#update');
The problem because you put wrong method at your route. Change it
// From
Route::get('editusers/{id}', 'UsersController#update')
// To
Route::put('editusers/{id}', 'UsersController#update')
Anyways, you should change your route to be standard. It should be:
//To show data you should use:
Route::get('users/edit/{id}', 'UsersController#show');
//To update user data.
Route::put('users/edit', 'UsersController#update');
i think you should use resources route to solve this:
Route::resource('editusers','UserController');
but first you need to run this command
php artisan make:controller UserController --resource

how to fix Route [projects/{projectId}/task/{taskId}/subtask] not defined in Laravel

I am developing application using laravel 5.2 and I have form.blade.php file in my subtasks folder in view folder.
subtasks/form.blade.php
<form class="form-vertical" role="form" method="post" action="{{ route('projects/{projectId}/task/{taskId}/subtask')}}">
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<input type="text" name="task_name" class="form-control" id="name" value="{{ old('task_name') ?: '' }}">
#if ($errors->has('task_name'))
<span class="help-block">{{ $errors->first('task_name') }}</span>
#endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Create Task</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
My controller is this
public function store(Request $request,$projectId,$taskId)
{
$subtask = new Subtask;
$subtask->subtask_name = $request->input('task_name');
$subtask->task_id = $taskId;
$subtask->project_id = $projectId;
$subtask->save();
//
}
my routes are here
Route::get('projects/{projectId}/task/{taskId}/subtask', function ($projectId, $taskId) {
return view('subtasks/form',['projectId'=>$projectId,'taskId'=>$taskId]);
});
Route::post('projects/{projectId}/task/{taskId}/subtask','SubtasksController#store');
but I got this error messages
Route [projects/{projectId}/task/{taskId}/subtask] not defined.
how can fix this?
You have two options: name your route:
Route::post('projects/{projectId}/task/{taskId}/subtask','SubtasksController#store')
->name('subtask_route);
and your form would use this. Remember to pass in the correct projectId and taskId
<form class="form-vertical" role="form" method="post" action="{{ route('subtask_route', ['projectId'=> $projectId, 'taskId'=>$taskId])}}">
Or create the url yourself:
<form class="form-vertical" role="form" method="post" action="/projects/{{$projectId}}/task/{{$taskId}}/subtask">

How to post to a database from a form

How to post the contents of a form to db? I know the database works.
<form method="post" action="{{ action('UserController#registration') }}">
<h1>Please Name</h1>
<textarea name="name"></textarea>
<button type="submit">Access</button>
</form>
Route::post('/registration', 'UserController#registration');
public function registration(Request $request)
{
DB::table(‘user2s’)->insert([‘name’=> $request->name]);
return 'success';
}
Add this token in your form
{!! Form::token() !!}
or this
<input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />

Categories