Laravel 6 validate multiple columns - laravel

Using the following code to create and update records with Laravel 6, how to add a unique validation that combines 'iso' and 'division' columns must unique, for both update and create? Also how to update the 'updated_at' column automatically without 'touch' it?
public function upsert(Request $request)
{
$this->authorize('manage','App\Admin\Division');
$this->validate($request,[
'records.iso' => 'required|regex:/^[A-Z]{2}$/',
'records.division' => 'required|min:2|max:3',
'records.remarks' => 'min:2|max:255|string|nullable',
],
[ 'records.iso.required'=>'Country iso Requied',
'records.iso.regex'=>'Country iso format : AA',
'records.division.required'=>'Division Code must entered',
'records.division.min'=>'Division Code minimum 2 characters',
'records.division.max'=>'Division Code maximum 3 characters',
],
);
$validatedData = $request->records;
if($validatedData['id']){ // have id, old record -> update
Division::where('id', $validatedData['id'])->update($validatedData);
//*******/ To correct updated_at no auto updated
$results = Division::find($validatedData['id']);
$results->touch();
//****** */ End of correction
}else{ // no id -> create record
$validatedData['auth'] = Auth::id();
$results = Division::create($validatedData);
}
return ['divisions' => $results];
}

All the validation rules apply for each field, i think you can do a foreach to workaround and compact the code, something like:
foreach ($records as $key => $record) {
$fields['record'.$key] = 'iso', 'division';
$rules['record'.$key] = 'required|min:2';
}
$validator = Validator::make($fields, $rules);
In the rules array you can put all the shared rules, but you have to create the other complex validation rules after this, so i think that your validation now is just fine.
And with the updated_at field, use the updateOrCreate
$results = Division::updateOrCreate(
['id', $validatedData['id'],
[$validatedData]
);
Adapt the above example for your code.

Related

how to check if cart has already added not allow more and update if different cart

I'm using composer require gloudemans/shoppingcart I am not sure how to maintain amount.
When i'm using one route that says add item when i'm using this route adding multiple item
How can i conditionally setup to add item in cart if this is unique
public function bookItem($id) {
$item = Item::where([
'status' => '1',
'id' => $id
])->first();
$product = Cart::add($item->id, $item->name, 1, $item->price); // This should not call always if it has not generated a row id then it should call
Cart::update($product->rowId, ['price' => 200]); // Will update the price if it is differ
return redirect()->route('book.item', ['id' => $item->id]);
}
I am not sure how to manage it. please guide
Looks like the package has a getContents() function that gathers all items into a collection. It also has a search(Closure) function that calls getContents() and then uses Laravel's filter function on the collection and returns the result.
$search = Cart::search(function($key,$value) use ($item) {
return $value === $item->id;
})->first();
if(!empty($search)){
Cart::update($search->rowId, ['price' => 200]);
}
else {
$product = Cart::add($item->id, $item->name, 1, $item->price);
}
Definitely check out the Laravel collection docs if you aren't familiar. This is the entey on filters:
https://laravel.com/docs/5.8/collections#method-filter

Opposite of required_if in laravel using multiple value evaluation

How to validate must null if another field has specific value or not null
In my case it is the opposite of required_if with multiple values
$rule = array(
'selection' => 'required',
'stext' => 'required_if:selection,2|required_if:selection,3',// stext should be null if selection is 2 or 3
);
And if needed how to perform own validation?
So in your example you can do something like this:
$rule = array(
'selection' => 'required',
'stext' => 'required'
);
// override the rule
if(in_array(request('selection'), [2, 3]))
{
$rule['stext'] = 'nullable';
}
This means if the selection is 2 the field will be required and if the selection field has any other value the stext field will be required.
I am not sure if I understood your question correctly. In any case the opposite of required_if is required_without so you can use that one if you want this field to be required even if the selection is empty.
With custom rule your passes method should look like this:
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class CustomRule implements Rule
{
protected $selection;
public __construct($selection)
{
$this->selection = $selection;
}
public function passes($attribute, $value)
{
return $value === null && in_array($this->selection, [2, 3]);
}
}
You use it like this:
$rule['stext'] = [ new CustomRule(request('selection') ]
I try to extend the validation rule. put the following in AppServiceProvider:
Validator::extend('null_if', function ($attribute, $value, $parameters, $validator) {
$other = $parameters[0];
$other_value = array_get(request()->toArray(), $other);
if ($parameters[1] == $other_value) {
return empty($value);
}
return true;
});
Tell me if it's work or what error given to you.

Yii2 ActiveRecord

I'm new to Yii and struggling to get my head around a few things.
I have model with this function:
public static function findNonGeo()
{
$query = new CallerIdentityQuery(get_called_class());
$query->select([
'cidref', 'caller_id', 'expiry', 'conf_call',
'type', 'redirect', 'destination', 'status', 'start_date',
'statusDesc' => new \yii\db\Expression("CASE status
WHEN 0 THEN 'Deactivated'
WHEN 1 THEN 'Active'
WHEN 2 THEN 'Inactive'
WHEN 3 THEN 'Unregistered'
ELSE 'Permanently Deleted' END")])
->where(['status' => '1'])
->andWhere(['type' => 'N'])
->andWhere(['custref' => Yii::$app->user->identity->typedIdentity->getId()]);
return $query;
}
And in my controller I call this method like so:
$model = CallerIdentity::findNonGeo()->where(['cidref' => $id])->one();
but when the data is returned its like its just ignoring the
->andWhere(['type' => 'N'])
because I can view records that aren't of type 'N'.
so I'm having to do in my controller, maybe I'm doing something wrong or just not understanding it but I don't get it:
$model = CallerIdentity::findNonGeo()->where(['cidref' => $id])->andWhere(['type'=>'N'])->one();
another thing when using findOne($id):
$model = CallerIdentity::findOne($id);
null is returned.
Would appreciate any form of explanation/info.
When using where you're setting the where part of the query, not adding to it. So in the findNonGeo function you're building the required where parts. But with CallerIdentity::findNonGeo()->where(['cidref' => $id]) you're removing the already added where parts.
Try like this:
CallerIdentity::findNonGeo()->andWhere(['cidref' => $id])->one();
by using andWhere you'll keep the other parts and just add another one to it.

Quickbase delete_record

I am unable to get delete_record function to work. Here is the delete_record code that I am currently using:
$qb->db_id = $myQbTables["table1"]; //using table 1 now
$queries = array( //this is the where clause now for the delete query
array(
'fid' => '12',
'ev' => 'EX',
'cri' => '1175'
),
array(
'fid' => '8',
'ev' => 'EX',
'cri' => $Child_ID
)
);
$results = $qb->do_query($queries, '', '', 'a', '1', 'structured', 'sortorder-A'); //get all results
if($results->errCode !== 0) {
echo $results->errTxt;
}
print_r($results);
foreach($results->table->records->record as $record){ //getting results of delete query
$Deletevar = $record->f[11];
$deleted = $qb->delete_record($Deletevar); //deleting results
print_r($deleted);
}
I know that the 'cri' matches things in my quickbase but I can't seem to use f[3] (the record id) to delete it!
NOTE
I have found the main issue I was having! If you are making API calls using the QB API, delete records and purge records does not include the app token!! Please use the following code to update the delete_records function!!!!
public function delete_record($rid) {
if($this->xml) {
$xml_packet = new SimpleXMLElement('<qdbapi></qdbapi>');
$xml_packet->addChild('rid',$rid);
$xml_packet->addChild('ticket',$this->ticket);
$xml_packet->addChild('apptoken',$this->app_token);
$xml_packet = $xml_packet->asXML();
$response = $this->transmit($xml_packet, 'API_DeleteRecord');
}
else {
$url_string = $this->qb_ssl . $this->db_id. "?act=API_DeleteRecord&ticket=". $this->ticket
."&apptoken=".$this->app_token."&rid=".$rid;
$response = $this->transmit($url_string);
}
return $response;
}
I think you may be missing something in your query. If you're trying to get a list of records where the field 12 is 1157 and field 8 is equal to $Child_ID you'll need to add AND into your query. When using the API in a URL the query would be &query={12.EX.1175}AND{8.EX.77} if, for example, the child ID were 77. To do that in the PHP SDK for Quickbase you add 'ao' => 'AND' to all arrays following the first query array. Try updating your $queries array to this:
$queries = array( //this is the where clause now for the delete query
array(
'fid' => '12',
'ev' => 'EX',
'cri' => '1175'
),
array(
'ao' => 'AND', // Saying that 12.EX.1175 AND 8.EX.$Child_ID
'fid' => '8',
'ev' => 'EX',
'cri' => $Child_ID
)
);
My PHP skills aren't particularly good, but you'll need to do a bit more to get the record ID#. In the original code, I think that $record->f[3] will just return to SimpleXMLObject for the third field in the array. That won't necessarily be field ID 3 (the record ID). You can either choose to not use the structured format (and change all of your code to match) or add a loop that goes through all the fields and deletes when it reaches the field with id 3:
foreach($results->table->records->record as $record){ //getting results of delete query
//Loop through all fields to find id 3
foreach($record->f as $field){
if($field['id'] == "3"){
$Deletevar = $field['id'];
$deleted = $qb->delete_record($Deletevar); //deleting result
print_r($deleted);
break; //Stops this loop since FID 3 was found
}
}
}
Some performance notes: If you aren't using any other fields, you can set your query to only return record IDs by passing a clist that is just '3'. Also, each delete_record() is a separate API call. There is another method purge_records that allows you pass a query and Quickbase deletes all of the records that match your query. You may want to look into using purge_records() because you can you use it in place of your do_query() and avoid all the loops. It uses fewer of your resources and Quickbase's to process as well.

Validate single rows only when they are present Laravel

So i have a model named Customer.
The db the Customer looks like this:
id, name, lastName, personal, address, zip, location, phones, emails updated_at, created_at
Emails and Phones is special rows because they are store as an json object example
['john#doe.com', 'some#othermail.com', 'more#mails.com']
I use the Customer Model to store the validation rules and custom messages like this
<?php
class Customer extends BaseModel
{
public function validationRules()
{
return array(
'name' => 'required|max:255',
'lastName' =>'max:255',
'personal'=> 'integer',
'location' => 'max:255',
'address' => 'max:255',
'zip' => 'required|integer',
'phones' => 'betweenOrArray:8,10|required_without:emails',
'emails' => 'emailOrArray'
);
}
public function validationMessages()
{
// returns Validation Messages (its too much to write down)
}
}
The OrArray Rule is found here https://stackoverflow.com/a/18163546/1430587
I call them through my controller like this
public function store()
{
$customer = new Customer;
$messages = $customer->validationMessages();
$rules = $customer->validationRules();
$input['name'] = Input::get('name');
$input['lastName'] = Input::get('lastName');
$input['personal'] = preg_replace("/[^0-9]/", "", Input::get('personal'));
$input['location'] = Input::get('location');
$input['address'] = Input::get('address');
$input['zip'] = Input::get('zip');
$input['emails'] = Input::get('emails');
$input['phones'] = Input::get('phones');
foreach($input['phones'] as $i => $value)
{
$input['phones'][$i] = preg_replace("/[^0-9]/", "", $value);
}
$validator = Validator::make($input, $rules, $messages);
}
This all works just fine, but I want to be able to PUT/PATCH request to update a single row.
But the validationRules has Required on certain fields so when its not present i cant update that single row. Without getting an error that the other fields (witch I'm not posting) is required.
How would I best approach this ?
You should get that instance of the model that represent the row you want to edit, that's why the resource controller's update method has a parameter that is the resource you want to edit.
public function update($resourceId) {
$customer = Customer::where('id', '=', $resourceId);
}
Now this customer has all the attributes you set before, so you can access them like:
$customer->name;
$customer->lastName;
So when you valide the values you can use the existing values in your validator where the input is empty:
$input['name'] = (Input::get('name')) ? (Input::get('name')) : $customer->name;
Or a prettier solution with the elvis operator:
$input['name'] = (Input::get('name')) ?: $customer->name;
I came up with another solution to this problem that works very well and its much cleaner.
$customer = Customer::find($id);
$input = Input::except('_method', '_token');
$customer->fill($input);

Resources