Mike
Mike

Reputation: 61

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.

Upvotes: 0

Views: 1800

Answers (3)

Sohel0415
Sohel0415

Reputation: 9853

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();

Upvotes: 1

Usama
Usama

Reputation: 944

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

Upvotes: 1

杨苏杭
杨苏杭

Reputation: 83

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.

Upvotes: 0

Related Questions