简体   繁体   中英

Save date into database with Carbon

I'm still new in Laravel. Currently, I'm learning and working on Carbon. I already looking on every documentation and other solution but I still don't get it. I hope there is someone can teach and show me step by step how to correct or improve my code.

ComplaintController.php

public function store(Request $request)
    {
        if (count($request->defect_id) > 0) {
            foreach($request->defect_id as $item=>$v) {
                 $data = array(
                     'defect_id' => $request->defect_id[$item],
                     'image' => $filename,
                     'description' => $request->description[$item],
                     'report_by' => Auth::user()->id,
                     'created_at' => Carbon::today()->toDateString(),
                     'updated_at' => Carbon::now()->toDateTimeString()
                 );
                Complaint::insert($data);

I'm saving created_at field as date only, no time and it is in the format of (yy-mm-dd) .

index.blade.php

<div class="panel-body">
   <table class="table table-hover">
        <thead>
        <tr>
            <th>Defect Name</th>
            <th>Description</th>
            <th>Image</th>
            <th>Report Date</th>
            <th>Due Date</th>
        </tr>
        </thead>
        @foreach($complaint as $c)
           <tr>
              <td>{{$c->defect->name}}</td>
              <td>{{$c->description}}</td>
              <td><img src="{{ Storage::url('complaint/' . $c->image)}}" class="" alt="{{$c->image}}"></td>
              <td>{{$c->created_at->toDateString()}}</td>
              <td></td>
           </tr>
        @endforeach
   </table>
</div>

My date is still showing finely in the table as (yy-mm-dd) but I want the date to be in format of (dd/mm/yy) so I try to use this kind of code {{$c->created_at->toDateString()->format("dd/mm/yy")}} and there is error appeared which is Call to a member function format() on string . Later on, I need to add another field for duedate and use this function addDays(30) . So, what I need to do? I'm guessing that I need to put another function in my model but I can't figure out how to do it.

This should work...

To save the date in the format dd/mm/yy, I would use the following:

date('d-m-Y', strtotime(Carbon\Carbon::now()))

To reformat the date in a view, I would use:

date('d-m-Y', strtotime($c->created_at))

I prefer to use the base php functions wherever possible to keep things simple.

If someone knows a reason not to, please let me know.

As for the adding of days,

date('d-m-Y', strtotime($c->created_at) + (60 * 60 * 24 * 30))

Should work.

Most date related problems can be solved with strtotime(), its very useful. Be sure to read up on it here: https://www.php.net/manual/en/function.strtotime.php

As mentioned by @Tim Lewis as well, replace

{{ $c->created_at->toDateString()->format("dd/mm/yy") }}

to

{{ $c->created_at->format("dd/mm/yy") }}

As toDateString() converts it to string before changing the format.


You can also try using standard php format for datetime, instead of Carbon class,

{{ date("d/m/Y", strtotime($c->created_at)) }}  

As for your second question of you want to add new table data column ,

<tr>
  <td>{{ $c->defect->name }}</td>
  <td>{{ $c->description }}</td>
  <td><img src="{{ Storage::url('complaint/' . $c->image)}}" class="" alt="{{$c->image}}"></td>
  <td>{{ $c->created_at->format("dd/mm/yy") }}</td>
  <td>{{ $c->created_at->addDays(30)  }}</td>
</tr>

Casted date fields:

In Laravel the created_at and updated_at fields are casted as date fields already. So you don't have to transform this into date formats.

You can simply do

<td>{{ $c->created_at->format("d/m/y") }}</td>

Fields created_at and updated_at are also automatically filled So you don't need to fill this fields

You can simply do

$data = array(
    'defect_id' => $request->defect_id[$item],
    'image' => $filename,
    'description' => $request->description[$item],
    'report_by' => Auth::user()->id,
);
Complaint::create($data);

Extra date field duedate When you add custom date fields you need to cast them in the model to be date.

Add this in you model:

protected $dates = ['duedate'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM