前端的Laravel显示日期最接近今天


Laravel show date on front end which is closest to today

对于我的模型(车间),我有一个名为' date '的字段,这个输入向用户显示了这个车间的日期。我想通过后端输入多个日期(逗号分隔),并在前端向用户显示最接近当前日期的日期。在我之前的尝试中,我无法将数组保存到数据库中,因此无法在前端显示用户,这些日期之一。

是否有一种简单的方法来创建我上面提到的东西,它容易吗??

我以前有什么:

public function store()
    {
        if(Input::hasFile('file'))
        {
            $file               = Input::file('file');
            $destinationPath    = 'uploads/images/workshops/';
            $filename           = $file->getClientOriginalName();
            $upload_success     = $file->move($destinationPath, $filename);
        }
        $new_workshop = array(
            'concept'   => Input::get('concept'),
            'title'     => Input::get('title'),
            'body'      => Input::get('body'),
            'author'    => Input::get('author'),
            'slug'      => Str::slug(Input::get('title')),
            'image'     => str_replace('''', '/', $upload_success),
            $thedate = array();
            foreach(explode(',',Input::get('date')) as $date){
               array_push($thedate,$date);
            }
            'date'      => $thedate,
        );
        $rules = array(
            'title'     => 'required|min:3|max:255',
            'body'      => 'required|min:10',
            'date'      => 'required',
        );
        $validation = Validator::make($new_workshop, $rules);
        if ( $validation->fails() )
        {
            return Redirect::route('admin.workshops.create')->withErrors($validation)->withInput();
        }
        $workshop = new Workshop($new_workshop);
        $workshop->save(); 
        return Redirect::route('admin.workshops.index');
    }

您需要内爆数组。这将为您把它变成一个字符串。

多个输入;

<input name="date[]".... /> //one for one date 
<input name="date[]".... /> //one for another date

这一切都取决于你如何在页面上设置日期。只要日期名称中有date[],它就会填充Input::get('date');

然后改变;

$thedate = array();
foreach(explode(',',Input::get('date')) as $date){
     array_push($thedate,$date);
}
'date'      => $thedate,

'date' => implode(',',Input::get('date')),

保存的值将是'date','date'…这取决于你发布的日期。

如果您只使用一个输入并且用a分隔日期,那么您只需;

改变
$thedate = array();
foreach(explode(',',Input::get('date')) as $date){
     array_push($thedate,$date);
}
'date'      => $thedate,

'date' => Input::get('date'),