应该是空白的。我怎样才能避免这种情况的发生?我使用这个代码来显示日期
public function bookedOn($chalet_id) {
$chalet = 'App'Chalet::where('chalet_id', '=', $chalet_id)->get();
if ($chalet->count() > 0) {
$books = 'App'EventBookings::where('chalet_id', '=', $chalet[0]->chalet_id)->value('created_at');
$str = date('M. d, Y',strtotime($books));
return $str;
} else {
return false;
}
}
我想这里有些事情我需要做:
$str = date('M. ')d, Y ', strtotime(书)美元);
如果返回false,则默认日期为1969年12月31日。我的意思是,如果你没有找到任何$chalet值。您可以通过返回默认日期来避免它,或者您可以检查返回的false值并在视图中放置空日期或默认日期。
如果它不能解决你的问题,那么检查$book是否为空。
谢谢你的夸奖。多一些思想,就一定不要去掉别的部分。因为如果你的条件不满足,那么什么都不会退还。所以你什么也得不到。为此,您可以返回当前日期日期('M ')。d, Y')那里(不确定它是否符合您的功能)。最好设置当前日期,而不是使用默认日期。你的代码可以是这样的
public function bookedOn($chalet_id) {
$chalet = 'App'Chalet::where('chalet_id', '=', $chalet_id)->get();
if ($chalet->count() > 0) {
$books = 'App'EventBookings::where('chalet_id', '=', $chalet[0]->chalet_id)->value('created_at');
if(!empty($books)){
$str = date('M. d, Y',strtotime($books));
}else{
$str = date('M. d, Y');
}
return $str;
} else {
return date('M. d, Y');
}
}
试试:
public function bookedOn($chalet_id) {
$chalet = 'App'Chalet::where('chalet_id', '=', $chalet_id)->get();
if ($chalet->count() > 0) {
$books = 'App'EventBookings::where('chalet_id', '=', $chalet[0]->chalet_id)->value('created_at');
if ($books == '') {
// some default date
$books = '2010-01-01';
}
$str = date('M. d, Y',strtotime($books));
return $str;
} else {
return false;
}
}