Sunday, 1 September 2019

Laravel 5.8: Display eloquent items sorted based on timestamp

I'm not sure, how this is called, so I'll explain it as good as possible.

I've a ticket system, where I display all comments in one section. In a different section, I display related information like "Supporter changed", "Ticket title changed", "Status of ticket changed" and so on.

Current rendered (unstyled) HTML: https://jsfiddle.net/2afzxhd8/

I would like to merge these two sections into one, that those related information are displayed between the comments of the ticket. Everything (comments + related information) should be displayed sorted based on the created_at timestamp.

New target rendered (unstyled) HTML: https://jsfiddle.net/4osL9k0n/

The ticket system has in my case these relevant eloquent models (and tables):

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Tickets extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'tracking_number', 'customer_id', 'category_id',
        'priority_id', 'subject', 'status_id', 'is_done',
        'supporter_id'
    ];

    protected $hidden = [
    ];

    protected $dates = ['deleted_at'];

    public function status() {
        return $this->belongsTo(TicketStatuses::class, 'status_id');
    }

    public function priority() {
        return $this->belongsTo(TicketPriorities::class, 'priority_id');
    }

    public function category() {
        return $this->belongsTo(TicketCategories::class, 'category_id');
    }

    public function supporter() {
        return $this->belongsTo(User::class, 'supporter_id');
    }

    public function operations() {
        return $this->hasMany(TicketOperations::class, 'ticket_id');
    }

    public function comments() {
        return $this->hasMany(TicketComments::class, 'ticket_id');
    }
}

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class TicketComments extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'ticket_id', 'text', 'user_id', 'is_html',
        'email_reply', 'internal_only'
    ];

    protected $hidden = [
    ];

    protected $dates = ['deleted_at'];

    public function ticket() {
        return $this->belongsTo(Tickets::class, 'id', 'ticket_id');
    }

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

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class TicketOperations extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'ticket_id', 'user_id', 'ticket_activity_id',
        'old_value', 'new_value'
    ];

    protected $hidden = [
    ];

    protected $dates = ['deleted_at'];

    public function ticket() {
        return $this->belongsTo(Tickets::class, 'ticket_id');
    }

    public function activity() {
        return $this->belongsTo(TicketActivities::class, 'ticket_activity_id');
    }

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

Please don't care about the CSS - it is styled in my case. It's just not relevant here.

Any idea, how I need to update my view to be able to build my target HTML?



from Laravel 5.8: Display eloquent items sorted based on timestamp

No comments:

Post a Comment