将id传递给其他控制器

时间:2015-11-26 07:49:17

标签: laravel-5 laravel-5.1

$job->created_by = $input['created_by'];

我想在此数组中传递用户ID,这是在我应该怎么做?

该字段是外键。

当我运行它时会抛出异常

SQLSTATE[23000]: Integrity constraint violation: 1452
     Cannot add or update a child row: a foreign key constraint fails 
     (`freight`.`jobs`, CONSTRAINT `approved_by` FOREIGN KEY (`created_by`)
     REFERENCES `users` (`id`))

     (SQL: insert into `jobs`
           (`company_id`, `origin`, `commodity`, `destination`,
            `created_by`, `approved_by`, `date`, `carrier`, `consolidator`,
            `overseas_agt`, `prepaid_fob`, `free_time`, `wt_pcs`, 
            `updated_at`, `created_at`)
           values (2, , , , asdsadasdsad, asdsadasdsad, , , , , , , ,
                   2015-11-26 07:32:02, 2015-11-26 07:32:02)
     )

2 个答案:

答案 0 :(得分:0)

您已在数据库迁移中将字段初始化为unsigned。 为:

language: ruby
services:
- mongodb
- redis-server
rvm:
- 2.1.1
before_script:
- cp config/secrets.example.yml config/secrets.yml
- cd frontend && npm install -g bower && npm install && bower install
script:
- RAILS_ENV=test bundle exec rake db:create
- bundle exec rake rspec-rerun:spec
before_install:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
after_success:
- openssl aes-256-cbc -k $DEPLOY_KEY -in /home/travis/build/my-build_location/config/deploy_id_rsa_enc_travis -d -a -out /home/travis/build/my-build_location/config/deploy_id_rsa
- chmod 600 /home/travis/build/my-build_location/config/deploy_id_rsa
- cd /home/travis/build/my-build_location
- "[[ $TRAVIS_BRANCH = 'development' ]] && travis_wait bundle exec cap staging deploy"
env:
  global:
    secure: secret_key

此处user_id是另一个表的主键。 更改表并再次运行迁移。 它肯定会工作。

答案 1 :(得分:0)

我发现我做错了我必须给出当前活动用户的id,正确的方法如下

$job->created_by = \Auth::user()->id;