LARAVEL - modifing database data before validating - laravel

I have varchar field in my table and I want to using unique validate but want to remove the whitespace in my table before validate it.
Example :
I have value in database "My Value". And if I send "myvalue" to request data, the validation must be work in this case. How to do it in laravel validation?

I don't know exactly what do you want to do.. DO you mean to do like this?
Go to your model then add this code
public function getNameAttribute($value) {
return str_replace(" ", "", $value);
}

Related

How can I get the data inside my notifications from within my Controller?

I'm trying to get the numbers of records from my notifications, where the candidate_user_id column from inside the data attribute is the same as the UserId (Authenticated User).
After I dd, I was able to get the data from all of the records in the table by using the pluck method (Line 1). I then tried to use the Where clause to get the items that I need
but this just didn't work, it was still returning all of the records in the table.
DashboardController.php:
public function index()
{
$notifications = Notification::all()->pluck('data');
$notifications->where('candidate_user_id', Auth::user()->id);
dd($notifications);
}
Here is a partial screenshot of the data that is being plucked.
How can I get the data from this, in a way like this ->where('candidate_user_id', Auth::user()->id);?
If data was a JSON field on the table you could try to use a where condition to search the JSON using the -> operator:
Notification::where('data->candidate_user_id', Auth::id())->pluck('data');
Assuming you only want this data field and not the rest of the fields, you can call pluck on the builder directly. There isn't much reason to hydrate Model instances with all the fields to then just pluck a single field from them if it is just a table field, so you can ask the database for just the field you want.
The data in the data field is a json string, so you can tell Laravel to automatically cast it as an array using the $casts property on each of the models that is notifiable.
For instance, if you have a User model which uses the trait (ie has use Notifiable), add this:
protected $casts = [
'data' => 'array',
];
If you want to access all notifications for the auth user.
$user = auth()->user();
dd($user->notifications->pluck('data'));
If you really want to do in your question way, here is how.
$notifications = Notification::all()->pluck('data');
$notifications = $notifications->where('candidate_user_id', Auth::user()->id)
->all();
This assumes you that you did not modify the default laravel notifications relationship and database migration setup. If you have modified some of the default ones, you need to provide how you modify it.

Validate if another field is empty laravel validation

I want to validate field if present and will fail if another field not present
How to do it ? I have create custom validation to get record based id on storage
I have field product_id and amount and the amount field validate between max and min based on product_id, if i present the amount = 100000 and not fill the product_id field the validating for amount field is passed and display error because i am not fill the product_id i want check before the custom validation execute will check if product_id empty or not..
I have read all validation method on laravel docs still didnt find the solution.
How to do it ?
Finally i found a solution...
on Form Request class i just add sometimes rule on validate instance so when the product_id field not present on amount field will not validate and when present it will validate using validation rules like this:
class StoreRequest extend FormRequest
{
protected function getValidatorInstance()
{
$validator = parent::getValidatorInstance();
$validator->sometimes('amount', 'required|numeric|max_amount:product_id', function ($input) {
return Arr::has($input, 'product_id');
});
return $validator;
}
}

Laravel unique validation must include appended text when validating

I am using the Laravel tenancy package in which there is a domains table in my database and each value in the domain column within this table is appended with .test.co.uk.
When the user enters the URL in the form, they are presented with an input element (shown above) in which they enter a URL/Domain but the .test.co.uk is already appended so the only thing they need to enter is the text that goes before that, e.g. they would enter johnsmith and in the domain column it would store johnsmith.test.co.uk. The problem I have is that I need the validation on this column to be unique but also include the .test.co.uk when performing the validation so that it looks at the value stored in the table because if a user enters johnsmith and there is currently a record in the domains table where the value is johnsmith.test.co.uk then the validation would pass but I need the validation to fail in this scenario. I am currently using a Request class which is extending the FormRequest class and have this:
public function rules()
{
return [
'url' => 'required|string|unique:domains,domain',
];
}
I have also tried a rule object but I don't think a rule object is the correct solution to this problem. Is there a convenient "Laravel" way of doing this?
In your Request class use prepareForValidation()
Docs: https://laravel.com/docs/7.x/validation#prepare-input-for-validation
protected function prepareForValidation()
{
$this->merge([
'url' => $this->url . '.test.co.uk',
]);
}

