简体   繁体   中英

Laravel 5.5 Checking if the data is on the database

Anyone can help me. I am new in laravel development.

table name
*Masterlist
*Users

column names

Masterlist = [id,name,date_of_birth,course];
Users = [id,name,date_of_birth,course];

I am working in RegisterController.php

The flow of this is..

User registration needs to check on masterlist data's the name,date_of_birth,course

If the user registration is equal to the data of the masterlist data's the registration will success then the data will save to the user table.

Thank you for help.

Assume Your MasterList is model of table MasterList . Then you can first check your masterList that your data exists or not, if exists then do your user registration like the following-

$masterList = MasterList::where('name',$name)->where('date_of_birth',$date_of_birth)->where('course',$course)->first();
 if($masterList!=null){
     ///Your user registration process
 }

And if you are having your data from $request object then-

$masterList = MasterList::where('name',$request->input('name'))->where('date_of_birth',$request->input('date_of_birth'))->where('course',$request->input('course'))->first();
 if($masterList!=null){
     ///Your user registration process
 }

To redirect with error message you could do something like the below after the if statement-

return redirect()->back()->with([
                'error' => 'Sorry! You are not listed in MasterList!!',
            ])->withInput();

Create a validator in your controller.

$validator = Validator::make()

create a customize rule.

php artisan make:rule filePath

Send the MasterList and Users data to your rule(when create the rule instance),Finally,Compare the data in you rule ,Return true or false. I suggest you use validator and rule.it useful and convenient.

Override the register method in RegisterController as follow:

    public function register(Request $request)
{
    $this->validator($request->all())->validate();

    //query the Masterlist table
    $masterList = Masterlist::where('name', $request->input('name'))
        ->where('date_of_birth', $request->input('date_of_birth'))
        ->where('course', $request->input('course'))
        ->value('id');

    if ($masterList) {
        //masterList exist -> proceed registration process
        event(new Registered($user = $this->create($request->all())));

        $this->guard()->login($user);

        return $this->registered($request, $user)
            ?: redirect($this->redirectPath());
    }
    else {
        //masterList not exist => process the error;
    }
}

Note: register function originally defined in RegistersUsers trait which is used in RegisterController

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM