最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Laravel eloquent version change - Stack Overflow

programmeradmin0浏览0评论

Check .git for composers.lock

I have the following code:

$where = [
    ['warehouse_id', $warehouse->id],
    ['product_id', $product->id],
    ['section_id', $sectionId]
];
$orWhere = [
    ['to_warehouse_id', $warehouse->id],
    ['product_id', $product->id],
    ['section_id', $sectionId]
];

$transactions = InventoryTransaction::where($where)->orWhere($orWhere)->get();

So query is from print_r(InventoryTransaction::where($where)->orWhere($orWhere)->toSql());

select*from `inventory_transactions`where((`warehouse_id`=?and `product_id`=?and `section_id`=?)or(`to_warehouse_id`=?and `product_id`=?and `section_id`=?))and `inventory_transactions`.`deleted_at`is null

I did composer update and had to change the above to:

$transactions = InventoryTransaction::where($where)->get();
$t = InventoryTransaction::where($orWhere)->get();
$transactions = $transactions->merge($t);

So query became from print_r(InventoryTransaction::where($where)->orWhere($orWhere)->toSql());

select * from `inventory_transactions` where ((`warehouse_id` = ? and `product_id` = ? and `section_id` = ?) or (`to_warehouse_id` = ? or `product_id` = ? or `section_id` = ?)) and `inventory_transactions`.`deleted_at` is null

Or else I will have duplicates, I had to revert the composer for it to work!! Anything I should do to keep my app updated as in do composer update and keep the above code. Note there are tons of places that need to be changed if that is fixed. This way I can't update to future versions!!

Check https://github/hilal-najem3/composers.git for composers.lock

I have the following code:

$where = [
    ['warehouse_id', $warehouse->id],
    ['product_id', $product->id],
    ['section_id', $sectionId]
];
$orWhere = [
    ['to_warehouse_id', $warehouse->id],
    ['product_id', $product->id],
    ['section_id', $sectionId]
];

$transactions = InventoryTransaction::where($where)->orWhere($orWhere)->get();

So query is from print_r(InventoryTransaction::where($where)->orWhere($orWhere)->toSql());

select*from `inventory_transactions`where((`warehouse_id`=?and `product_id`=?and `section_id`=?)or(`to_warehouse_id`=?and `product_id`=?and `section_id`=?))and `inventory_transactions`.`deleted_at`is null

I did composer update and had to change the above to:

$transactions = InventoryTransaction::where($where)->get();
$t = InventoryTransaction::where($orWhere)->get();
$transactions = $transactions->merge($t);

So query became from print_r(InventoryTransaction::where($where)->orWhere($orWhere)->toSql());

select * from `inventory_transactions` where ((`warehouse_id` = ? and `product_id` = ? and `section_id` = ?) or (`to_warehouse_id` = ? or `product_id` = ? or `section_id` = ?)) and `inventory_transactions`.`deleted_at` is null

Or else I will have duplicates, I had to revert the composer for it to work!! Anything I should do to keep my app updated as in do composer update and keep the above code. Note there are tons of places that need to be changed if that is fixed. This way I can't update to future versions!!

Share Improve this question edited Feb 1 at 19:38 Hilal Najem asked Feb 1 at 18:04 Hilal NajemHilal Najem 1775 silver badges17 bronze badges 10
  • 1 Can you be more specific? What is the exact version of Laravel you had before and what version did you update to after. If this was a minor to minor update it sounds like there was a bug introduced – apokryfos Commented Feb 1 at 18:45
  • It would also help a lot to add what queries were executed on the working version and on the new version, see how to do that here – matiaslauriti Commented Feb 1 at 18:51
  • I will update the question with both composer.lock – Hilal Najem Commented Feb 1 at 18:52
  • I couldn't add composer lock but I provided queries usingprint_r(InventoryTransaction::where($where)->orWhere($orWhere)->toSql()); – Hilal Najem Commented Feb 1 at 19:19
  • Check this github/hilal-najem3/composers.git for composers – Hilal Najem Commented Feb 1 at 19:37
 |  Show 5 more comments

2 Answers 2

Reset to default 1

you issue is mostly due to not specifying much about the relation of your filters, and since you don't need to duplicate product and warehouse bc you check for the same ID, you can be more explicit like so:

InventoryTransaction::query()
  ->where('product_id', $product->id)
  ->where('section_id', $sectionId)
  ->where(function (Builder $query) use ($warehouse) {
    $query->where('warehouse_id', $warehouse->id)
      ->orWhere('to_warehouse_id', $warehouse->id);
  })
  ->get();

resulting in:

select * from `inventory_transactions` where `product_id` = ? and `section_id` = ? and (`warehouse_id` = ? or `to_warehouse_id` = ?) and `inventory_transactions`.`deleted_at` is null

The solution is to create a CustomQueryBuilder:

<?php

namespace App\Database;

use Illuminate\Database\Eloquent\Builder;

class CustomQueryBuilder extends Builder
{
    public function orWhere($column, $operator = null, $value = null, $boolean = 'or')
    {
        // If $column is an array (multiple conditions), wrap it properly
        if (is_array($column)) {
            return parent::orWhere(function ($query) use ($column) {
                $query->where($column);
            });
        }

        return parent::orWhere($column, $operator, $value, $boolean);
    }
}

And the n create a new Model called BaseModel which extends Model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Database\CustomQueryBuilder;

class BaseModel extends Model
{
    public function newEloquentBuilder($query)
    {
        return new CustomQueryBuilder($query);
    }
}

And extend from it instead of Model like class InventoryTransaction extends BaseModel

发布评论

评论列表(0)

  1. 暂无评论