Php Laravel 5收集问题:其中不等于
我目前正在开发一个用户可以插入excel文件的模式。如果记录是新的或与数据库中存在的记录相同,则系统的任务是上载和/或添加新的数据库记录。但它还需要一个delete函数来删除slug列与name列不相同的记录 目前我正在使用的是Laravel 5.3,这是我的控制器Php Laravel 5收集问题:其中不等于,php,laravel,laravel-5.3,laravel-collection,Php,Laravel,Laravel 5.3,Laravel Collection,我目前正在开发一个用户可以插入excel文件的模式。如果记录是新的或与数据库中存在的记录相同,则系统的任务是上载和/或添加新的数据库记录。但它还需要一个delete函数来删除slug列与name列不相同的记录 目前我正在使用的是Laravel 5.3,这是我的控制器 <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Product; use App\Http\Requests; use
<?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);
}
这是产品型号:
<?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',
];
}
我希望有人能帮我走出这个深坑。为了完成这项工作,我已经在互联网的浪潮中航行了大约12个小时
~nitsuJ变化
$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::whereRaw('slug != name')->get();
简单的,其中
不起作用,因为它比较产品。slug
与的“name”
(字符串)。试试这个
$product = Product::where('slug', $value->slug)->first();
$product_false = Product::whereRaw('slug != name')->get();
简单的,其中
不起作用,因为它比较了产品。slug
与“name”
(字符串)。我设法解决了它
我设法解决了它
集合、雄辩和查询生成器是不同的。集合提供了一系列方法来处理数组,而不是数据库或模型 在集合上下文中,
whereNot()
不可用
但同样的功能也可以通过whereNotIn('key',[value])
与
中排名不在(4)
集合中的情况相同,雄辩和查询生成器也不相同。集合提供了一系列方法来处理数组,而不是数据库或模型
在集合上下文中,whereNot()
不可用
但同样的功能也可以通过whereNotIn('key',[value])
与
相同,其中排名不在(4)
而不是=代码>一次尝试使用
替换“!=”结果是:$product\u false=product::all()->where('slug','!=','name')->get()代码>?不应该是$product\u false=product::all()->where('slug','!=',$value->slug)->get()嘿,谢谢,但是,它不起作用。取而代之的是:取而代之的是=代码>一次尝试使用
替换“!=”结果是:$product\u false=product::all()->where('slug','!=','name')->get()代码>?不应该是$product\u false=product::all()->where('slug','!=',$value->slug)->get()嘿,谢谢,但是,它不起作用。相反,我得到了:我以前试过。都是记录,;包括slug和name实际上相等的参数:这可能是因为第三个参数被解析为字符串,请尝试使用products.slug和products.name,而不仅仅是列名。您还可以使用,也许是更好的方法:$products_false=Product::whereRaw('slug!=name');product.slug/product.name result in:ur whereRaw建议结果:为了将来的参考,我只需要在产品模型上运行一个测试,并获取“slug”列与“name”列不相等的记录。这是因为,在使用whereRaw时,需要在其中附加->get(),现在只返回查询生成器。我以前试过。都是记录,;包括slug和name实际上相等的参数:这可能是因为第三个参数被解析为字符串,请尝试使用products.slug和products.name,而不仅仅是列名。您还可以使用,也许是更好的方法:$products_false=Product::whereRaw('slug!=name');product.slug/product.name result in:ur whereRaw建议结果:为了将来参考,我只需要在产品模型上运行一个测试,并获取“slug”列与“name”列不相等的记录。这是因为,在使用whereRaw时,需要向其追加->get(),因为现在只返回查询生成器。
$product = Product::where('slug', $value->slug)->first();
$product_false = Product::where('slug', '!=' , 'name')->get();
$product = Product::where('slug', $value->slug)->first();
$product_false = Product::whereRaw('slug != name')->get();
$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();
collect([
[
'name' => 'foo',
'rank' => 2
],[
'name' => 'bar',
'rank' => 3
],[
'name' => 'foobar',
'rank' => 4
],
])->whereNotIn('rank', [4])