Laravel unique validation - change value before checking - laravel

I want to check for unique url's by extracting domain name from url.
e.g. https://stackoverflow.com and https://stackoverflow.com/something?foo=bar should show error.
I have form request like below,
public function rules()
{
return [
'name' => 'required',
'url' => 'required|unique:sites,url',
'favicon' => 'nullable|image',
];
}
I tried changing the form request value but it's not working,
// The getDomainName() returns the stackoverflow.com for https://stackoverflow.com
$this->request->set('url',
Site::getDomainName($this->request->get('url'))
);
I also tried following,
$url = $this->request->get('url');
$domain = 'https://' . Site::getDomainName($url);
$uniqueRule = Rule::unique('sites', 'url')
->where(function ($query) use ($domain) {
return $query->where('url', $domain) ? false : true;
});
Any help would be appreciated, thank you!

You need to parse url to extract domain name with protocol and then you can merge into a form request. You have to do it in starting of rules method.
$result = parse_url(request()->url);
request()->merge(['url'=>$result['scheme']."://".$result['host']]);

Solved using prepareForValidation()
protected function prepareForValidation()
{
$this->merge([
'url' => 'https://' . Site::getDomainName($this->request->get('url'))
]);
}

Related

How to edit picture on Coudinary with Laravel

Any Idea how to edit pictures on Cloudinary using Laravel API? I did a lot of searches, but I didn't find any references. The add worked successfully, but I didn't find a solution for the edit.
Add code
$user->user_image = Cloudinary::upload(
$request->file('user_image')->getRealPath(),
[
'folder' => 'Testing'
]
)->getSecurePath();
Attempt at updating picture
public function updatePicture(Request $request, $user_id)
{
$data = $request->validate([
'user_image' => '',
]);
$data = Cloudinary::upload(
$request->file('user_image')->getRealPath(),
[
'folder' => 'Testing'
]
)->getSecurePath();
User::where("user_id", $user_id)->update($data);
return response()->json([
'message' => 'Successfully updated Picture!',
'success' => true,
], 200);
}
For deleting, you can use the destroy() method of the API, for example:
$result = Cloudinary::destroy(
$public_id, //Public ID of the file from Cloudianry to delete - returned from the Upload API response
[
'...' => '...' //any optional parameters
]
)
For a list of all optional parameters and possible values please see:
https://cloudinary.com/documentation/image_upload_api_reference#destroy_method
In terms of updating, I am assuming you are referring to Cloudinary's update details of a single resource method of the Admin API. If so, you can access it like so:
$admin_api = Cloudinary::admin();
$result = $admin_api->update($public_id, $options = []);
Alternatively, if you're referring to the explicit method of the Upload API then you could access it like so:
$result = Cloudinary::explicit($public_id, $options = []);

Invalid ruote rule to catch request from vue-tables-2 component

