Multiple forms in one page in Laravel 5.1 - php

I have one blade page has one form to update and another one to save
my question How i can submit both according to method type
I tried to achieve that like the following example
public function postCompanyProfileSettings(Request $request)
{
if($request->isMethod('POST')) {
// do something to save
}
if($request->isMethod('PUT')) {
// do something to update
}
}
it's working well with POST method but with PUT return Route Exception MethodNotAllowedHttpException in RouteCollection.php line 219:
I think that the issue in routs.php but i don't know what exactly to do to handle one route for multiple forms (multiple methods)
My route in route.php file
//setting routes...
get('/home/settings', 'CompanyProfileController#getCompanyProfileSettings');
post('/home/settings','CompanyProfileController#postCompanyProfileSettings');
Do there is any way to achieve that?

Alternatively you could use a hidden input
public function postCompanyProfileSettings(Request $request)
{
if(isset($request->get('update')) {
// do something to update
}
// do something to save
}
And routes..
post('/home/settings','CompanyProfileController#postCompanyProfileSettings');

In my opinion i'll use the same method which is POST.
1st form:
<form method="POST" action={{ url('vault/{batch_centre_id}/candidates/{id}', ['form' => '1']) }}>
2nd form:
<form method="POST" action={{ url('vault/{batch_centre_id}/candidates/{id}', ['form' => '2']) }}>
in your action check form:
if ($request->get('form') == 1) {
return $request->get('form');
} else if ($request->get('form') == 2) {
return $request->get('form');
}
return result;
So from the above you can have unlimited forms on a single page so long you tag your forms and verify them from your controller.
Check this answer

Related

How to encrypt id in URL laravel

I want to encrypt the id in URL I'll show my controller code and route. I've already used Crypt::encrypt($id); in my controller but it's not working properly so I've commented that line in my controller
this is my controller
public function update(TenderRequest $request,$id){
$tender = TenderMaster::findOrFail($id);
//Crypt::encrypt($id);
if($request->extend_date < $request->end_date || $request->bid_status > 0){
return 'unsuccess';
} else{
$transaction = DB::transaction(function () use($request,$tender,$id) {
$tender->extend_date = $request->extend_date;
$tender->remarks = $request->remarks;
$tender->update($request->all());
});
return 'BID '.$tender->ref_no.' Succesfully Updated';
}
}
}
this is my route
Route::post('tender/update/{id}','Tender\TenderMasterController#update')->name('bid.update');
this is my blade
<form action="{{route('bid.update' ,Crypt::encrypt('id'))}}" class="form-horizontal" id="bid-update" method="POST">
{{ csrf_field() }}
#method('POST')
#include ('tender.form', ['formMode' => 'edit'])
</form>
Put this in your form action tag
<form action="/tender/update/{{Crypt::encrypt('id')}}" class="form-horizontal" id="bid-update" method="POST">
{{ csrf_field() }}
#method('POST')
#include ('tender.form', ['formMode' => 'edit'])
</form>
And replace this line of your controller:
$tender = TenderMaster::findOrFail($id);
With this:
$tender = TenderMaster::findOrFail(Crypt::decrypt($id));
And don't forget to add this line above in your controller
use Illuminate\Support\Facades\Crypt;
Hopefully it'll work
there's function encrypt and decrypt
but, i would like to disagree with idea of encrypting user id, its far from best practice
i would like to recommend you to use policy, policy guide
Use laravel builtin encryption to achieve this:
While adding your route in frontend, encrypt id with encryption helper like this:
{{route('bid.update', encrypt($id))}}
Now, In your controller, decrypt the id you have passed.
public function update($id, Request $request){
$ID = decrypt($id);
$tender = TenderMaster::findOrFail($ID);
..
...
}
I hope you understand.
Here is the docs:
https://laravel.com/docs/6.x/helpers#method-encrypt
https://laravel.com/docs/6.x/helpers#method-decrypt

How to update a record in lumen

I am simply trying to update a record but it says undefined variable in my view.
My html form view is like:
<form name="add_page" data-toggle="validator" role="form" id="add-pageform" action='{{url("update/page/$page->page_id")}}'>
My Controller:
public function updatePage(Request $request,$id)
{
$page = $request->all();
$plan = PageList::find($id);
$plan->update($page);
return view('edit-list');
}
and my route.php
$router->post('update/page/{id}','AjaxController#updatePage');
when i trying to access my view it says
Undefined variable: page (View: D:\xampp\htdocs\dev\app\resources\views\edit-list.blade.php)
its displaying error from in my form tag
any help would be appreciated:
You have not passed the $page variable to the view. Update your controller action like such:
public function updatePage(Request $request,$id)
{
$page = $request->all();
$plan = PageList::find($id);
$plan->update($page);
return view('edit-list')->with('page', $page);
}
For more information on passing data to views check this link
You should change your routes method to a patch if you want to update and then insert this into your form #method{'PATCH'}

