When i want to insert data in the database i get this error
MethodNotAllowedHttpException in RouteCollection.php line 219
I'm use resource controller
this is my form
<form action="library" method="POST" enctype="multipart/form-data">
{!! csrf_field() !!}
Enter the name of section: <input type="text" name="section_name"> <br>
Upload an image: <input type="file" name="image"> <br>
<button type="submit" class="btn btn-default">Create Section</button>
</form>
and this is my store function
public function store(Request $request)
{
$section_name = $request->input('section_name');
$file = $request->file('image');
$destenationPath = 'iamges';
$filename = $file->getClientOriginalName();
$file->move($destenationPath, $filename);
DB::table('sections')->insert(['section_name' => $section_name, 'image_name' => $filename]);
return redirect('admin');
}
and this my Route
Route::resource('library', 'Main');
You are using action="library", so the form is submitted to library. But, here is nothing to deal with library. You need to submit the form to store() method in Mian controller.
Change action="library" to action="{{ action('Main#store') }}" in form starting tag.
add this route='library.store' to your form:
<form method="POST" route="library.store" enctype="multipart/form-data" files="true">
and your rout should be:
Route::resource('library', 'controller_class_name');
Change you route to:
Route::resource('library', 'MainController');
Also, check out your controller. It should be placed into app\Http\Controllers directory, named MainController.php and it should include this code:
class MainController extends Controller
{
....
public function store(Request $request)
....
}
Related
Hi im new to laravel 9 and im trying to upload file from view to controller, but somehow the input file didnt pass the file to controller.
im using laravel 9 with boostrap in it.
heres my code :
view
<form enctype="multipart/form-data" action="{{ route('profile.put') }}" method="POST">
#csrf
#method('POST')
<div class="card-body">
<input id="file" name="file" type="file">
<button type="submit" class="btn btn-block btn-primary">{{ __('Save') }}</button>
</div>
</form>
controller
public function put(Request $request){
return $request;
}
i try to see the $request variable but the result is like this
enter image description here
i try the solution like add enctype="multipart/form-data" but still didnt work.
thank you in advance
I think you simply should get the request by its name or using `$request->all()`, you can do these two things:
In your controller you should write your put method like below:
public function put(Request $request){
$file = '';
if($request->file('file'))
{
$file = $request->get('file);
}
return $file;
}
or you can use the below code in your put method
public function put(Request $request){
return $request->all();
}
Note: If you don't send the file as a JSON Response you should return it to view after saving operation;
and also, there is no need to put #method('POST') inside the form;
my Route :
Route::resource('/posts',PostsControllerWithAll::class);
my form :
i try to read the given data from this form
<form method="POST" action="{{ action('PostsControllerWithAll#store') }}">
#csrf
<input type="text" name="title" placeholder="enter title">
<input type="submit" name="submit">
</form>
my controller :
public function create()
{
return view("posts.create");
}
public function store(Request $request)
{
return $request->all();
}
and this is my route list
route list image , please check the link image
Try using the named route, like you show on your print screen.
<form method="POST" action="{{ action('posts.store') }}">...</form>
To validated the fields values on store method, use dd($request->all()) this dump the variable and die, it's good to see what is expected.
i just started laravel and i am trying to create a simple post form for practive, when submitted it gives an 419 Error so I used #csrf inside the form.
HTML Form:
<form method="POST" action="/posts">
#csrf
<input type="text" name="title" placeholder="Enter Title">
<input type="submit" name="submit">
</form>
Route:
Route::resource('/posts', 'PostController');
Store Function:
public function store(Request $request)
{
//
$post = new Post;
...
$post->save();
return redirect('/posts'); //Tried
return redirect()->route('posts.index');
}
#csrf fixed the 419 error and made the post work.
The Post works only when there is no redirect(). I think the #csrf is making the redirect() not work.
I've been searching and couldn't find a solution. How do I fix this?
make action="{{ route('posts.store') }}" instead of action="/posts"
make return redirect()->route('posts.index');
Using Laravel 5.4, I'm tring to setup a form where a user can enter a food name and its image. While trying to upload a PNG or JPG image, I get the following Validation Error:
The image must be an image.
If I remove the validation for image, I get a FatalErrorException:
Call to a member function getClientOriginalExtension() on null
which is a helper function provided by Intervention/Image library. This could mean that the image didn't upload at all.
FORM
<form action="/hq/foods" method="POST">
{{ csrf_field() }}
<input type="text" name="name">
<input type="file" name="image">
<button type="submit" class="btn btn-success ">
ADD FOOD ITEM
</button>
</form>
CONTROLLER
public function store(Request $request) {
$this->validate($request, [
'name' => 'required|min:2|max:255',
'image' => 'required|image'
]);
$food_category = FoodCategory::find($request->food_category_id);
$food = new Food;
$food->name = $request->name;
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('img/foods/' . $filename);
Image::make($image)->resize(800, 400)->save($location);
$food->image = $filename;
$food->save();
Session::flash('success',
'
<h4>Success!</h4>
<p>The food item has been added to the Menu.</p>
');
return back();
}
What am I missing?
To upload images you need to add:
enctype="multipart/form-data"
to your form so instead of:
<form action="/hq/foods" method="POST">
you should use
<form action="/hq/foods" method="POST" enctype="multipart/form-data">
If you use Laravelcollective
{!! Form::open(['method'=>'post','files' => true,'url'=>'/hq/foods']) !!}
OR
Using HTML
<form action="/hq/foods" method="POST" enctype="multipart/form-data">
you need to add enctype="multipart/form-data" in your from
I'm trying to transfer data from the form to overwrite the content database .But get the error.
It is my route,method and form.
Route::get('edit/{id}','JokeController#edit');
public function edit(JokeModerateRequest $request,$id) {
$joke = Joke::findOrFail($id);
return view('jokes.edit', [ 'joke' => $joke ]);
}
<form action="/update" method="post">
<input type="text" value="{{ $joke -> content}}" name="body">
<input type="submit" value="Save">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
But when I try use next route and method
Route::post('update/{id}','JokeController#update');
function update(JokeModerateRequest $request,$id)
$joke = Joke::findOrFail($id);
$joke->content = $request -> get('content');
$joke->save();
return back();
I have next error
Sorry, the page you are looking for could not be found.
1/1
NotFoundHttpException in RouteCollection.php line 145:
The problem lies here:
<form action="/update" method="post">
This will redirect to /update route (which you haven't defined) instead of /update/id. Those two are different routes.
To fix this, change action of the form to:
<form action="/update/{{ $joke->id }}" method="post">