Database data field check before Data insertion












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.










share|improve this question

























  • And what exactly stops you from adapting the example you quoted? $count = DB::table('SamplingOrder')->where('date', $date)->where('place', $place)

    – Boris Schegolev
    Nov 21 '18 at 14:51











  • Please improve your question as theplace attribute is not mentioned anywhere.

    – Yahya Uddin
    Nov 21 '18 at 15:06











  • @YahyaUddin: Yup, Bymistake added another method from the controller. Got it resolved. Thanks for looking into it

    – Nagendra Bhat
    Nov 21 '18 at 17:02
















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.










share|improve this question

























  • And what exactly stops you from adapting the example you quoted? $count = DB::table('SamplingOrder')->where('date', $date)->where('place', $place)

    – Boris Schegolev
    Nov 21 '18 at 14:51











  • Please improve your question as theplace attribute is not mentioned anywhere.

    – Yahya Uddin
    Nov 21 '18 at 15:06











  • @YahyaUddin: Yup, Bymistake added another method from the controller. Got it resolved. Thanks for looking into it

    – Nagendra Bhat
    Nov 21 '18 at 17:02














0












0








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.










share|improve this question
















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.







php mysql database laravel laravel-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 14:59









Yahya Uddin

5,9571455112




5,9571455112










asked Nov 21 '18 at 14:29









Nagendra BhatNagendra Bhat

178




178













  • And what exactly stops you from adapting the example you quoted? $count = DB::table('SamplingOrder')->where('date', $date)->where('place', $place)

    – Boris Schegolev
    Nov 21 '18 at 14:51











  • Please improve your question as theplace attribute is not mentioned anywhere.

    – Yahya Uddin
    Nov 21 '18 at 15:06











  • @YahyaUddin: Yup, Bymistake added another method from the controller. Got it resolved. Thanks for looking into it

    – Nagendra Bhat
    Nov 21 '18 at 17:02



















  • And what exactly stops you from adapting the example you quoted? $count = DB::table('SamplingOrder')->where('date', $date)->where('place', $place)

    – Boris Schegolev
    Nov 21 '18 at 14:51











  • Please improve your question as theplace attribute is not mentioned anywhere.

    – Yahya Uddin
    Nov 21 '18 at 15:06











  • @YahyaUddin: Yup, Bymistake added another method from the controller. Got it resolved. Thanks for looking into it

    – Nagendra Bhat
    Nov 21 '18 at 17:02

















And what exactly stops you from adapting the example you quoted? $count = DB::table('SamplingOrder')->where('date', $date)->where('place', $place)

– Boris Schegolev
Nov 21 '18 at 14:51





And what exactly stops you from adapting the example you quoted? $count = DB::table('SamplingOrder')->where('date', $date)->where('place', $place)

– Boris Schegolev
Nov 21 '18 at 14:51













Please improve your question as theplace attribute is not mentioned anywhere.

– Yahya Uddin
Nov 21 '18 at 15:06





Please improve your question as theplace attribute is not mentioned anywhere.

– Yahya Uddin
Nov 21 '18 at 15:06













@YahyaUddin: Yup, Bymistake added another method from the controller. Got it resolved. Thanks for looking into it

– Nagendra Bhat
Nov 21 '18 at 17:02





@YahyaUddin: Yup, Bymistake added another method from the controller. Got it resolved. Thanks for looking into it

– Nagendra Bhat
Nov 21 '18 at 17:02












3 Answers
3






active

oldest

votes


















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.






share|improve this answer


























  • Thanks for the simplest answer. Choose this among all the others. Thanks for taking a time for the resolution. Solved my requirement.

    – Nagendra Bhat
    Nov 21 '18 at 17:04











  • @NagendraBhat Please mark this answer as correct if this solved your issue

    – Yahya Uddin
    Nov 21 '18 at 17:08











  • Yup. Thanks for the help

    – Nagendra Bhat
    Nov 21 '18 at 17:21



















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





