Laravel update request validation how to get the id - laravel

I am using Laravel 5.2 and I have managed to set correctly my update request for my users but when I do exactly the same for another table (clusters), I cannot get the id in the clusterrequest validation.
Here is what I have for my clusters:
routes:
Route::post('clusterFormUpdate/{id}', ['uses'=>'ClusterController#postFormUpdate','middleware' => ['permission:user-edit']]);
Controller:
public function postFormUpdate(ClusterUpdateRequest $request, $id)
{
$inputs = $request->all();
ClusterUpdateRequest:
...
public function rules()
{
$id = $this->id;
dd($id);
return [
'name' => 'required|max:255|unique:clusters,name,' . $id . ',id',
'countries' => 'required',
];
}
When I run this, I get null.
If I try the same for my user table, I get the id that is printed correctly so I was wondering where I can look for this problem?
Thanks.

To get the id in the Request you can use the route() method:
public function rules()
{
$id = $this->route('n'); //or whatever it is named in the route
return [
'name' => 'required|max:255|unique:clusters,name,' . $id . ',id',
'countries' => 'required',
];
}

Related

how can I save user ID on Laravel

Hello I am newbie.
// Framework 6.9.0
$id = id(); // user id
public function store(Request $request) {
$data = $request->validate([
'title' => 'required|max:100',
'description' => 'required',
'user_id' => $id
]);
CRUD::create($data);
return redirect('/')->with('success', 'test is successfully saved');
}
'user_id' => $id Is it possible ? how can I insert a "user id" in TABLE
I will be waiting for your response !
If you are using the default authentication system, you can use the Auth facade.
'user_id' => Auth::id()
and add this line at the top of your controller:
use Illuminate\Support\Facades\Auth;
Also, remove this line:
$id = id();

Laravel form request validation on store and update use same validation

I create laravel form validation request and have unique rules on that validation.
I want use it on store and update method without create new form request validation again.
but the problem is when on store the id doesnt exist and the validate is passed
and when on update i failed the pass the validating because the id is exist on storage
i want to ignore the id on unique rules but use same form validate request
what is best practice to check on form validate request class if this action from store or update method to ignore unique id ?
Ok.. i can do it like #porloscerros Ψ suggest
public function rules()
{
$rules = [
'name' => 'required|string|unique:products|max:255',
];
if (in_array($this->method(), ['PUT', 'PATCH'])) {
$product = $this->route()->parameter('product');
$rules['name'] = [
'required',
'string',
'max:255',
Rule::unique('loan_products')->ignore($product),
];
}
return $rules;
}
Try this, it worked for me.
Laravel unique: third param can exclude the id for example, of the record, like this:
public function rules()
{
return [
'name' => 'required|string|max:255|unique:products,'.$this->id,
];
}
Why are you checking the id when store or update in FormRequest? You don't need this. The id comes to your controller's method like as parameter. Or laravel will create the model using DI in the your controller's method public function update(User $user) and then you can use $user like an instance of User model. You may check the id in web.php or api.php:
https://laravel.com/docs/7.x/routing#parameters-regular-expression-constraints
And I suggest you not to use one FormRequest for two methods. This is bad practice
im using this
$validated = $request->validated();
use this method:
public function createAccount(RegisterRequest $request)
{
$attr = $request->validated();
instead of something like this:
public function createAccount(Request $request)
{
$attr = $request->validate([
'name' => 'required|string|max:255',
'email' => 'required|string|email|unique:users,email',
'password' => 'required|string|min:6|confirmed'
]);
use php artisan make:request RegisterRequest
public function rules()
{
return [
'name' => 'required|string|max:255',
'email' => 'required|string|email|unique:users,email',
'password' => 'required|string|min:6|confirmed'
];
}
public function rules()
{
if (request()->isMethod('post')) {
$rules = [
'image' => 'required|image|mimes:jpeg,jpg,png|max:2000',
'name' => 'required|unique:categories'
];
} elseif (request()->isMethod('PUT')) {
$rules = [
'name' => 'required|unique:categories,name'
];
}
return $rules;
}

What are REST standards for PUT and POST methods?

I am implementing API in Laravel and get comment that my POST and PUT methods are not accurate according to the REST standards.
I am using POST for create new resource and PUT for updating existing one. Can not see problem.
endpoints:
Route::post('/cities', [
'uses' => 'CityController#store'
]);
Route::put('/cities/{id}', [
'uses' => 'CityController#update'
]);
PUT and POST method :
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required|min:3',
'latitude' => 'required|numeric',
'longitude' => 'required|numeric'
]);
// update model and only pass in the fillable fields
$this->cityRepository->update(
$request->only($this->cityRepository->getModel()->fillable), $id
);
return $this->cityRepository->show($id);
}
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|min:3',
'latitude' => 'required|numeric',
'longitude' => 'required|numeric'
]);
$data = $this->cityRepository->create(
$request->only($this->cityRepository->getModel()->fillable));
if ($data) {
$message = self::SUCCESSFULLY_CREATED;
$code = self::HTTP_CODE_CREATED;
} else {
$message = self::UNSUCCESSFULLY_CREATED;
$code = 409;
}
return $this->sendResponse($message, $data, $code);
}
Send response:
public function sendResponse($message, $result = [], $code = 200)
{
$response = [
'message' => $message,
];
if (!empty($result)) {
$response['data'] = $result;
}
return response()->json($response, $code);
}
Show method:
public function show($id)
{
return $this->model->findOrFail($id);
}
You could return the created object from your store method instead of SUCCESSFULLY_CREATED. Aside from that the code looks good.
Take a look at the table on https://laravel.com/docs/5.8/controllers#resource-controllers, which has a fairly useful REST definition of various CRUD routes:
GET /photos index photos.index
GET /photos/create create photos.create
POST /photos store photos.store
GET /photos/{photo} show photos.show
GET /photos/{photo}/edit edit photos.edit
PUT/PATCH /photos/{photo} update photos.update
DELETE /photos/{photo} destroy photos.destroy
Here's a good resource for which HTTP methods you should return:
https://www.restapitutorial.com/lessons/httpmethods.html

