Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel:使用Elequent的条件选择列_Php_Laravel_Eloquent - Fatal编程技术网

Php Laravel:使用Elequent的条件选择列

Php Laravel:使用Elequent的条件选择列,php,laravel,eloquent,Php,Laravel,Eloquent,我试图获取带有一些if条件的列,比如“如果列为null,则获取其他列” 我当前的代码 $products = Product::whereHas('images', function($q) { $q->where('published', 1) })->get(['id as value', 'product_short_name as label']); 需要让它像 如果prdouct\u short\u name为Nullget meproduct\u nameasl

我试图获取带有一些if条件的列,比如“如果列为null,则获取其他列”

我当前的代码

$products = Product::whereHas('images', function($q) {
    $q->where('published', 1)
})->get(['id as value', 'product_short_name as label']);
需要让它像


如果
prdouct\u short\u name
Null
get me
product\u name
as
label
如果您使用的是MySQL,您可以尝试使用rawSelect

Product::whereHas('images', function($q) {
    $q->where('published', 1)
})->selectRaw('id as value, IF (product_short_name IS NULL, product_name, product_short_name) as label')->get();

如果您使用的是MySQL,那么可以尝试使用rawSelect

Product::whereHas('images', function($q) {
    $q->where('published', 1)
})->selectRaw('id as value, IF (product_short_name IS NULL, product_name, product_short_name) as label')->get();