Laravel 5.8:显示雄辩的项目,基于时间戳排序

use*_*991 6 php mysql laravel eloquent

我不确定如何称呼它,所以我将尽力解释。

我有一个票务系统,在其中显示所有评论。在不同部分中,我显示相关信息,例如“支持者已更改”,“票证标题已更改”,“票证状态已更改”等。

当前呈现的(无样式的)HTML:https://jsfiddle.net/2afzxhd8/

我想将这两部分合并为一个,以便在票证的注释之间显示那些相关信息。所有内容(评论和相关信息)都应根据created_at时间戳显示。

呈现(未样式化)HTML的新目标:https ://jsfiddle.net/4osL9k0n/

在我的情况下,票务系统具有以下相关的雄辩模型(和表格):

<?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');
    }
}
Run Code Online (Sandbox Code Playgroud)
<?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');
    }
}
Run Code Online (Sandbox Code Playgroud)
<?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');
    }
}
Run Code Online (Sandbox Code Playgroud)

请不要在乎CSS-它是我的样式。这只是无关紧要的。

任何想法,我需要如何更新视图以构​​建目标HTML?

use*_*991 2

我刚刚找到了这个答案,但是这个答案有一个大问题:在最坏的情况下,它会用不同的对象覆盖一些对象,这会导致集合中可能丢失对象。

来自Laravel 文档:集合

merge方法将给定的数组或集合与原始集合合并。如果给定项目中的字符串键与原始集合中的字符串键匹配,则给定项目的值将覆盖原始集合中的值。

因此,我必须将逻辑更新为:

$ticket = Tickets::where('tracking_number', '=', $request->tracking_number)->first();
$comments = $ticket->comments;
$operations = $ticket->operations;
$history_unsorted = new Collection();
$history_unsorted = $history_unsorted->merge($comments);
$history_unsorted = $history_unsorted->merge($operations);
$history = $history_unsorted->sortBy('created_at');
Run Code Online (Sandbox Code Playgroud)

这可以避免原始集合被覆盖。

有了这个,我可以简单地循环$history

@foreach($history as $history_item)
    @if ($history_item instanceof App\TicketOperations)
        <!-- Ticket Operation -->
    @else
        <!-- Ticket Comment (Text) -->
    @endif
@endforeach
Run Code Online (Sandbox Code Playgroud)