Laravel 4 human readable timestamps with turntables

Is there a way to show created_at and updated_at timestamps in humanoid format if used in a pivot table?

Im new to the whole laravel pivot table, but right now I am using a pivot table to store user comments on individual posts, and each of them has timestamps for creating and updating_on_module.

If I just used a regular table with a model, I would expand the base model, which looks like this:

use Carbon\Carbon;

class Base extends Eloquent {

protected function getHumanTimestampAttribute($column)
{
    if ($this->attributes[$column])
    {
        return Carbon::parse($this->attributes[$column])->diffForHumans();
    }

    return null;
}

public function getHumanCreatedAtAttribute()
{
    return $this->getHumanTimestampAttribute("created_at");
}

public function getHumanUpdatedAtAttribute()
{
    return $this->getHumanTimestampAttribute("updated_at");
} 

}

But how I did not use the model for pivot tables, how could I get around this? Is there a way to get these functions to work with my pivot tables? Is using a model for a pivot table the easiest / correct way to do this?

, .

/**
 * Guide comments
 *
 * @return mixed
 */

public function guide_comments()
{
    return $this->belongsToMany('Guide', 'guides_comment')->withTimestamps();
}  
+4
3

, 2 : User Category "--", (category_user: id, user_id, category_id, created_at, updated_at) 2.

:

// User model
public function categories()
{
   return $this->belongsToMany('Category')->withTimestamps();
}

// Category model
public function users()
{
   return $this->belongsToMany('User')->withTimestamps();
}

, Carbon , , , :

$user = User::with('categories');

$user->categories->first()->pivot->created_at->diffForHumans();

PivotModel ( ).


, :

// same for both models
public function getPivotHumansCreatedAtAttribute()
{
    if (is_null($this->pivot)) return null;

    return $this->pivot->created_at->diffForHumans();
}

// then you access it like this:
Category::first()->pivotHumansCreatedAt; // null, since it not called as relation
User::first()->categories()->first()->pivotHumansCreatedAt; // '2 days ago'
+7

Laravel.

http://laravel.com/docs/eloquent#working-with-pivot-tables

Laravel Pivot. , "Base", Eloquent. Eloquent Eloquent. , Pivot:

 public function newPivot(Model $parent, array $attributes, $table, $exists)
 {
     return new YourCustomPivot($parent, $attributes, $table, $exists);
 }

newPivot `` , Illuminate\Database\Eloquent\Model. Eloquent Illuminate\Database\Eloquent\Model, , .

class YourCustomPivot extends Illuminate\Database\Eloquent\Relations\Pivot {

protected function getHumanTimestampAttribute($column)
{
    if ($this->attributes[$column])
    {
        return Carbon::parse($this->attributes[$column])->diffForHumans();
    }

    return null;
}

public function getHumanCreatedAtAttribute()
{
    return $this->getHumanTimestampAttribute("created_at");
}

public function getHumanUpdatedAtAttribute()
{
    return $this->getHumanTimestampAttribute("updated_at");
} 

}

.

class Base extends Eloquent {
     public function newPivot(Model $parent, array $attributes, $table, $exists)
     {
         return new YourCustomPivot($parent, $attributes, $table, $exists);
     }

}
+2

PHP:

$date = '2014-04-30 19:49:36';  //date returned from DB

echo date("H:i:s",strtotime(str_replace('/','-',$date)));     // 19:49:36
echo date("Y/m/d",strtotime(str_replace('/','-',$date)));     // 2014/04/30

: http://www.php.net/manual/en/datetime.formats.date.php

+1

Source: https://habr.com/ru/post/1540303/


All Articles