【问题标题】:Multiple orWheres wont working in Laravel多个或在 Laravel 中不工作的地方
【发布时间】:2019-05-13 11:45:51
【问题描述】:

大家好,我尝试了多个 where,其中 2 where 用于过滤,另外 2 where 用于搜索,但是当我添加这 2 个 orwhere 时,前两个在哪里不起作用,但后两个在工作,我不知道是什么问题,请帮助我

我试过这段代码,但它不起作用,也许我不知道如何使用它

$estudiantes= Usuario::
        where('rol', '=', 'ESTUDIANTE')
            ->whereHas('perfil', function($query) use ($data){
                if($data!="")
                    $query->Where('doc_identidad', '=' ,$data)
                        ->orWhere('nombres','like','%'.$data.'%')
                        ->orWhere('apellidos','like','%'.$data.'%');
                dd($query);
            })->get();

这是我的指标控制器

namespace App\Http\Controllers;

use App\Indicator;
use DB;
use Illuminate\Http\Request;
use Validator;

class IndicatorsController extends Controller
{
    public function index(Request $request)
    {
        if (request()->has('fin') OR request()->has('strat') OR request()->has('s')) {
            $joinindicators = DB::table('indicators')
                ->join('financial_categories', 'financial_categories.id', '=', 'indicators.financial_id')
                ->join('strategy_categories', 'strategy_categories.id', '=', 'indicators.strat_id')
                ->join('time_frame_categories', 'time_frame_categories.id', '=', 'indicators.timeframe_id')
                ->select('indicators.*', 'financial_categories.financial_type', 'strategy_categories.strategy',
                    'time_frame_categories.timeframe')
                ->where('financial_id', '=', request('fin'))->orwhere('strat_id', '=', request('strat'))
                ->orwhere('indicator_name', 'like', '%'.request('s').'%')
                ->orWhere('Description', 'like', '%'.request('s').'%')
                ->paginate(8)->appends([
                    'financial_id' => request('fin'),
                    'strat_id' => request('strat'),
                    //    'indicator_name' => request('s')
                ]);
        } else {

            $joinindicators = DB::table('indicators')
                ->join('financial_categories', 'financial_categories.id', '=', 'indicators.financial_id')
                ->join('strategy_categories', 'strategy_categories.id', '=', 'indicators.strat_id')
                ->join('time_frame_categories', 'time_frame_categories.id', '=', 'indicators.timeframe_id')
                ->select('indicators.*', 'financial_categories.financial_type', 'strategy_categories.strategy',
                    'time_frame_categories.timeframe')
                ->paginate(8);
        };

        $indis = Indicator::orderBy('created_at', 'desc')->paginate(8);

        return view('pages.index_indicator', compact('joinindicators', 'indis'));

} }

【问题讨论】:

    标签: laravel eloquent model


    【解决方案1】:

    我认为你应该尝试把你的“where”用小写。

    $query->where
    

    https://laravel.com/docs/5.8/queries#where-clauses

    【讨论】:

    • ->where('financial_id', '=',request('fin'))->orwhere('strat_id', '=',request('strat')) 这两个地方好像工作,但是当我添加两个 ->orwhere('indicator_name', 'like','%' .request('s'). '%') ->orwhere('Description', 'like','%' 。 request('s'). '%') 第一个不起作用,但最后两个起作用
    • @CalvinCalvento 你说“行不通”是什么意思?添加了预期的忽略值?
    • 当我选择一个产品时,它不会过滤它,它只会显示除类似产品之外的所有产品
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 2017-08-09
    • 2016-07-25
    • 2019-08-07
    • 1970-01-01
    • 2015-01-02
    相关资源
    最近更新 更多