How to encrypt id in URL laravel - php

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

Related

I got new function for API Controller and it's not working at this

I coded update status for category. I got a new function in an API
Controller, so when I click submit it is not working.
This is solved when I move to category.update, but I can't because that function is used for something else.
web.php
Route::patch('category/{$category}', 'Admin\CategoryController#change')
->name('category.change');
Route::resource('category', 'Admin\CategoryController')
->middleware('loggedin');
This is the new function for API Controller:
public function change($category, Request $request)
{
$cate = Category::find($category);
if ($cate->category_status == 0) {
$cate->category_status = 1;
$cate->save();
} else {
$cate->category_status = 0;
$cate->save();
}
return back()->with('success', 'Success!');
}
list.blade.php
<form autocomplete="off" action="{{ route('category.change', [$cate->category_id]) }}" method="POST" enctype="multipart/form-data">
#method('PATCH')
#csrf
<button class="fa fa-eye" type="submit"></button>
</form>
First, in route, delete $
Route::patch('category/{category}', 'Admin\CategoryController#change')
->name('category.change');
Second - use route with named param
route('category.change', ['category' => $cate->category_id])
Third - in controller action Request must be first
public function change(Request $request, $category){}

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.

Laravel Change database column when button is clicked

I want to change the status of a task to complete. I have a status_id column in the database and 1 equals complete. I would like the click of the button to change the status_id to 1
My route
Route::patch('/tasks/completed/{Task}', 'TasksController#completedUpdate')->name('completedUpdate');
My button
<form action="{{ route('completedUpdate', $task->id) }}" method="POST">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<button type="submit" class="button is-inverted" style="margin-top: 10px;">Mark Complete</button>
</form>
My controller
public function completedUpdate(Request $request, $task)
{
$task->status_id = $request->status_id;
$task->save;
return redirect()->back()->with('message', 'task marked complete');
}
the error it gives me is:
Attempt to assign property of non-object
Let me know if any more info is needed
You should change:
public function completedUpdate(Request $request, $task)
{
$task->status_id = $request->status_id;
$task->save;
return redirect()->back()->with('message', 'task marked complete');
}
into:
public function completedUpdate(Request $request, Task $task)
{
$task->status_id = $request->status_id;
$task->save();
return redirect()->back()->with('message', 'task marked complete');
}
so you need to typehint type of $task variable and use save() method instead of save property.
Also probably instead of:
/tasks/completed/{Task}
you should use:
/tasks/completed/{task}
$task->save; should be $task->save();
With ->save, it is looking for a property on the model, hence the error message re 'assigning a property'. Whereas ->save() calls the save method on the object.
In your controller, you're assigning the $task->status_id a value of $request->status_id but you're actually not passing the status_id in your form in your HTML code. You can put a hidden element in your form which is <input name="status_id" value="1" />.
In the meanwhile, do not forget that $task->save; must be $task->save();
Good luck!

Multiple forms in one page in Laravel 5.1

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

Attempt to update users table generates MethodNotAllowedHttpException, Laravel-4

UsersController:
public function update($id)
{
if( ! $this->user->isValid(Input::all()))
{
return Redirect::back()->withInput()->withErrors($this->user->errors);
}
$user = $this->user->find($id);
$user->save();
return Redirect::route('users.index');
}
Route:
Route::resource('users','UsersController');
Model:
protected $table = 'users'
edit.blade.php:
{{ Form::model($user, array('route'=>array('users.update','$user'=>'id'))) }}
I notice that this does NOT generate a "PUT" action. The page source:
<form method="POST" action="https://zocios.com/users/id" accept-charset="UTF-8"><input name="_token" type="hidden" value="...">
Hitting the Update User button gets me:
Exception \ MethodNotAllowedHttpException
Is the problem "$user->save();"? Something else I'm doing wrong? Thanks!
You need to specify the method:
{{ Form::model($user, array('method' => 'put', 'route'=>array('users.update','$user'=>'id'))) }}
There is no other method than GET and POST that is accepted (despite the specs), so the framework does the job of identyfying hidden input in your form _method to make it work.

Categories