Laravel Pivot table extra date-time field format (Not timestamp) using carbon object

Ravi

I have a pivot table ('offer_usage_detail') in my app, table having 4 field as below

id          int           AutoIncrement
user_id     int           reference to user table
offer_id    int           reference to offer table
use_date    date-time     Store date time when user use offer

I need to show list of user who used offer with date and time in d-m-Y H:i format.

So i added below code in my offer model

 public function user() {
    return $this->belongsToMany('User', 'offer_usage_detail', 'offer_id', 'user_id')->withPivot('use_time');
}

Currently i m using core php's date function with strtotime to format date time, But i want to know is there any way to convert Pivot tables date time fields to carbon object.

I tried putting use_time field in Offer Model's protected $dates = array('created_at','use_time'); but it didn't worked.

Is it possible to convert extra pivot table column to carbon object if column is date time field?

Jarek Tkaczyk

The best solution I would suggest is creating custom pivot model for this relation:

// Offer model (the same goes for the User model, but change to 'instanceof Offer`
public function newPivot(Eloquent $parent, array $attributes, $table, $exists)
{
    if ($parent instanceof User) return new OfferUserPivot($parent, $attributes, $table, $exists);

    return parent::newPivot($parent, $attributes, $table, $exists);
}

// OfferUserPivot
use Illuminate\Database\Eloquent\Relations\Pivot;

class OfferUserPivot extends Pivot {
   // override either property:
   protected $dates = ['use_time'];

   // or method:
   // public function getDates()
   // {
   //   return ['use_time']; // and other columns like created_at etc if you like
   // }
}

// Then you can do this:
$user->offers->first()->pivot->useTime; // Carbon object
$offer->users->first()->pivot->useTime; // Carbon object

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Convert a mySQL date to a British time format in Laravel 5.2 using Carbon?

Carbon unexpected date-time format in Laravel

Save date to MySQL format by using Laravel Carbon

Laravel Carbon format date

Using Carbon in Laravel to extract and format a date and time from 20220916#120000

Adding time field to the current date field using Carbon

Filter models with pivot table extra field value in laravel

Laravel Carbon format wrong date

Fetch date and convert format using laravel 5.2 eloquent query & carbon

Laravel how to query a pivot table created_at timestamp equal to a date

How to format time to "H:i:s" in laravel using carbon

laravel need to display only date not time using the carbon class

Change Date format using Carbon

Convert Carbon date to html date time format

Javascript timestamp parsed as wrong date by Carbon in Laravel

Laravel PIVOT table with extra fields

Carbon milliseconds timestamp using UNIX time

Changing Carbon object date format to be used for query

Carbon date format properly not formatting in Laravel?

Format date string in carbon in laravel5?

Laravel Timestamp Format / JavaScript Date

How to select format date using carbon?

Laravel Attach/Detach model from pivot depending on pivot extra field

Laravel : orderBy date a pivot table

getting the value of an extra pivot table column laravel

Attach extra attributes to Laravel pivot table

Removing extra pivot object in Laravel's Eloquent

How to convert date format from dd/mm/yyyy to yyyy-mm-dd using carbon on Laravel

Adjust date of Carbon object using string