Get data and insert

1

Maybe what you ask is very basic, but I'm getting involved in this: I have a list of Ejm products

What I need is to know if this product exists in my BDD and if I can enter it, they will tell me that is easy, but my way of inserting is the following, in order to create the code I keep the frequency + sequence.

That is why I consult the database to see if the code exists BY EJM 0021 to 0029, in this case there are 2 data that are 0021 and 0022, but if not, I need to be able to enter 0023, 0024, 0025 correlatively .

I have the following code where I ask when entered data exist belonging to the frequency and sequence

$nCod=Productos::where('frecuencia','=',$frecuencia)
            ->where('secuencia', '=', $secuencia)
            ->count();

if($nCod<10){
           $buscar=Productos::where('frecuencia','=',$frecuencia)
            ->where('secuencia', '=', $secuencia)
               ->get();

        }else{
            echo "Error, no se puede ingresar.";
        }

How can I do the cycle to know if the code exists, but enter it consecutively?

    
asked by Karli 09.11.2018 в 15:47
source

1 answer

0

I see that you use Laravel so I recommend the firstorcreate() method that will first look for a match with some record, if it does not find a match then it will proceed to make a new high.

EXAMPLE

$data = ModelName::firstOrCreate([
    'email' => '[email protected]'
], [
    'comunidad' => 'SO en español',
    'usuarios' => 'muchisimos',
    'calidad' => 'toda la del mundo' 
]);

In the example I show you, look for the match by email, if you can not find it then register the%% of%

    
answered by 09.11.2018 в 15:51