doufu7835 2017-03-11 15:57
浏览 103
已采纳

在laravel 5中从url params为模型添加值

Playing around with a referral system in laravel 5.4. I have been able to create a unique shareable link for each user.

When another user clicks on that, I want the portion of the url with the referral id of the link's original owner to be added to the referrer field of the user.

I tried this method and been getting this error, what better way is there to do this.

 use Illuminate\Support\Facades\Input;

 protected function create(array $data)
     {

    $ref = Input::get('ref');

    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'referrer' => $ref

    ]);

    return $user;

}

I am getting an error complaining that the referrer column cannot be null even though there is a ref on the link. Sample link.

http://localhost:8000/register?ref=1b0a6294-043d-11e7-86bf-56847afe9799

User Model

protected $fillable = [
    'name', 'email', 'password', 'level', 'btc_address', 'referrer'
];` 
  • 写回答

1条回答 默认 最新

  • dongxing4196 2017-03-11 17:11
    关注

    I think the problem might be your routes and url you use in form for registering user.

    The url you showed is probably for displaying registration form, but when you send the form, you send it to http://localhost:8000/register url so you don't have ref defined in your url.

    You should make sure, that you send form also to http://localhost:8000/register?ref=1b0a6294-043d-11e7-86bf-56847afe9799 url or put hidden field with ref value from get action.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