Laravel对数据库行运行更新只返回已更改的字段?

时间:2014-08-29 15:25:58

标签: php mysql database laravel

我正在构建一个活动记录器,而不是需要尽可能细化。一个例子是用户可以更新一些项目细节,

project = Project::find($id);

        //die(print_r(Input::all()));

        $project->name = Input::get('name');
        $project->description = Input::get('description');
        $project->total_cost = Input::get('cost');
        $project->start_date = Input::get('start_date');
        $project->finish_date = Input::get('end_date');
        $project->run_number_days = $this->get_days_between_two_dates(Input::get('start_date'), Input::get('end_date'));
        $project->num_days_from_year_start = $this->get_days_between_two_dates("2014-01-01", Input::get('start_date'));
        //$project->color = $this->project_rbg_to_project_hex();
        $project->organisation_id = Input::get('organisation_id');
        $project->sales_person = Input::get('sales_person');
        $project->project_manager = Input::get('project_manager');
        $project->user_id = ResourceServer::getOwnerId();
        $project->client_id = Input::get('client_id');



        $project->save();

PROJECT

    Array
    (
        [id] => 1
        [name] => Project #1
        [description] => Descriptive text.
        [total_cost] => 0.00
        [start_date] => 2014-01-01
        [finish_date] => 2014-04-30
        [run_number_days] => 120
        [num_days_from_year_start] => 1
        [color] => #92e807
        [sales_person] => 1
        [project_manager] => 1
        [client_id] => 1
        [organisation_id] => 1
        [user_id] => 1
        [created_at] => -0001-11-30 00:00:00
        [updated_at] => 2014-08-29 16:04:43
    )

POST