share|improve this answer
























  • Thanks for taking a time for the resolution. Solved my requirement.

    – Nagendra Bhat
    Nov 21 '18 at 17:03



















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()






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414306%2fdatabase-data-field-check-before-data-insertion%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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.






    share|improve this answer


























    • Thanks for the simplest answer. Choose this among all the others. Thanks for taking a time for the resolution. Solved my requirement.

      – Nagendra Bhat
      Nov 21 '18 at 17:04











    • @NagendraBhat Please mark this answer as correct if this solved your issue

      – Yahya Uddin
      Nov 21 '18 at 17:08











    • Yup. Thanks for the help

      – Nagendra Bhat
      Nov 21 '18 at 17:21
















    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.






    share|improve this answer


























    • Thanks for the simplest answer. Choose this among all the others. Thanks for taking a time for the resolution. Solved my requirement.

      – Nagendra Bhat
      Nov 21 '18 at 17:04











    • @NagendraBhat Please mark this answer as correct if this solved your issue

      – Yahya Uddin
      Nov 21 '18 at 17:08











    • Yup. Thanks for the help

      – Nagendra Bhat
      Nov 21 '18 at 17:21














    0












    0








    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 '18 at 15:07

























    answered Nov 21 '18 at 15:01









    Yahya UddinYahya Uddin

    5,9571455112




    5,9571455112













    • Thanks for the simplest answer. Choose this among all the others. Thanks for taking a time for the resolution. Solved my requirement.

      – Nagendra Bhat
      Nov 21 '18 at 17:04











    • @NagendraBhat Please mark this answer as correct if this solved your issue

      – Yahya Uddin
      Nov 21 '18 at 17:08











    • Yup. Thanks for the help

      – Nagendra Bhat
      Nov 21 '18 at 17:21



















    • Thanks for the simplest answer. Choose this among all the others. Thanks for taking a time for the resolution. Solved my requirement.

      – Nagendra Bhat
      Nov 21 '18 at 17:04











    • @NagendraBhat Please mark this answer as correct if this solved your issue

      – Yahya Uddin
      Nov 21 '18 at 17:08











    • Yup. Thanks for the help

      – Nagendra Bhat
      Nov 21 '18 at 17:21

















    Thanks for the simplest answer. Choose this among all the others. Thanks for taking a time for the resolution. Solved my requirement.

    – Nagendra Bhat
    Nov 21 '18 at 17:04





    Thanks for the simplest answer. Choose this among all the others. Thanks for taking a time for the resolution. Solved my requirement.

    – Nagendra Bhat
    Nov 21 '18 at 17:04













    @NagendraBhat Please mark this answer as correct if this solved your issue

    – Yahya Uddin
    Nov 21 '18 at 17:08





    @NagendraBhat Please mark this answer as correct if this solved your issue

    – Yahya Uddin
    Nov 21 '18 at 17:08













    Yup. Thanks for the help

    – Nagendra Bhat
    Nov 21 '18 at 17:21





    Yup. Thanks for the help

    – Nagendra Bhat
    Nov 21 '18 at 17:21













    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





    share|improve this answer
























    • Thanks for taking a time for the resolution. Solved my requirement.

      – Nagendra Bhat
      Nov 21 '18 at 17:03
















    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





    share|improve this answer
























    • Thanks for taking a time for the resolution. Solved my requirement.

      – Nagendra Bhat
      Nov 21 '18 at 17:03














    1












    1








    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





    share|improve this answer













    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






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 21 '18 at 15:06









    nakovnakov

    2,639189




    2,639189













    • Thanks for taking a time for the resolution. Solved my requirement.

      – Nagendra Bhat
      Nov 21 '18 at 17:03



















    • Thanks for taking a time for the resolution. Solved my requirement.

      – Nagendra Bhat
      Nov 21 '18 at 17:03

















    Thanks for taking a time for the resolution. Solved my requirement.

    – Nagendra Bhat
    Nov 21 '18 at 17:03





    Thanks for taking a time for the resolution. Solved my requirement.

    – Nagendra Bhat
    Nov 21 '18 at 17:03











    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()






    share|improve this answer




























      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()






      share|improve this answer


























        0












        0








        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()






        share|improve this answer













        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()







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 15:30









        shahabphpshahabphp

        1115




        1115






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414306%2fdatabase-data-field-check-before-data-insertion%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]