使用什么作为我的默认值来实现正确的laravel选择在哪里?

问题描述 投票:0回答:1

拜托,我正在写一个函数,我需要指导

function($file_name='*'){
    File::where('filename',$file_name)->get();
}

我希望文件名在未定义文件名时拉出数据库表中的所有文件名列,如果是,则应使用该值来提取正确的数据。

我的问题是,我应该在函数输入中使用什么作为filename的默认值来工作?

即使是原始的SQL我会欣赏

sql laravel select eloquent where
1个回答
0
投票

首先,你缺少你的功能名称

function getFile($file_name = null){

    $q = File::query();

    if($file_name == null ){
       // Do nothing this will get all files at the end since you haven't applied a where clause.
    }else{
        $q = File::where('filename',$file_name);
    }

    $result = $q->get();

    return $result;
}
© www.soinside.com 2019 - 2024. All rights reserved.