It's supposed to be blank. How can I prevent this from happening? I am using this code to show the date
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;
}
}
I think there is something I need to do here:
$str = date('M. d, Y',strtotime($books));
Try like this:
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;
}
}
It is taking a default date which is Dec. 31, 1969 if you are returning false. I mean, if you are not finding any $chalet value. You can avoid it by returning a default date or you can check for returned false value and put empty date or a default date on your view.
If it do not solve your problem then check whether the $book is empty or not.
Update
Thank you for your compliment. Some more thoughts, you must not remove the else part. Because if your condition do not satisfy then nothing will be returned. So you will get nothing. For this you can return the current date date('M. d, Y') there (not sure if it matches your functionality). And it is better to set current date rather than using a default date. So your code could be something like this
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');
}
}