Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 有没有办法加入拉威尔的两个系列?_Php_Laravel_Laravel 6 - Fatal编程技术网

Php 有没有办法加入拉威尔的两个系列?

Php 有没有办法加入拉威尔的两个系列?,php,laravel,laravel-6,Php,Laravel,Laravel 6,假设我从HTTP请求中检索到一个数组: $request['sports'] => [0 => 'basketball', 1 => 'bowling', 2 => 'Tennis']; 然后,在我的数据库中检索到以下集合: $sports = [0 => 'basketball', 1 => 'bowling', 3 => 'boxing'] 在laravel中是否有一个函数,其中“拳击”运动数据库将被删除,因为在HTTP请求的集合中找不到它。“网

假设我从HTTP请求中检索到一个数组:

$request['sports'] => [0 => 'basketball', 1 => 'bowling', 2 => 'Tennis'];
然后,在我的数据库中检索到以下集合:

$sports = [0 => 'basketball', 1 => 'bowling', 3 => 'boxing']

在laravel中是否有一个函数,其中“拳击”运动数据库将被删除,因为在HTTP请求的集合中找不到它。“网球”将添加到数据库中,因为它包含在HTTP请求的集合中。“basketball”和“bowling”没有任何操作,因为它们都在请求和数据库中找到了?

您可以使用collect()helper从数组中创建集合,然后筛选所需的元素-这些元素在一个数组中,而不在另一个数组中。以下是一个例子:

$input = collect(['basketball', 'bowling', 'Tennis']);
$database = collect(['basketball', 'bowling', 'boxing']);

$added = $input->diff($database);
$removed = $database->diff($input);

$database->concat($added)->diff($removed);

// ['basketball', 'bowling', 'Tennis'];
    $request['sports'] = [0 => 'basketball', 1 => 'bowling', 2 => 'Tennis'];
    $db = [0 => 'basketball', 1 => 'bowling', 3 => 'boxing'];

    // create collections
    $requestCollection = collect($request['sports']);
    $dbCollection = collect($db);

    // filter items from db colection, that are not in request collection
    $toDelete = $dbCollection->filter(function ($item) use ($requestCollection) {
        return !$requestCollection->contains($item);
    })->each(function ($item){
        // todo: delete them
    });
    // filter items from request colection, that are not in db collection
    $toAdd = collect($requestCollection)->filter(function ($item) use ($dbCollection) {
        return !$dbCollection->contains($item);
    })->each(function ($item){
        // todo: add them
    });

    dd($toDelete, $toAdd);
dd将打印出:

Illuminate\Support\Collection {#1953
  #items: array:1 [
    3 => "boxing"
  ]
}
Illuminate\Support\Collection {#1954
  #items: array:1 [
    2 => "Tennis"
  ]
}

您可以使用collect()helper从数组中创建集合,然后筛选所需的元素—这些元素在一个数组中,而不是在另一个数组中。以下是一个例子:

    $request['sports'] = [0 => 'basketball', 1 => 'bowling', 2 => 'Tennis'];
    $db = [0 => 'basketball', 1 => 'bowling', 3 => 'boxing'];

    // create collections
    $requestCollection = collect($request['sports']);
    $dbCollection = collect($db);

    // filter items from db colection, that are not in request collection
    $toDelete = $dbCollection->filter(function ($item) use ($requestCollection) {
        return !$requestCollection->contains($item);
    })->each(function ($item){
        // todo: delete them
    });
    // filter items from request colection, that are not in db collection
    $toAdd = collect($requestCollection)->filter(function ($item) use ($dbCollection) {
        return !$dbCollection->contains($item);
    })->each(function ($item){
        // todo: add them
    });

    dd($toDelete, $toAdd);
dd将打印出:

Illuminate\Support\Collection {#1953
  #items: array:1 [
    3 => "boxing"
  ]
}
Illuminate\Support\Collection {#1954
  #items: array:1 [
    2 => "Tennis"
  ]
}