0

I have a data coming from the HTML Page. And i want to check whether the date and the place values already exists. If they exists, it should throw an error saying Data is already present, if those date and place data is not there it should allow the user to save it.

Here is the code which i have written to save it,

public function StoreSampling(Request $request)
{
  $date = Carbon::createFromFormat('d-m-Y', $request->input('date'))->format('Y-m-d');

  $doctorname = Input::get('doctorselected');
  $product = Input::get('product');
  $product= implode(',', $product);
  $quantity = Input::get('qty');
  $quantity =implode(',',$quantity);
  $representativeid = Input::get('representativeid');
  //Store all the parameters.
  $samplingOrder = new   SamplingOrder();
  $samplingOrder->date = $date;
  $samplingOrder->doctorselected = $doctorname;
  $samplingOrder->products = $product;
  $samplingOrder->quantity = $quantity;
  $samplingOrder->representativeid = $representativeid;
  $samplingOrder->save();
  return redirect()->back()->with('success',true);
}

I searched some of the Stack over flow pages. And came across finding the existence through the ID And here is the sample,

$count = DB::table('teammembersall')
    ->where('TeamId', $teamNameSelectBoxInTeamMembers)
    ->where('UserId', $userNameSelectBoxInTeamMembers)
    ->count();

if ($count > 0){
    // This user already in a team
    //send error message
} else {
    DB::table('teammembersall')->insert($data);
}

But i want to compare the date and the place. And if they are not present, i want to let the user to save it. Basically trying to stop the duplicate entries.

Please help me with this.

Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231
Nagendra Bhat
  • 43
  • 1
  • 9

3 Answers3

1

There are very good helper functions for this called firstOrNew and firstOrCreate, the latter will directly create it, while the first one you will need to explicitly call save. So I would go with the following:

$order = SamplingOrder::firstOrNew([
  'date' => $date,
  'place' => $place 
], [
   'doctorname'       => Input::get('doctorselected'),
   'product'          => implode(',', Input::get('product')),
   'quantity'         => implode(',',Input::get('qty')),
   'representativeid' => Input::get('representativeid')
]);

if($order->exists()) {
   // throw error
   return;
}

$order->save();
// success
nakov
  • 13,938
  • 12
  • 60
  • 110
0

You need to modify your query to something like this:

$userAlreadyInTeam = SamplingOrder::where('date', $date)
    ->where('place', $place) // I'm not sure what the attribute name is for this as not mentioned in question
    // any other conditions
    ->exists();

if (userAlreadyInTeam) {
    // Handle error
} else {
    // Create
}

You do not need to use count() as your only trying to determine existence.

Also consider adding a multi column unique attribute to your database, to guarantee that you don't have a member with the same data and place.

Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231
0

The best way is to use the laravel unique validation on multiple columns. Take a look at this. I'm presuming that id is your primary key and in the sampling_orders table. The validation rule looks like this:

'date' => ['unique:sampling_orders,date,'.$date.',NULL,id,place,'.$place]

p.s: I do not see any place input in your StoreSampling()

shahabphp
  • 369
  • 1
  • 2
  • 10