【问题标题】:Eloquent - Get latest date and insert the result to another tableEloquent - 获取最新日期并将结果插入另一个表
【发布时间】:2015-03-10 16:08:21
【问题描述】:

我们有两张桌子

标题

+------+--------------------------------------+--------------+---------------------+
| 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. 我想为 subs 表中的标题 (title_id) 按创建日期 (created_at) 查找最新的 sub。
  2. 将该结果(日期)插入到标题表 => 标题 (id) => subs_updated
  3. 将该查询与上述 attach() 函数一起使用


非常感谢任何帮助

【问题讨论】:

  • 能否将您的SubTitle 模型的代码添加到问题中?
  • 嗨 Lucas,我​​已经发布了潜艇模型。标题模型太大,无法发布。

标签: laravel laravel-4 eloquent


【解决方案1】:

如果我正确分析了您的问题,您希望在每次创建新子时更新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){ ...

【讨论】:

  • 我爱你!!效果很棒。再次感谢您的宝贵时间:)
【解决方案2】:
DB::statement(
   'update titles set subs_updated = 
       (select max(created_at) from subs where title_id = ?)
    where id = ?', 
    [$titleId, $titleId]
);

或者,假设Title hasMany Sub关系,更多雄辩方式:

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

【讨论】:

  • 有什么想法可以用上面的 attach() 函数来实现吗?谢谢
【解决方案3】:

假设您遵循 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
)); 

【讨论】:

  • 嗨,Dave,你能告诉我如何使用上面的 attach() 函数来实现这个查询吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
  • 2016-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-13
相关资源
最近更新 更多