如果没有退出数据,则firstOrFail()不起作用

时间:2018-07-01 16:43:23

标签: php laravel database-design eloquent

如果数据库为空,则此firstOrFail()方法向我显示错误。数据库为空时如何访问我的视图。

$tag = Admin::where('option_name','tag')->firstOrFail();
        return view('admin.products')->with([
            'tag'  =>  $tag->option_value,
        ]);

或者我想这样做

   try{

        $tag = Admin::where('option_name','tag')->firstOrFail();
        dd($tag);
    }catch (ErrorException $e){
        echo "xxxx";
    }

2 个答案:

答案 0 :(得分:0)

 $tag = Admin::whereOptionName('tag')->get();
        return view('admin.products')->with([
            'tag'  =>  $tag->first()->option_value,
        ]);

答案 1 :(得分:0)

您可以将此代码与默认值结合使用:

select a.*
from arch a
where not exists (select 1
                  from arch a2
                  where a2.arch_number not like '%/%' and
                        a.arch_number like a2.arch_number || '/%'
                 );

$tag=Admin::whereOptionName('tag')->first(); return view('admin.products')->with([ 'tag' => collect($tag)->get('option_value','default value'), ]); 中,您可以通过view获得价值