Using vue-tables-2 component in my #vue/cli 4.0.5 app
I see that GET request generated
http://local-ctasks-api.com/api/adminarea/activity-logs-filter?query=&limit=10&ascending=1&page=1&byColumn=0
and I try in Laravel 6 Backend REST API to set route to catch it as :
Route::group(['middleware' => 'jwt.auth', 'prefix' => 'adminarea', 'as' => 'adminarea.'], function ($router) {
Route::get('activity-logs-filter?query={query}&limit={limit}&ascending={ascending}&page={page}&byColumn={column}', 'API\Admin\ActivityLogController#filter');
But I got 404 error,
Is my route invalid ?
UPDATED # 1:
Yes, var with “/api” was unaccesible. I fixed it and running request without “/adminarea”
http://local-ctasks-api.com/api/activity-logs-filter?query=&limit=10&ascending=1&page=1&byColumn=0
I moved route definition out of any block :
Route::get('activity-logs-filter?query={query}&limit={limit}&ascending={ascending}&page={page}&byColumn={column}', 'API\Admin\ActivityLogController#filter');
I got error in browser :
"error": "INCORRECT ROUTE"
with control action defined in app/Http/Controllers/API/Admin/ActivityLogController.php :
public function filter( $query, $limit, $ascending, $page, $column )
{
\Log::info('!!++ filter $this->requestData ::');
\Log::info(print_r( $this->requestData, true ));
Why error ?
Thanks!
I think you forgot put api on prefix
Route::group(['middleware' => 'jwt.auth', 'prefix' => 'api/adminarea', 'as' => 'adminarea.'], function ($router) {
EDIT:
Don't put parameter on route like that, use Request instance
Route::get('activity-logs-filter,'API\Admin\ActivityLogController#filter');
and controller
public function filter(Request $request){
$query = $request->query;
$limit = $request->limit;
$ascending = $request->ascending;
$page = $request->page;
$column = $request->column;
dont forget use Illuminate\Http\Request; on your controller

How to Redirect using two parameter in Controller using Laravel 5.5

Controller is :
elseif ($profile_is_exsit > '0')
{
$url = DB::table('marriage_bureau')->select('title','custom_id')->where('user_id',$user_id)->first();
$title = $url->title;
$custom_id = $url->custom_id;
return redirect('marriage-bureau/{title}/{custom_id}');
}
This return redirect is generating Error. I need to generate a URL followed by the following route.
Routes in web.php
Route::get('marriage-bureau/{title}/{id}','marriage_bureau\ViewMarriageBureauController#index');
You can provide variable to the url
return redirect("marriage-bureau/{$title}/{$custom_id}");
You can do this
return redirect()->route('route name', ['title' => $title, 'custom_id' => $custom_id]);
In your controller use:
elseif ($profile_is_exsit > '0')
{
$url = DB::table('marriage_bureau')->select('title','custom_id')->where('user_id',$user_id)->first();
$title = $url->title;
$custom_id = $url->custom_id;
return redirect()->route('your-route-name', ['title' => $title, 'custom_id' => $custom_id]);
}
In your route use:
Route::get('marriage-bureau/{title}/{id}',
[
'uses'=>'marriage_bureau\ViewMarriageBureauController#index',
'as'=>'your-route-name',
]
);

How to get paymentMethodNonce in Braintree API?

I'm working in laravel 5.4
My transactions are successfull when I try a 'fake_nonce' type of string provided by the braintree docs. But when I tried to get the paymentMethodNonce it always gives me error like nonce not found. And sometimes http error!!! If I try to configure it by myself!
Take a look at my controller function below
public function addOrder(Request $request){
$customer = Braintree_Customer::create([
'firstName' => $request->guest_name,
'email' => $request->guest_email,
'phone' => $request->guest_phone
]);
$customer->success;
$customer->customer->id;
$find = Braintree_Customer::find($customer->customer->id);
$nonceFromTheClient = Braintree_PaymentMethodNonce::find($find);
$result = Braintree_Transaction::sale([
'amount' => $request->subtotal,
'paymentMethodNonce' => $nonceFromTheClient,
'options' => [
'submitForSettlement' => True
]
]);
if ($result->success) {
$settledTransaction = $result->transaction;
} else {
print_r($result->errors);
}
Cart::destroy();
return view('guest/track', compact('result'));
}
$nonceFromTheClient = Braintree_PaymentMethodNonce::find($find);
Your using the wrong nonce, this nonce must come from the DropIn ui and not be generated on your code.
Please check the onPaymentMethodReceived() method provided in the JS SDK.
Please check this reference

Laravel 5 - Allow to pass only FormRequest rules fields

I would like to use FormRequest validation in which
allow request fields only in rules() return array keys.
In the below code, I would like to allow request fields only 'os', 'number', 'version'. If request include the other field , return error response.
How can I modify the code ?
public function rules()
{
return [
'os' => [
'required',
\Rule::in(['android', 'ios']),
],
'number' => 'required|integer',
'version' => ['required', 'regex:/^\d+.\d+.\d+$/'],
];
}
There is a way you can do this using form request. It may not send the proper response but it works.
In your Form Request's authorize method use the following code.
public function authorize ()
{
$params = $this->request->keys();
$os_status = in_array('os', $params);
$number_status = in_array('number', $params);
$version_status = in_array('version', $params);
$check = $os_status & $number_status & $version_status & (count($params) != 3 ? false : true);
return $check;
}
It will return HTTP response with 403 status code.

Resources