I'm trying to get columns with some if conditions like "if column is null, get other column instead".
My current code
$products = Product::whereHas('images', function($q) {
$q->where('published', 1)
})->get(['id as value', 'product_short_name as label']);
Need to make it like
if prdouct_short_name
is Null
get me product_name
as label
If you are using MySQL you can try to use 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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.