laravel request validation rules pass parameter

I'm going straight to the point here, I am wondering if it is possible to pass a parameter on a validation rule in Laravel.
Here's my code:
I need to pass the $product->id to the ProductUpdateRequest class.
I've read some articles and to no avail can't pass a parameter into it. my other solution was to not use the validation rule class and do the validation directly on the controller by using $request->validate[()]. Since I can access the $product->id on the controller I can easily do the validation. but out of curiosity is there a way for me to pass the $product->id on the validation class?
CONTROLLER
public function update(ProductUpdateRequest $request, Product $product)
{
$request['detail'] = $request->description;
unset($request['description']);
$product->update($request->all());
return response([
'data' => new ProductResource($product)
], Response::HTTP_CREATED);
}
VALIDATION RULE
public function rules()
{
return [
'name' => 'required|max:255|unique:products,name'.$product->id,
'description' => 'required',
'price' => 'required|numeric|max:500',
'stock' => 'required|max:6',
'discount' => 'required:max:2'
];
}
Any suggestions/answers/help would be highly appreciated.
You can get the resolved binding from request
$product = $this->route('product');
Inside your rules method you can get the product instance with the above method.
public function rules()
{
$product = $this->route('product');
return [
'name' => 'required|max:255|unique:products,name'.$product->id,
'description' => 'required',
'price' => 'required|numeric|max:500',
'stock' => 'required|max:6',
'discount' => 'required:max:2'
];
}
It works when you make a function with this Product $product (when you used the Resource route in most cases)
public function update(ProductUpdateRequest $request, Product $product)
{
// code goes here
}
but if you make it like the below it won't work ()
public function update(ProductUpdateRequest $request, $id)
{
// code goes here
}
This is how I would validate unique product name on update. I pass the product ID as a route parameter, the use the unique validation rule to validate that it the product name does't exist in the Database except for this product (id).
class ProductController extends Controller {
public function update(Request $request, $id) {
$this->validate($request, [
'name' => 'required|max:255|unique:products,name'.$id,
]);
// ...
}
}
For custom request in validation rule you can put in your
View :
<input type="hidden" value="product_id">
In Validation Request :
public function rules()
{
$product_id = $this->request->get('product_id');
return [
//
];
}

How to validate PUT parameter in Laravel?

I use PUT routing:
Route::put('offers/{id}/accept', 'OfferController#accept');
And controller:
public function accept(Request $request, $id)
{
$validator = Validator::make($request->all(), [
"id" => 'required|integer'
]);
}
But validation rule does not work for $id parameter. How to validate that?
It's not particular to put, its particular to any route parameter. This is because route params not included in the all() collection.
public function accept(Request $request, $id)
{
$validator = Validator::make(array_merge(
[
'id'=>$id
],
$request->all()
), [
"id" => 'required|integer'
]);
}

Resources