Php laravel 5中的日期格式(或子字符串??)

Php laravel 5中的日期格式(或子字符串??),php,laravel,laravel-5,Php,Laravel,Laravel 5,我有一个laravel 5应用程序,其中我需要从输入类型月份中提取月份和年份。我将这些属性存储在不同的列中,因为我必须根据年份搜索记录。我是laravel的新手,我想知道laravel中是否有带分隔符的子字符串方法。我查了一下文件,但没找到 这是我从中获取月份的输入: <input name="month-' + meta.row + '" class="form-control month" type="month" /> 这是迁移文件 Schema::create('repor

我有一个laravel 5应用程序,其中我需要从输入类型月份中提取月份和年份。我将这些属性存储在不同的列中,因为我必须根据年份搜索记录。我是laravel的新手,我想知道laravel中是否有带分隔符的子字符串方法。我查了一下文件,但没找到

这是我从中获取月份的输入:

<input name="month-' + meta.row + '" class="form-control month" type="month" />
这是迁移文件

Schema::create('reports', function(Blueprint $table)
    {
        /*other columns*/
        $table->integer('month',false,false,'2');
        $table->integer('year',false,false,'4');
        /*other columns*/
    });
我也不确定上面的语法是否正确。我试图把int固定大小放在末尾。我使用的是sql server


任何帮助都将不胜感激,即使只是一个提示。

要分割年值和月值,您可以使用该函数

$input = Input::get('date');
$explode = explode('-', $input);
$year = $explode[0];
$month = $explode[1];

这里做了很多假设,所以请相应地调整代码。如果你不明白,请留下评论

在存储任何需要表单将数据发布到的路由之前,可以执行以下操作,例如:

routes.php

Route::post('report', [
    'uses' => 'ReportsController@store'
]);
public function store(Request $request)
{
    // date should be the name of your input in your form, 
    // i.e. whatever is in the "name" attribute
    $dateInput = $request->get('date');
    // or you could do $dateInput = \Input::get('date') if you're not injecting Illuminate\Http\Request
    $date      = explode('-', $dateInput); 
    $year      = $date[0];
    $month     = $date[1];

    // I am assuming your model is named Report and you're using Eloquent
    \Report::create([
        'month' => $month,
        'year'  => $year
    ]);

    // finished so do something here
    return redirect()->back();
}
ReportsController.php

Route::post('report', [
    'uses' => 'ReportsController@store'
]);
public function store(Request $request)
{
    // date should be the name of your input in your form, 
    // i.e. whatever is in the "name" attribute
    $dateInput = $request->get('date');
    // or you could do $dateInput = \Input::get('date') if you're not injecting Illuminate\Http\Request
    $date      = explode('-', $dateInput); 
    $year      = $date[0];
    $month     = $date[1];

    // I am assuming your model is named Report and you're using Eloquent
    \Report::create([
        'month' => $month,
        'year'  => $year
    ]);

    // finished so do something here
    return redirect()->back();
}
编辑:处理多个日期字段

视图中更新html,如下所示:

<!-- with the name value followed by [] it will pass value as an array, you can have as many of these as you like -->
<input name="date[]" data-month="month-' + meta.row + '" class="form-control month" type="month" />
<input name="date[]" data-month="month-' + meta.row + '" class="form-control month" type="month" />
<input name="date[]" data-month="month-' + meta.row + '" class="form-control month" type="month" />
重要提示:在问题中,您说您正在寻找从Laravel中拆分字符串的解决方案-现在Laravel确实提供了许多辅助方法作为框架的一部分,其中一些方法甚至用于字符串操作,但我相信这是处理问题的错误方法。你肯定应该看看PHP是如何帮助你做到这一点的,因为这是一个非常小的问题,而不是你需要一个框架,所以请注意explode方法是PHP编程语言的一部分,而不是Laravel。也许你可以研究一下为什么有人会使用框架来更好地理解我的观点


希望有帮助

你的问题我不清楚。您说您想连接,但听起来好像您想在将输入值插入DB之前分离。那么,您是想从数据库中获取一些数据,月份和年份,然后将它们连接起来,还是想在插入数据库之前将它们分开?@haakym对不起,我的错。我的意思是子串。不连接。在将它们插入DBS之前,我试图将它们分开。那么,在插入DB之前,您试图拆分一个字符串吗?你看过《爆炸》吗?如果你有一个像
$date=“2015-10”
这样的字符串,你可以做
$dateSplit=explode(“-”,$date)
,它将把它分割成一个数组,因此如果你访问
$dateSplit[0]
它将给你一年,而
$dateSplit[1]
它将给你一个月。这是你想做的还是我完全误解了?这正是我需要的。我必须把它写在一个函数的模型里,对吗?我是laravelWell的新手,通常你会有一个控制器,它有一个插入db的方法,你可以使用一个模型通过create方法插入。你现在有控制器吗?另一个问题。在
$dateInput=$request->get('date')自动生成名称,因为数据是从表中收集的。你知道怎么做吗?是否可以在其中使用id而不是name?您必须使用name属性,因为提交表单就是这样工作的。它是表单上唯一的日期输入吗?如果是,您可以这样做:
。希望这有意义!?有多个日期输入,如果我更改名称,则只会提交最后一行尝试
,然后日期将作为一个数组,所有输入都命名为
date[]
。这有帮助吗?这是我从json
“month”:{“0”:“2015-06”,“2”:“2015-06”,“1”:“2015-06”},“value-0”:“comment-0”:“value-2”:“comment-2”:“value-1”:“comment-1”:“
我不知道它是否会起作用,因为这将在稍后直接转到db。”。。我将其他输入值留空