-1

I have a table named time_entries and the column name is time_start.

I'm inserting Carbon::now(); on it. The data stored in the column is 2021-08-26 09:51:37 and blade.php shows same result.

Model TimeEntry:

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class TimeEntry extends Model
{
    use HasFactory;

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function getTimeStartAttribute($value)
    {
        return Carbon::createFromFormat('H:i:s', $value)->TimeString();
    }
}

Controller:

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\TimeEntry;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AttendanceController extends Controller
{
    public function index()
    {
        $data = TimeEntry::where('user_id', Auth::user()->id)->paginate(2);

        return view('Admin.Attendance-History-page', compact('data'));
    }
}

Blade view:

<tbody id="table_data">
    @foreach ($data as $punch)
        <tr>
            <td>{{ $punch->user->name }}</td>
            <td>{{ $punch->user->employee_id }}</td>
            <td>19 Feb 2019</td>
            <td>{{ $punch->time_start }} </td>
            <td>{{ $punch->time_end }}</td>
            <td>9 hrs</td>
            <td>
                <span class="badge bg-inverse-success">Perfect</span>
            </td>
            <td>0</td>
        </tr>
    @endforeach
</tbody>

I want to know how can I format the output from 2021-08-26 09:51:37 to something like 09:51:AM in Blade?

Thank you for your help.

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
kealaxshan
  • 338
  • 2
  • 14
  • What have you tried? It should work with the date() and strtotime() functions, just like it would in any PHP file. – M. Eriksson Aug 31 '21 at 06:46
  • no this is a php framework so we have to get form models – kealaxshan Aug 31 '21 at 06:48
  • 1
    Yes, I know it's a framework, but you do know that you can use native PHP functions in frameworks as well, right? Just create a method in the model that returns the date you want in the format you want (using date() and strtotime()). When you ask a question here, don't just ask us to fix it for you without showing us that you at least made an attempt. We're here to help you sort out issues you might have with your attempt, not to write it all for you (which an answer to this would require us to) – M. Eriksson Aug 31 '21 at 06:52
  • thank you but this is my first project on laravel. i don't know to how to get data attribute form database to the models and the model to videw – kealaxshan Aug 31 '21 at 07:06
  • But that's literally what the posted code does? – M. Eriksson Aug 31 '21 at 07:08
  • I'm explain this code get data form the database and show to the view call Attendance-History-page.blade.php. it show what is in the database exeat result but i what to change a particular data row attribute form the model and sent it to the view. – kealaxshan Aug 31 '21 at 07:15
  • Your `$data` variable contains a collection of `TimeEntry` models. In your foreach loop in the view, you're iterating through the collection so each `$punch` contains a single `TimeEntry` model. So if you add a method to that model that returns the data you want (the time in the format you want), you just call it with: `$punch->yourMethod()` in the view. – M. Eriksson Aug 31 '21 at 07:23
  • thank you my brother Mr.Magnus Eriksson it work thank you so much – kealaxshan Aug 31 '21 at 07:30

1 Answers1

1

There is a straight forward solution for this using the PHP date() method.

{{ date('h:m A', strtotime($punch->time_start)) }}

This will output 09:51 AM

If you want to output 09:51:AM then just add : after the m and remove the space, like so:

{{ date('h:m:A', strtotime($punch->time_start)) }}
Gass
  • 7,536
  • 3
  • 37
  • 41
  • Author can also take advantage of [`casting`](https://laravel.com/docs/8.x/eloquent-mutators#date-casting), so it is automatic (no need to do `date(...)` everytime, author would be able just to do `{{ $punch->time_start->format('h:m:A') }}`). – matiaslauriti Aug 31 '21 at 08:39