在循环中向数组添加值

Iam working on a laravel project which stores values to a DB entry in loop on meeting certain conditions.

This first creates an array if the entry is for the first time and adds a value to it. Henceforth, it recalls the array and keeps adding values to it.

if(is_null($lead->shown_to)) {
    $a = array();
    array_push($a, "lead 1");
    $lead->shown_to = serialize($cart);
    $lead->save();
} else {
    $a=unserialize($lead->shown_to);
    array_push($a, "lead 2");
    $lead->shown_to = serialize($a);
    $lead->save();
}

To be able to create an array and add distinct elements to it repeatedly.

Is there a way to first check if the element exists in it or not. If it does, just move ahead, else add it?

Thanks in advance.

There're a couple of methods you can use.

You can first look for the value on the DB if exists using a column from the database like:

$result = Model::where( 'column', 'value' );

if ( $result ) {
  // update already exists
} else {
  // create one
}


// Retrieve flight by name, or create it if it doesn't exist...
$flight = App\Flight::firstOrCreate(['name' => 'Flight 10']);

// Retrieve by name, or instantiate...
$flight = App\Flight::firstOrNew(['name' => 'Flight 10']);

Also it depends what you are looking for as firstOrCreate persists the value into the DB where firstOrNew just creates a new instance where you need to call save()

to check a value exists in an array you can use array_search(). this will return the value if exists. if not it returns false.

if(!array_search('lead 2', $a)) {
    // array does't has 'lead 2' so,
    array_push('lead 2', $a);
}

In Laravel I would take advantage of the Collections because they have a lot of helpful methods to work with.

I would do something like this:

OPTION 1

//Depending on the value of $lead->show, initialize the cart variable with the serialization of the attribute or and empty array and transform it to a collection.

$cart = collect($lead->shown_to ? unserialize($lead->shown_to) : []);

//Ask if the collection doesn't have the given value. If so, added it.
if (!$cart->contains($your_value)) {
    $cart->push($your_value);
}

//Convert to array, serialize and store
$lead->shown_to = serialize($cart->toArray());
$lead->save();

OPTION 2

//Depending on the value of $lead->show, initialize the cart variable with the serialization of the attribute or and empty array and transform it to a collection.

$cart = collect($lead->shown_to ? unserialize($lead->shown_to) : []);

//Always push the value
$cart->push($your_value);

//Get the unique values, convert to an array, serialize and store
$lead->shown_to = serialize($cart->unique()->toArray());
$lead->save();

You can get more creative using the collections and they read better on Laravel

I think you can use updateOrCreate, if not exists it will create now, if exists, it will update it, so you can keep assigning value to shown_to property

$lead= App\Lead::updateOrCreate(
    ['name' => 'Lead 1'],
    ['shown_to' => serialize($a)]
);

if you wan to keep the existing shown_to better to use json data, so that you can do like

$lead= App\Lead::updateOrCreate(
        ['name' => 'Lead 1'],
        ['shown_to' => json_encode(array_push(json_decode($a), $newData))]
    );