Reputation: 55
I have a code that posts details from a form that looks like this. Field that contains Ampang, Kuala Lumpur
= area_slug
This form then goes into a controller that as a post function to save the values in a table. Below is the code in the post function:
if ( $this->outlet->locations->count() ) {
foreach ( $this->outlet->locations as $index => $location ) {
$area = Level2::find( $locations[ $index ][ 'area_id' ] );
$area_slug = ( $area ? $area->slug : '' );
$shipping_fee_value = (float)$locations[ $index ][ 'shipping_fee_value' ];
// two type of conditional checks
// $shipping_fee_allow_negotiate = !empty( $location[ 'shipping_fee_allow_negotiate' ] );
if ( !empty( $locations[ $index ][ 'shipping_fee_allow_negotiate' ] ) ) {
$shipping_fee_allow_negotiate = 1;
} else {
$shipping_fee_allow_negotiate = 0;
}
/**
* Update each location belongs to Outlet
* Laravel Model Insert
* @see https://laravel.com/docs/8.x/eloquent#updates
*/
$location->area_slug = $area_slug;
$location->shipping_fee_value = $shipping_fee_value;
$location->shipping_fee_allow_negotiate = $shipping_fee_allow_negotiate;
$location->update();
}
} else {
foreach ( $locations as $location ) {
$area = Level2::find( $location[ 'area_id' ] );
$area_slug = ( $area ? $area->slug : '' );
$shipping_fee_value = (float)$location[ 'shipping_fee_value' ];
// two type of conditional checks
// $shipping_fee_allow_negotiate = !empty( $location[ 'shipping_fee_allow_negotiate' ] );
if ( !empty( $location[ 'shipping_fee_allow_negotiate' ] ) ) {
$shipping_fee_allow_negotiate = 1;
} else {
$shipping_fee_allow_negotiate = 0;
}
/**
* Create Location and related it to Outlet with `outlet_id`
* Laravel Model Insert
* @see https://laravel.com/docs/8.x/eloquent#inserts
*/
$location = new Location();
$location->outlet_id = $this->outlet->id;
$location->area_slug = $area_slug;
$location->shipping_fee_value = $shipping_fee_value;
$location->shipping_fee_allow_negotiate = $shipping_fee_allow_negotiate;
$location->save();
}
}
So what I'm trying to achieve is to prevent the same area_slug
being posted eg. Ampang, Kuala Lumpur
in both Location 1
and Location 2
respective fields. I have the idea of getting the previous area_slug
but I'm not sure how to do so. If there are any unclear details do comment.
Upvotes: 2
Views: 1096
Reputation: 168
when you creating migration set table field as unique
$table->unique('email');
then you searching use distinct
for query
return DB::table('teat_table')
->select('teat_table.*')
->distinct()
->get();
Upvotes: 0
Reputation: 331
I suggest you the following to have a good quality software :
Create a separate request Validation object called ModelStore with the following command :
php artisan make:request ModelStore
use this created as a parameter of your controller store function. like following
public function store(ModelStore $request)
in this ModelStore class implement 2 more function (there is 2 created by default for error code and message)
public function rules()
{
return [
'request_field' => ['LaravelValidation|anotherLaravelValidation']
];
}
//In this function you can put all your if else validations and returned a validation array
public function all($keys = null)
{
$data = parent::all($keys);
$data['one_field'] = affectValue;
return $data;
}
finaly in your controller store function start with this line to have the data array
$validated_data = $request->validated();
And then you have your data validated and cleaned. in the controller you keep only the logic and not validation.
To answer your initial question : if you want to have a complete unique slug field than you simply implement it in the rules with the distinct or unique laravel validation.
Good luck
Upvotes: 0