Proper Laravel routing with absence of URI parameters

I have a form which can be saved as draft. The initial route will not have the parameter with the submitted id -- since it has not been submitted. Once it's saved, the route will contain the submitted id to retrieve the data and show it to the user.
I am currently creating multiple routes to accommodate this, which is very messy and can see how this will be an issue to maintain.
How can I account for the absence of parameters in routes, especially the form routes or controllers which throw an error with missing variables?
Routes:
Route::get('/request/{unit}/{id}',
'RequestsController#showNewRequest')->name('request.show-new-request');
Route::get('/request/{unit}/{id}/{rid}',
'RequestsController#showRequest')->name('request.show-request');
Route::post('/request/{unit}/{id}',
'RequestsController#storeNew')->name('request.store-new');
Route::post('/request/{unit}/{id}/{rid}',
'RequestsController#store')->name('request.store');
Controllers
public function showNewRequest($unit, $id) { }
public function showRequest($unit, $id, $rid) { }
Form/Blade:
#if(isset($rid))
<form class="form-horizontal" method="POST" enctype="multipart/form-data"
action="{{ route('request.store', ['unit' => $unit, 'id' => $id, 'rid' => $rid]) }}">
#else
<form class="form-horizontal" method="POST" enctype="multipart/form-data"
action="{{ route('request.store-new', ['unit' => $unit, 'id' => $id]) }}">
#endif
You can use ? in the route parameters. This will let you ignore them. Then you can change the code to something like this:
Route::get('/request/{unit}/{id}/{rid?}',
'RequestsController#showRequest')->name('request.show-request');
Controller:
public function showRequest($unit, $id, $rid = null) {
if ($rid) {
//Do something with $rid
} else {
//Do something considering that this is a draft.
}
}
This also applies to post routes.

Using Request and Response in Laravel

Here is my Form
{{ Form::open(array('url' => 'register', 'class' => 'form-signin')) }}
// form username , password .. fields here
{{ Form::close() }}
And the route is
Route::post('register', 'RegisterController#registeruser');
And the Controller is
public function registeruser()
{
//validation
return Redirect::to('/')->withErrors($validator); // main
}
As, i am being redirected i won't be having any fields that is filled in the form.
I know that i should use request and response for this.
I tried with below response, even this is a horrible try.
return Response::view('home')->header('utf-8', $messages);
But while doing above even the page reloads and the values filled in the form disappears. How can i through the errors without the values disappears ?
Is there a way to have filled the fields in the form as entered ?
You need to use ->withInput()
return Redirect::to('/')->withInput()->withErrors($validator); // main
You can use Input::flash()
public function registeruser()
{
//validation
Input::flash();
return Redirect::to('/')->withErrors($validator); // main
}
Then in the controller method that handles the redirect you can access the old input with Input::old(). Using Input::all() will only return data for the current request.
You can also use the ->withInput() chained method on your return line.
Read more about it here http://laravel.com/docs/4.2/requests
Let's suppose you have a validation like this:
$validation = Validator::make($input, $rules);
Then you need to do that:
if( $validation->fails() )
{
return Redirect::back()->withInput()->withErrors($validator->messages());
}
note the Redirect::back() that allows you to get back to your form and fill it automatically with user input

Missing argument Laravel controller function edit

I have a problem with this
I have un list of articles, and each element has a button to edit, how the next code:
<p>modifier l'article</p>
and I'm sending to the file route:
Route::get('/edit', 'ArticleController#edit');
to the file ArticleController method edit:
public function edit($idarticle)
{
$artic=article::find($idarticle);
if(is_null ($artic))
{
App::abort(404);
}
$form_data = array('route' => array('article.update', $artic->idarticle), 'method' => 'PATCH');
$action = 'modifier';
return View::make('article.create')->with('artic', $artic);
}
then I don't understand my error
Probably change Route::get('/edit', 'ArticleController#edit'); to Route::get('/edit/{idarticle}', 'ArticleController#edit');
Also
<p>modifier l'article</p>
needs to be
<p>modifier l'article</p>
The parameter in the router is not passed as an html parameter, but rather a part of the URL. So combines these two changes, it should be working.

Categories