drb88830 2016-09-16 07:35
浏览 72
已采纳

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

I am currently working on a modal where a user can insert an excel file. The task of the system is to upload and/or add a new database record if the records are new or identical to what exists in the database. BUT it also needs a delete function for getting rid of those records where the slug column is not identical to the name column.

At the moment I am using Laravel 5.3, and this is my controller as it is now:

<?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);
                }

The dd above returns all products, so the collection query is not working properly (see below for the raw SQL that I am trying to run in this collection)

                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");
        }
    }
}

}

This is the Product model:

<?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',
];
}

Here is the PHPMyAdmin raw SQL (which works) that I basically am looking for to use in the collection:

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

I hope someone can help me out from this pit. I have been sailing the waves of the internet for about 12 hours now just to get this done.

~ nitsuJ

  • 写回答

4条回答 默认 最新

  • doucheyi1347 2016-09-16 13:26
    关注

    I managed to solve it.

    $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条)

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