Laravel 5集合问题:哪里不等于

时间:2016-09-16 07:35:21

标签: php laravel laravel-5.3 laravel-collection

我目前正在处理用户可以插入excel文件的模式。如果记录是新的或与数据库中存在的记录相同,则系统的任务是上载和/或添加新的数据库记录。但它还需要一个删除函数来删除那些slug列与name列不同的记录。

目前我正在使用 Laravel 5.3 ,这是我现在的控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;
use App\Http\Requests;
use Illuminate\Support\Facades\DB;
use Input;
use Maatwebsite\Excel\Facades\Excel;

class ProductsController extends Controller {

public function importExcel(Request $request) {
    if (Input::hasFile('productFile')) {
        $path = Input::file('productFile')->getRealPath();
        $checkbox = Input::get('productCheckbox');
        $data = Excel::load($path, function($reader) {
        })->get();

        if (!empty($data) && $data->count()) {
            foreach ($data as $key => $value) {
                $product = Product::all()->where('slug', $value->slug)->first();
                $product_false = Product::all()->where('slug', '!=' , 'name')->get();

                if ($product_false !== null){
                    //delete row if slug does not matches name
                    dd($product_false);
                }

上面的dd返回所有产品,因此集合查询无法正常工作(请参阅下面我尝试在此集合中运行的原始SQL)

                if ($product !== null) {
                    //update row if exist
                    $product->name = $value->name;
                    $product->description = $value->description;
                    $product->price = $value->price;
                    $product->save();
                } else {
                    //add new row if not exist
                    $product = new Product;
                    $product->slug = $value->slug;
                    $product->name = $value->name;
                    $product->description = $value->description;
                    $product->price = $value->price;
                    $product->save();
                }

            }
            header("Location: /products");
        }
    }
}

}

这是产品型号

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'slug', 'name', 'description', 'price',
];
}

这是我基本上希望在集合中使用的PHPMyAdmin原始SQL(可以工作):

SELECT * FROM `products` WHERE `slug` != `name`

我希望有人可以帮助我摆脱困境。为了完成这项工作,我已经在互联网的浪潮中航行了大约12个小时。

~nitsuJ

4 个答案:

答案 0 :(得分:1)

更改

$product = Product::all()->where('slug', $value->slug)->first();
$product_false = Product::all()->where('slug', '!=' , 'name')->get();

$product = Product::where('slug', $value->slug)->first();
$product_false = Product::where('slug', '!=' , 'name')->get();

答案 1 :(得分:0)

试试这个

$product = Product::where('slug', $value->slug)->first();
$product_false = Product::whereRaw('slug != name')->get();

简单where在将products.slug"name"(字符串)进行比较时无法正常工作。

答案 2 :(得分:0)

我设法解决它。

$data = Excel::load($path, function($reader) {

            $importedSlugs = $data->select(array('slug'))->toArray();
                    //collection of imported slugs
                    $collectionOfImportedSlugs = collect($importedSlugs)->flatten()->all();

                    //get all product slugs
                    $productSlugs = Product::all()->pluck('slug');

                    //get all different slugs!
                    $diffSlugsArray = $productSlugs->diff($collectionOfImportedSlugs)->all();
                    //dd($diffSlugsArray);

                    foreach ($diffSlugsArray as $diffSlug) {
                        $product_false = Product::all()->where('slug',     $diffSlug)->first();

                        echo $product_false->slug . 'has been deleted!';

                        $product_false->delete();
                    }
        })->get();

答案 3 :(得分:0)

集合,雄辩的和查询生成器是不同的。集合提供了一堆方法来处理数组,而不是数据库或模型。

在集合上下文中whereNot()不可用。

但是可以通过whereNotIn('key', [value])

实现相同的功能
collect([
    [
      'name' => 'foo',
      'rank' => 2
    ],[
      'name' => 'bar',
      'rank' => 3
    ],[
      'name' => 'foobar',
      'rank' => 4
    ],
 ])->whereNotIn('rank', [4])

where rank not in (4)

相关问题