下拉菜单:仅在3个上验证2个项目 [英] DropDown: Validate 2 items on 3 only

查看:71
本文介绍了下拉菜单:仅在3个上验证2个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个表,第一个是Students,具有3个字段(名称,名字,fk_diploma).然后,我的第二个表名为Diplomas,并且有1个字段名为(type_diploma).

I have 2 tables, the first is Students with 3 fields (name, firstname, fk_diploma).Then, my second table is named Diplomas and there is 1 field named (type_diploma).

有关信息,我在type_diploma字段中有3个值:

For information, I have 3 values in my field type_diploma:

1) DiplomaA
2) DiplomaB
3) DiplomaC

在我的验证系统中,我要验证DiplomaADiplomaB而不是DiplomaC,我必须有一条错误消息.

In my validate system, I want the DiplomaA or DiplomaB to be validated but not the DiplomaC, I must have an error message.

例如: *对不起,您没有文凭C的技能."

您是否知道我该怎么做?

Do you have an idea of how I can do that ?

public function store(Request $request)
    {    
      $diploma = Diploma::select('type_diploma')->where('id',$request->fk_diploma)->get();
      if($diploma->type_diploma != 'DiplomaC')  
      {
      $request->validate([
            'name' => 'required|min:3',
            'firstname' => 'required|min:2|max:200',
            'fk_diploma' => 'required'
        ]);
       }

       $exists = Student::where('name', $request->get('name'))->where('firstname', $request->get('firstname'))->where('fk_diploma', $request->get('fk_diploma'))->count();

       if (!$exists){
            Student::create($request->all());
            return redirect()->route('students.index')
                ->with('success', 'new data created successfully');
        }

        else{
            return redirect()->route('students.index')
                ->with('error', 'duplicate');

        }   

    }

我的模型文凭

class Diploma extends Model
{
    protected  $fillable = ['type_diploma'];


    public function diplomas(){

        return $this->hasMany('App\Student', 'fk_diploma');
    }


}

模型学生

class Student extends Model
{
    protected  $fillable = ['name', 'firstname', 'fk_diploma'];


    public function diplomas(){

        return $this->belongsTo('App\Diploma' , 'fk_diploma');
    }

推荐答案

这不是最好的方法,但它是我现在唯一想到的方法:

This is not the best way to do it, but its the only one i could think right now:

1)将您的请求类型更改为public function store(Request $request)

1) change the type of your request to public function store(Request $request)

2)在您的函数中执行以下操作:

2) Do this in your function:

public function store(dateRequest $request)
{    
      $diploma = Diploma::select('type_diploma')->where('id',$request->fk_diploma)->get();
      if($diploma->type_diploma != 'DiplomaA' && $diploma->type_diploma != 'DiplomaB')  
      {
      $request->validate([
            'name' => 'required|min:3',
            'firstname' => 'required|min:2|max:200',
            'fk_diploma' => 'required'
        ]);
       }

       $exists = Student::where('name', $request->get('name'))->where('firstname', $request->get('firstname'))->where('fk_diploma', $request->get('fk_diploma'))->count();

       if (!$exists){
            Student::create($request->all());
            return redirect()->route('students.index')
                ->with('success', 'new data created successfully');
        }

        else{
            return redirect()->route('students.index')
                ->with('error', 'duplicate');

        }   

}

这篇关于下拉菜单:仅在3个上验证2个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