Eloquent - 获取最新日期并将结果插入另一个表格

时间:2015-03-09 14:25:00

标签: laravel laravel-4 eloquent

我们有2张桌子

标题

+------+--------------------------------------+--------------+---------------------+
| id   | title                                | subs_updated | created_at          |
+------+--------------------------------------+--------------+---------------------+
|  104 | movie 1                              |              | 2014-11-11 12:08:28 |
|  129 | movie 2                              |              | 2014-11-11 12:08:29 |
+------+--------------------------------------+--------------+---------------------+

替补

+----+----------+----------+---------+---------------------+
| id |  label   | title_id | subs    | created_at          |
+----+----------+----------+---------+---------------------+
| 13 | English  |      104 | English | 2014-11-12 05:05:39 |
| 15 | Italian  |      104 | Italian | 2014-11-12 05:25:00 |
| 16 | Dutch    |      104 | Dutch   | 2014-11-13 05:40:51 |
| 18 | Arabic   |      129 | Arabic  | 2014-11-12 06:05:28 |
| 19 | Arabic   |      129 | Arabic  | 2014-11-12 06:07:23 |
+----+----------+----------+---------+---------------------+

在SubsController中我有:

public function attach()
{
    $input = Input::except('_token');

    if ( ! Input::get('label') || ! Input::get('subs')) {
        return Response::json(trans('subs::main.fillAllRequiredFields'), 400);
    }

    if ( ! isset($input['title_id']))
    {
        return Response::json(trans('dash.somethingWrong'), 500);
    }

    else
    {
        $this->model->fill($input)->save();
    }

    return Response::json($this->model, 201);
}


子模型

<?php

class Link extends Entity {

    public $table = 'subs';

    protected $guarded = array('id');

    public function title()
    {
        return $this->belongsTo('Title');
    }
}
  1. 我想在子表中为标题(title_id)找到最新的创建日期(created_at)。
  2. 将结果(日期)插入titles表=&gt; title(id)=&gt; subs_updated
  3. 将该查询与上述attach()函数一起使用

  4. 任何帮助都非常感谢

3 个答案:

答案 0 :(得分:0)

DB::statement(
   'update titles set subs_updated = 
       (select max(created_at) from subs where title_id = ?)
    where id = ?', 
    [$titleId, $titleId]
);

或假设Title hasMany Sub关系,更多 Eloquent 方式:

$subsUpdated = $title->subs()->max('created_at');
$title->update(['subs_updated' => $subsUpdated]);

答案 1 :(得分:0)

假设您遵循Laravel的命名约定并设置了适当的模型:

$latestSub = Sub::select('created_at')->where('title_id', 104)->orderBy('created_at', 'desc')->first();
Title::where('id', 104)->update(array(
    'subs_updated'  => $latestSub->created_at
));

如果您没有设置模型,只需使用相同的代码,但使用查询构建器而不是Eloquent

$latestSub = DB::table('subs')->select('created_at')->where('title_id', 104)->orderBy('created_at', 'desc')->first();
DB::table('titles')->where('id', 104)->update(array(
    'subs_updated'  => $latestSub->created_at
)); 

答案 2 :(得分:0)

如果我正确分析了您的问题,您希望每次创建新子时都更新subs_updated。这可以使用model events

很好地完成

Sub模型中,覆盖boot()以注册事件:

public static function boot(){
    parent::boot();

    self::created(function($sub){
        $title = $sub->title;
        if( ! is_null($title)){
            $title->subs_updated = $sub->created_at;
            $title->save();
        }
    });
}

如果您想在每次更新或创建子版本时更新subs_updated,可以使用saved事件:

self::saved(function($sub){ ...