【问题标题】:how to handle null value in Laravel Eloquent?如何在 Laravel Eloquent 中处理空值?
【发布时间】:2021-09-01 09:59:09
【问题描述】:

我是 laravel 的新手。实际上我想过滤一些关于 state 和 typeofsupply 列的数据。就像如果 state="south australia" 和 typeofsupply="Inverter & Storage" 然后向我显示这两个值上都存在的这些行。如果我像 state="south australia" 和 typeofsupply=Nul 进行过滤。那么这些行将显示谁南澳大利亚的存在状态和 typeofsupply 应该是列表中的任何一个(所有类型的供应)

public function show(Request $request){

    $typeofsupply='';
    $state='';
    if(!$request->all()==Null) {

        if(!$request->state==Null) {
            $state=$request->state;
        }
        if(!$request->typeofsupply==Null) {
            $typeofsupply=$request->typeofsupply;
        }

        $items = Postjob::where('state',$state)
                        ->where('typeofsupply',$typeofsupply)
                        ->get();
        dd($items);

【问题讨论】:

  • 如果您不希望 typeofsupply 成为查询的一部分,只需将 ->where('typeofsupply',$typeofsupply) 留在查询之外
  • @RiggsFolly 我想要两个都在我的查询中
  • 为什么,任何行实际上在 typeofsupply 中都包含 NULL?
  • 我正在处理此页面过滤器planet.solarmarkit.com/solar-jobs 如果用户可以在框上应用过滤器,则第二个框的值将返回 null

标签: mysql laravel-5 eloquent


【解决方案1】:

这就是我解决问题的方法......

$items = Postjob::when($state!=Null,function ($query) use ($state) {
                    return $query->where('state', $state);})->
                when($typeofsupply!=Null,function ($query) use ($typeofsupply) {
                    return $query->where('typeofsupply', $typeofsupply);})->get();

【讨论】:

  • 请在您的回答中提供更多详细信息。正如目前所写的那样,很难理解您的解决方案。
  • @Community 非常感谢您抽出宝贵时间。我解决了我的问题并附上了答案。
猜你喜欢
  • 1970-01-01
  • 2019-11-03
  • 2015-08-21
  • 2014-11-17
  • 1970-01-01
  • 1970-01-01
  • 2016-12-29
  • 2016-08-18
  • 1970-01-01
相关资源
最近更新 更多