Array
(
    [id] => 1
    [name] => Project #1
    [description] => Descriptive text.
    [total_cost] => 0.00
    [start_date] => 2014-01-01
    [finish_date] => 2014-04-30
    [num_days_from_year_start] => 1
    [color] => #92e807
    [sales_person] => 1
    [project_manager] => 1
    [organisation] => Array
        (
            [id] => 1
            [name] => Compan
            [information] => "".
            [type] => organisation
            [notifications] => 0
            [add_all] => 0
            [created_at] => -0001-11-30 00:00:00
            [updated_at] => -0001-11-30 00:00:00
        )

    [collabs] => Array
        (
            [0] => Array
                (
                    [id] => 33
                    [email] => amani@domain.com
                    [first_name] => Amani
                    [last_name] => domain
                    [display_name] => amanidoamin
                    [initials] => AI
                    [active] => 1
                    [login_type] => normal
                    [created_at] => 2014-08-13 11:00:17
                    [updated_at] => 2014-08-13 15:01:46
                    [pivot] => Array
                        (
                            [project_id] => 1
                            [user_id] => 33
                        )

                )

            [1] => Array
                (
                    [id] => 1
                    [email] => simon@domain.com
                    [first_name] => Simon
                    [last_name] => lastname
                    [display_name] => lastname
                    [initials] => SA
                    [active] => 1
                    [login_type] => normal
                    [created_at] => -0001-11-30 00:00:00
                    [updated_at] => -0001-11-30 00:00:00
                    [pivot] => Array
                        (
                            [project_id] => 1
                            [user_id] => 1
                        )

                )

            [2] => Array
                (
                    [id] => 31
                    [email] => domain@domain.com
                    [first_name] => domain
                    [last_name] => domain
                    [display_name] => domaindomain
                    [initials] => SA
                    [active] => 1
                    [login_type] => normal
                    [created_at] => 2014-08-13 11:00:17
                    [updated_at] => 2014-08-13 15:01:46
                    [pivot] => Array
                        (
                            [project_id] => 1
                            [user_id] => 31
                        )

                )

            [3] => Array
                (
                    [id] => 32
                    [email] => richard@domain.com
                    [first_name] => Richard
                    [last_name] => latname
                    [display_name] => richardlastname
                    [initials] => RB
                    [active] => 1
                    [login_type] => normal
                    [created_at] => 2014-08-13 11:00:17
                    [updated_at] => 2014-08-13 15:01:46
                    [pivot] => Array
                        (
                            [project_id] => 1
                            [user_id] => 32
                        )

                )

            [4] => Array
                (
                    [id] => 35
                    [email] => matt@domain.com
                    [first_name] => Matt
                    [last_name] => lastname
                    [display_name] => mattlastname
                    [initials] => MF
                    [active] => 1
                    [login_type] => normal
                    [created_at] => 2014-08-13 11:00:17
                    [updated_at] => 2014-08-13 15:01:46
                    [pivot] => Array
                        (
                            [project_id] => 1
                            [user_id] => 35
                        )

                )

        )

    [client] => Array
        (
            [id] => 1
            [name] => Client
            [information] => 
            [type] => client
            [add_all] => 0
            [created_at] => -0001-11-30 00:00:00
            [updated_at] => -0001-11-30 00:00:00
        )

    [team] => Array
        (
            [0] => Array
                (
                    [id] => 3
                    [name] => Artists
                    [information] => 
                    [type] => team
                    [created_at] => -0001-11-30 00:00:00
                    [updated_at] => -0001-11-30 00:00:00
                    [pivot] => Array
                        (
                            [project_id] => 1
                            [team_id] => 3
                        )

                    [users] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 35
                                    [email] => matt@domain.com
                                    [first_name] => Matt
                                    [last_name] => lastname
                                    [display_name] => mattlastname
                                    [initials] => MF
                                    [active] => 1
                                    [login_type] => normal
                                    [created_at] => 2014-08-13 11:00:17
                                    [updated_at] => 2014-08-13 15:01:46
                                    [pivot] => Array
                                        (
                                            [team_id] => 3
                                            [user_id] => 35
                                        )

                                )

                        )

                )

            [members] => Array
                (
                    [0] => Array
                        (
                            [id] => 35
                            [email] => matt@domain.com
                            [first_name] => Matt
                            [last_name] => lastname
                            [display_name] => mattlastname
                            [initials] => MF
                            [active] => 1
                            [login_type] => normal
                            [created_at] => 2014-08-13 11:00:17
                            [updated_at] => 2014-08-13 15:01:46
                            [pivot] => Array
                                (
                                    [team_id] => 3
                                    [user_id] => 35
                                )

                        )

                )

        )

    [creator_id] => 1
    [creator] => Array
        (
            [id] => 1
            [email] => simon@domain.com
            [first_name] => Simon
            [last_name] => lastname
            [display_name] => simonlastname
            [initials] => SA
            [active] => 1
            [login_type] => normal
            [created_at] => -0001-11-30 00:00:00
            [updated_at] => -0001-11-30 00:00:00
        )

    [created_at] => Array
        (
            [date] => -0001-11-30 00:00:00
            [timezone_type] => 3
            [timezone] => UTC
        )

    [updated_at] => Array
        (
            [date] => 2014-08-29 16:04:43
            [timezone_type] => 3
            [timezone] => UTC
        )

    [collaborators] => Array
        (
            [0] => Array
                (
                    [id] => 35
                    [email] => matt@domain.com
                    [first_name] => Matt
                    [last_name] => Lastname
                    [display_name] => mattlastname
                    [initials] => MF
                    [active] => 1
                    [login_type] => normal
                    [created_at] => 2014-08-13 11:00:17
                    [updated_at] => 2014-08-13 15:01:46
                    [pivot] => Array
                        (
                            [team_id] => 3
                            [user_id] => 35
                        )

                )

            [1] => Array
                (
                    [id] => 33
                    [email] => amani@domain.com
                    [first_name] => Amani
                    [last_name] => LastName
                    [display_name] => amanilastname
                    [initials] => AI
                    [active] => 1
                    [login_type] => normal
                    [created_at] => 2014-08-13 11:00:17
                    [updated_at] => 2014-08-13 15:01:46
                    [pivot] => Array
                        (
                            [project_id] => 1
                            [user_id] => 33
                        )

                )

            [2] => Array
                (
                    [id] => 1
                    [email] => simon@domain.com
                    [first_name] => Simon
                    [last_name] => Lastname
                    [display_name] => simonlastname
                    [initials] => SA
                    [active] => 1
                    [login_type] => normal
                    [created_at] => -0001-11-30 00:00:00
                    [updated_at] => -0001-11-30 00:00:00
                    [pivot] => Array
                        (
                            [project_id] => 1
                            [user_id] => 1
                        )

                )

            [3] => Array
                (
                    [id] => 31
                    [email] => domain@domain.com
                    [first_name] => User
                    [last_name] => User9
                    [display_name] => useruser9
                    [initials] => SA
                    [active] => 1
                    [login_type] => normal
                    [created_at] => 2014-08-13 11:00:17
                    [updated_at] => 2014-08-13 15:01:46
                    [pivot] => Array
                        (
                            [project_id] => 1
                            [user_id] => 31
                        )

                )

            [4] => Array
                (
                    [id] => 32
                    [email] => richard@domain.com
                    [first_name] => User
                    [last_name] => User10
                    [display_name] => useruser
                    [initials] => RB
                    [active] => 1
                    [login_type] => normal
                    [created_at] => 2014-08-13 11:00:17
                    [updated_at] => 2014-08-13 15:01:46
                    [pivot] => Array
                        (
                            [project_id] => 1
                            [user_id] => 32
                        )

                )

        )

    [cost] => 0.00
    [organisation_id] => 1
    [client_id] => 1
    [end_date] => 2014-04-30
)

现在有时在更新中只有一个字段实际上已经更改了内容,是否可以返回哪些字段有新内容?有没有比我现在更好的更新方式?

1 个答案:

答案 0 :(得分:1)

尝试将数据库行与array_diff()

的输入进行比较
$affectedColumns = array_diff($project->toArray(), Input::all());


<小时/> 如果Input返回的内容超过了所比较的内容,请使用Input::only()

指定要比较的内容
$compareIndexes = array('id','name','description','total_cost','start_date','finish_date','run_number_days','num_days_from_year_start','color','sales_person','project_manager','client_id','organisation_id','user_id','created_at','updated_at');

$affectedColumns = array_diff($project->toArray(), Input::only($compareIndexes ));

或者,使用$project->getFillable()检索模型的可填写键。

$affectedColumns  = array_diff($project->toArray(), Input::only($project->getFillable()));