douying0108 2019-06-14 07:22
浏览 82

如何验证每个用户是否存在一个只包含一次的值?

I am creating a coffee shop website because I want to learn Laravel and I like coffee. I have a dashboard where the user can manage his coffee. He can manually select from all kind of coffee. With my approach, I detect 2 Major Problems which is connected to the question.

1. Problem

Let's say the User has added 2 coffees with the id of 1 and 2

id  |  user_id  |  selected_coffee  |  created_at  |  updated_at
----------------------------------------------------------------
1   |  1        |  1                |  xxxxxxxxxx  |  xxxxxxxxxx
1   |  1        |  2                |  xxxxxxxxxx  |  xxxxxxxxxx

If the User selects in these orders because he can select multiple. * Let's say he select the coffee id 1, 2 and the last one 3.*

Because the code detects that the first id is in the database, it is ignoring the other values.

2. Problem

Let's say the User1 has added 2 coffees with the id of 1 and 2

id  |  user_id  |  selected_coffee  |  created_at  |  updated_at
----------------------------------------------------------------
1   |  1        |  1                |  xxxxxxxxxx  |  xxxxxxxxxx
1   |  1        |  2                |  xxxxxxxxxx  |  xxxxxxxxxx

The User2 can select the coffee but it is not written in the Database which I need for display visually on the frontend

I know it is not well seen to add 2 Problems but my thought is if we can fix the number 1. The problem then the 2.Problem will disappear.

Here is my code which makes it unique:

public function store(Request $request)
{
  app('App\Http\Controllers\ScoreController')->store($request);

  $message = ['selected_coffee.unique' => 'Check Coffee ID already exist'];
  $this->validate($request,[
    'selected_coffee' => 'unique:manage_coffees',
  ],$message);


  if($request->input('checkedDrink') != null){

    foreach ($request->input('checkedDrink') as $selected_id) {

      $manage_coffee = new ManageCoffee();

      $manage_coffee->user_id = \Auth::user()->id;

      $manage_coffee->selected_coffee = (int)$selected_id;

      $manage_coffee->save();

    }
  }

  return redirect('dashboard/coffee');
}

I found the validator in StackOverflow but it was not well explained. I think I must do a where clause in the ->validate but I don't know where I should put it.

Hopefully, you guys can help me.

Torsten

  • 写回答

1条回答 默认 最新

  • dongxing1960 2019-06-14 07:47
    关注

    If I understood you correct you need exist rule but with additional condition like in example from official docs.

    Something like that:

    use Illuminate\Validation\Rule;
    
    $this->validate($request,[
       'selected_coffee' => [
           Rule::exists('manage_coffees')->where(function ($query) {
               $query->where('user_id', Auth::user()->id);
               //you can specify any conditions you like
           }),
       ],
    ],$message);
    
    评论

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