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.
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!
To split the year and month values you can use the explode function.
$input = Input::get('date');
$explode = explode('-', $input);
$year = $explode[0];
$month = $explode[1];