doumi7854 2015-06-06 18:13
浏览 147
已采纳

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

I have a laravel 5 application where i need to take the month and the year from the input type month. i have stored these attributes in different columns because i have to search records depending on the year.I'm new to laravel and i'm wondering if there is any substring method with delimiters in laravel. i searched in the documentation but no luck.

This is the input where i get the month from:

<input name="month-' + meta.row + '" class="form-control month" type="month" />

The value that is posted from the above input is in the following format:

2015-06

And this is the migration file

Schema::create('reports', function(Blueprint $table)
    {
        /*other columns*/
        $table->integer('month',false,false,'2');
        $table->integer('year',false,false,'4');
        /*other columns*/
    });

Also i am not sure if the above syntax is correct. I tried to put the int fixed size at the end. I'm using sql server with it.

Any help would really be appreciated, even if it's just a hint.

  • 写回答

2条回答 默认 最新

  • dongyou6909 2015-06-06 19:17
    关注

    A lot of assumptions made here so adjust your code accordingly. If you don't understand something please leave a comment.

    Before you store anything you need to have a route which your form will post the data to, you could do something like this for example:

    routes.php

    Route::post('report', [
        'uses' => 'ReportsController@store'
    ]);
    

    ReportsController.php

    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();
    }
    

    EDIT: to deal with multiple date fields

    Update html in view as so:

    <!-- 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" />
    

    Controller method

    $dateInputs = $request->get('date'); // get the date input array
    $reports = []; // this will keep what we input in our DB
    
    // loop through the date input array
    foreach($dateInputs as $dateInput)
    {
        $date      = explode('-', $dateInput); 
        $year      = $date[0];
        $month     = $date[1];
    
        $reports[] = \Report::create([
                'month' => $month,
                'year'  => $year
            ]);
    }
    
    return $reports; // this is what has been input! YAY!
    

    Important point: In the question you said you were looking for a solution for splitting the string from Laravel - now Laravel does provide a lot of helper methods as part of the framework and some of them are even for string manipulation, but I believe this is the wrong way of approaching your issue. You definitely should be looking at how PHP can help you do this as it's a pretty trivial issue and not something you would require a framework for, so please note that the explode method is a method as part of the PHP programming language and not Laravel. Perhaps you can do some research into why someone would use a framework to better understand the point I'm making.

    Hope that helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器