Laravel 5:组合表

时间:2015-11-21 14:32:26

标签: mysql laravel laravel-5 eloquent blade

截至目前,我正在开发一个Laravel-5项目,我想建立一些活动日志。我确实有一个结合多个表的查询,并提供了我需要的数据,但是一旦我尝试将数据放在屏幕上,就会出现问题。

这是我现在使用的查询ProfileController:

$activity = DB::select("SELECT user_id, created_at, source, project_id
    FROM (
        SELECT id as project_id, user_id, created_at, 'project' as source FROM projects
        UNION ALL SELECT project_id, user_id, created_at, 'comment' as source FROM comments
        UNION ALL SELECT project_id, user_id, created_at, 'favorite' as source FROM favorites
        UNION ALL SELECT project_id, user_id, created_at, 'like' as source FROM likes
    ) as a
    WHERE user_id = $id
    ORDER BY created_at DESC");

这是我的profile.blade.php中的代码:

<ul class="list-group">
@foreach($activity as $act)
    @if($act->source == 'project')
        <li class="list-group-item">{{ $act->user_id }} added a new project: {{ $act->project_id }}</li>
    @elseif($act->source == 'like')
        <li class="list-group-item">{{ $act->user_id }} likes {{ $act->project_id }}!</li>
    @elseif($act->source == 'comment')
        <li class="list-group-item">{{ $act->user_id }} commented on {{ $act->project_id }}</li>
    @elseif($act->source == 'favorite')
        <li class="list-group-item">{{ $act->user_id }} has {{ $act->project_id }} to his favourites!</li>
    @endif
@endforeach
</ul>

这就是我的模型:

class Project extends Model
{
    protected $fillable = [

        'title',
        'description',
        'user_id',
        'snapshot',
        'views'
    ];

    public function creator() {
         return $this->belongsTo('App\User', 'user_id', 'id');
    }

    public function tags() {
         return $this->belongsToMany('App\Models\Tag');
    }
}
class Likes extends Model
{
    protected $fillable = [
        'user_id',
        'project_id'
    ];

    public function like() {
         return $this->belongsTo('App\User', 'user_id', 'id');
    }

    public function project() {
         return $this->belongsTo('App\Models\Project', 'project_id', 'id');
    }
}
class Comment extends Model
{
    protected $fillable = [
        'user_id',
        'project_id',
        'comment'
    ];

    public function poster() {
         return $this->belongsTo('App\User', 'user_id', 'id');
    }

    public function project() {
         return $this->belongsTo('App\Models\Project', 'project_id', 'id');
    }
}
class Favorite extends Model
{
    protected $fillable = [
        'user_id',
        'project_id'
    ];

    public function favorite() {
         return $this->belongsTo('App\User', 'user_id', 'id');
    }

    public function project() {
         return $this->belongsTo('App\Models\Project', 'project_id', 'id');
    }
}

现在我知道我只是要求在我的Blade页面上给我user_id或project_id,我这样做是因为类似于$ act-&gt; creator-&gt; name或$ act-&gt; project-&gt;标题会给我一个错误:

  

未定义的属性:stdClass :: $ creator

  

未定义的属性:stdClass :: $ project

我确实认为这可能与我构建查询的方式有关,并且这种方式混合表可能会使事情变得更加困难,但我似乎无法找到方法在Eloquent中构建此查询,或者找到一种方法来访问属于这些user_id和project_id的数据,而无需使用我在模型中创建的公共函数。

1 个答案:

答案 0 :(得分:0)

使用当前设置,您无法从Laravel模型中受益,因为您需要循环浏览不同的模型。我的建议是构建一个名为Activity的新表,其中包含3列:id,type,user_id。并使用多态关系。

此外,您可以从事件/侦听器中受益,以便在Activity表中创建记录。您可以从排队侦听器中受益,因此如果这不会影响您的客户体验,则不会在线创建活动记录。