Laravel Backpack : Storing Belongs To Many relationships using custom view

I have a flight class and this flight has a custom view field like so:
This represents a belongs to many relationship which stores website_id / flight_id and pricing as pivot data in a pivot table.
The custom view uses JS to send this data back to the controller in this format:
{"1":{"price_adult":"434","price_child":"545"},"2":{"price_adult":"323","price_child":"324"},"3":{"price_adult":"434","price_child":"43"}}
Trying to send this data with the request doesn't create the relations fields, and because I do not have a flight ID at the point of creating this within the controller I can not loop this JSON to make the relations manually.
Can anyone point out what the best course of action is or if there is support for this? I took a look at the docs but they are woefully short and patchy in terms of being much help.
EDIT:
I should have said I can probably make this work using a custom name attribute on the model for the relation, then add a set mutator to loop this data and update the prices relation but I don't want to go down this route if there is support for this I am missing out of the box in backpack.
EDIT2:
Someone asked about the relation:
$this->belongsToMany(Website::class, 'website_pricing')->withPivot('price_adult', 'price_child');
This is working fine its not a problem with the relation working its how can I get backpack to store the data as a relation when the flight has no ID yet, or how can I pass the data I posted above in such a way that the backpack crud controller can handle it?
You may need to create a flight first, if no flight id is being provided. Can you explain the database relational structure more?
Basically thought I should post what I did because no one could provide an answer to this.
So basically you have to copy the store / update functions from the parent, changing a few lines.
$this->crud->hasAccessOrFail('create');
// fallback to global request instance
if (is_null($request)) {
$request = \Request::instance();
}
// replace empty values with NULL, so that it will work with MySQL strict mode on
foreach ($request->input() as $key => $value) {
if (empty($value) && $value !== '0') {
$request->request->set($key, null);
}
}
// insert item in the db
$item = $this->crud->create($request->except(['save_action', '_token', '_method']));
$this->data['entry'] = $this->crud->entry = $item;
// show a success message
\Alert::success(trans('backpack::crud.insert_success'))->flash();
// save the redirect choice for next time
parent::setSaveAction();
return parent::performSaveAction($item->getKey());
Basically any line which references a function in the parent class using $this->method needs to be changed to parent::
This line is what I used to submit the relations JSON string passed to the controller as relations $item->prices()->sync(json_decode($request->input('prices'), true));
This is done after the line containing $item = $this->crud->create as the item id that just got stored will be available at that point.

associate() and 1tomany strange behavior

im using Laravel 5.3 and ive a simple controller with a "store" method, this is the "belongTo" side of the relations.
The others 2 models contain correctly the "hasMany" function.
public function store(Request $request)
{
$user_id = JWTAuth::parseToken()->authenticate()->id;
if(!Vehicle::where('id', '=', $request->vehicle_id)->exists()){
return $this->response->error('could_not_create_trip_errore_veicolo', 500);
}
if(!Ztl::where('id', '=', $request->ztl_id)->exists()){
return $this->response->error('could_not_create_trip_errore_ztl', 500);
}
$request->request->add(['user_id' => $user_id]);
$trip = new Trip($request->all());
//$trip->user()->associate($request->user_id);
//$trip->vehicle()->associate($request->vehicle_id);
//$trip->ztl()->associate($request->ztl_id);
if(true)
{
if($trip->save()){
return $this->response->created();
}else return $this->response->error('could_not_create_trip', 500);
}else return $this->response->error('could_not_create_trip_current_user_Error', 500);
}
First question is:
why if comment or uncomment the "associate" method, nothing changes.
Do I need to put these on the controller or I've not weel understand what is the meaning of this method.
Second:
If I send to my controller some data, using form for testing, what is "required" is the 3 foreign keys.
If I send a number that is not on my other "hasmany" table an error is rise, but if try to insert something like "2dsksk" where 2 is a correct ID of the "many" table and then a random string, the ID is taken by the insert as 2, this is correct?
Validation take just the "correct" number part of the data...the question is, why? and this is secure?
associate just sets the foreign key of the child. This means you will need to save your model afterwards.
$trip->user()->associate($request->user_id);
$trip->save();
For the second issue, that's likely MySQL truncating the data based on the data type of the column. If you don't want that to happen, you would likely need to set MySQL to strict mode.
http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sql-mode-strict

Resources