This is my html form
<form class="form-horizontal" action="{{action('BlogController#update',[$blog->id]) }}" method="post">
<input name="method" type="hidden" value="patch"/>
<div class="form-group">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
Here is route:
Route::patch('blog/{id}','BlogController#update');
Controller :
public function update(Request $request,$id){
$input = $request->all();
$blog =findOrFail($id);
Blog::update($input);
//var_dump($input);
return back();
}
Can you please show me where is the issue?
In your code you have write $blog = findOrFail($id); to get blog which is not correct. You can do it using
$blog = Blog::findOrFail($id);
Now you have the blog, you need to update the blog. So, the update code should be
$blog->update($input);
To make this update method works, you need to make the fields(the fields you are updating) fillable in Blog model.
You're using the wrong syntax. Do something like this to make it work:
public function update(Request $request, $id)
{
Blog::where('id', $id)->update($request->all());
return back();
}
give the name whatever you wish say blog:
Route::patch('blog/{id}','BlogController#update')->name('blog');
your HTML code
<form class="form-horizontal" action="{{route('blog', $blog->id)}}" method="post">
hope this help you!!
you have many syntax problems!
try it this way:
form:
<form class="form-horizontal"
action="{{ route('blog.update', ['id' => $blog->id]) }}"
method="post">
{{ csrf_field() }}
<input name="_method" type="hidden" value="patch"/>
<!-- other inputs -->
</form>
Route:
Route::any('blog/{id}','BlogController#update')->name('blog.update');
Controller:
public function update(Request $request, $id){
$blog = Blog::findOrFail($id);
$blog->update([
'key' => 'value'
]);
// never use $request->all() because of security issues!
return back();
}
<form class="form-horizontal" action="{{route('blog.update',[$blog->id]) }}" method="post">
{{csrf_field()}}
{{ method_field('PATCH') }}
Your Route Like This
Route::resource('blog', 'BlogController');
Your Controller
public function update(Request $request,$id){
$blog =Blog::findOrFail($id);
$blog->database_fieldname1=$request->value1;
$blog->database_fieldname2=$request->value2;
$blog->database_fieldname3=$request->value3;
$blog->save();
return back();
}
Related
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.
This is what my code looks like
Route:
Route::put('/articles/{$article}', 'ArticlesController#update');
Controller:
public function update($id){
$article=Article::find($id);
$article ->title = request('title');
$article->excerpt=request('excerpt');
$article->body=request('body');
$article->save();
return redirect('/articles/'. $article->id);
}
Blade:
<form method="POST" action="/articles/{{$article->id}}" >
#csrf
#method('PUT')
And everytime I try to submit an update I get this:
The PATCH method is not supported for this route. Supported methods: GET, HEAD.
I'm currently stuck on this one.
Try this
<form action="/articles/{{$article->id}}" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
and the route
Route::put('/articles/{article}', 'ArticlesController#update');
It's better to do:
in routing
Route::put('/articles/{id}', 'ArticlesController#update')->name('articles.update');
in controller
public function update(Request $request, $id)
{
// logic
}
don't forget to use Request in controller
in blade
it's better to use naming for routes but it might be a problem in your action
<form method="POST" action="{{ route('articles.update', $article->id) }}">
simple way using blade
<form action="/articles/{{$article->id}}" method="POST">
#method('put')
#csrf
</form>
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');
I'm getting an error 404|Not found , while the route is existing and the debugger is on I have cleared route cachebut still showing the error, when I php artisan route:list it shows POST | products/{product}/favourites | product.fav.store | App\Http\Controllers\HomeController#store How can I solve this?
controller
public function store(Request $request, Product $product)
{
$request->user()->favouriteProducts()->syncWithoutDetaching([$product->id]);
return back();
}
Blade
<span class="pull-right">
Add to Fav
<form id="product-fav-form" class="hidden" action=" {{route('product.fav.store', $product) }}" method="POST">
{{ csrf_field()}}
</form>
</span>
Route
Route::post('/products/{product}/favourites', 'HomeController#store')->name('product.fav.store');
Try this ,
<form method="POST" action="{{ route('product.fav.store', ['product' => $product->id]) }}">
Change the controller function
public function store(Request $request,$product)
{
$request->user()->favouriteProducts()->syncWithoutDetaching($product);
return back();
}
You should change your route like.
<form action="{{ route('product.fav.store', ['product' => $product->id]) }}" >
Try this :
<form id="product-fav-form" class="hidden" action="{{route('product.fav.store', $product->name)}}" method="POST">
controller :
public function store(Request $request, $name) {
$request->user()->favouriteProducts()->syncWithoutDetaching($name);
return back();
}
I am using laravel 5.2. I want to print the database content that is stored in my database dynamically on the desired page. I tried but an error appears everytime i.e;( undefined variable:). I just want to print whatever content I store in my database table dynamically.
My code is here:
My model name is:gallery
My routes:
Route::get('/gallery/list' ,[
'uses'=>'gallerycontroller#viewgallerylist',
'as'=>'viewgallery'
]);
Route::post('/gallery/save' ,[
'uses'=>'gallerycontroller#savegallery',
'as'=>'savegallery'
]);
My controller:
public function viewgallerylist()
{
$galleries = gallery::all();
return view('gallery')->with('galleries', $galleries);
}
public function savegallery(Request $request)
{
$gallery1=$request['gallery_name'];
$gallery=new gallery();
$gallery->name=$gallery1;
$gallery->save();
return redirect()->route('viewgallery');
}
My desired page:
<form method="post" action="{{ route('savegallery') }}">
<input class="form-control" type="text" name="gallery_name">
<button type="submit" class="btn btn-primary" id="upl">Create+</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
#foreach($galleries as $gallery)
<p>{{ $gallery->name }}</p>
#endforeach
Most model Classes will have a capital letter. Are you sure your Model isn't called Gallery instead of gallery?
which means that you need to call Gallery::all() in your controller and make sure use App\Gallery; is at the top of your page.
public function viewgallerylist()
{
$galleries = Gallery::all();
return view('gallery')->with('galleries', $galleries);
}
The problem might be with your model.... please provide a larger view
Route::get('/' ,[
'uses'=>'gallerycontroller#viewgallerylist',
'as'=>'viewgallery'
]);
Route::post('/' ,[
'uses'=>'gallerycontroller#savegallery',
'as'=>'savegallery'
]);