validation from database

Published 4 months ago by devamit2018

i have a table of students and extract the student by their id.i want to validate if the id value exists in database or not? any idea...

Best Answer (As Selected By devamit2018)
burlresearch

Just worth a note, that you could do this query in Eloquent a little more directly:

$studentExists = Student::where('name', $request->name)
    ->where('age', $request->age)
    ->exists();
burlresearch
'student_id' => 'exists:students'
dleroari

In general, if data does not exist in table, it will return either true or null. For instance if you run:


// Find if id = 1 exists $user = classname::find(1)->exists();

This will return true if data exists, or null if not.

The other way would be, which I believe looks cleaner is:


// Check if id = 1 exists $user = $u->where('id', '=',1)->get();

// Return boolean true/false $user->isEmpty();

You can also use this in a if-statement like:


// Check if id = 1 exists $user = $u->where('id', '=',1)->get();

// Return boolean true/false $user->isEmpty();

if($user->isEmpty()){ // Do something }

There are multiple ways you can check if data exists or not. I would recommend taking a look at the laravel documentation on Eloquent https://laravel.com/docs/5.6/eloquent#other-creation-methods.

I hope this helps.

devamit2018

Call to a member function isEmpty() on string .this is the error i get after using if conditions

devamit2018

this is my code

    $Name=$request->get('name');
   
    $Age=$request->get('age');

  $students=DB::table('students')
   ->select('id','name','age','email','phone','created_at')
   ->where('name','=',$Name)
   ->orWhere('age','=',$Age)

   ->get();
  
   
   if($Name->isEmpty()||$Age->isEmpty())
   {
       return "doesnot exist";
   }
   else
   {
    return view('viewreport',compact('students'));
   }

  
  

 }
biishmar
$rule = ["name" =>'required', "age" => 'required'];

$validator = Validation::make(request()->all(), $rule);

if($validator->fails()) {
    return "Name or Age is empty";
}

$name=request()->name; // Dont use first letter as capital for variables
   
$age=request()->age;


$students=DB::table('students')
   ->where('name',$name)
   ->where('age',$age)
   ->get();

if($students->isEmpty()) {
    return "Doesnt exists";
}

return view('viewreport',compact('students'));

import Validation without fail....

devamit2018

it worked but here if any condition is true it returns the value..for example if name is true and age is false then it extracts the value on the basis of name.......

devamit2018

this is the final result that checks the conditions

$values=[
        'name'=>'exists:students,name',
        'age'=>'exists:students,age'];

    $validator = Validator::make(request()->all(), $values);
    
    if($validator->fails()) {
        return "Name and Age doesnt match";
    }

   $Name=$request->get('name');
   
   $Age=$request->get('age');

  $students=DB::table('students')
   ->select('id','name','age','email','phone','created_at')
   ->where('name','=',$Name)
   ->Where('age','=',$Age)

   ->get();

   if($students->isEmpty()) {
    return "Doesnt exists";
}

return view('viewreport',compact('students'));
burlresearch

Whoa, I'm confused about what you want here.

Your question was:

I want to validate if the [student] id value exists in database or not?

So I was expecting that you were just looking for the syntax for:

public function student(Request $request)
{
    $validator = Validator::make(request()->all(), [
        'id' => 'exists:students'
    ]);
    if ($validator->passes()) // exists;
    if ($validator->fails()) // no student;

But now, your last post, you're checking to see if the the students {name + age} already exist in the database - which means that your validation passes if they're found? But, this is a problem, since it's quite possible to have 2 students, of the same age, with the same name at a school. In fact, I actually had classmates like this - so I know it's possible.

This also has nothing to do with your question - so I'm confused. What are you trying to do? (I think you're off target...)

devamit2018

name and age are the table fields..for the verification both name and age should be matched...name and age are just the examples i took may be we can add id field also.mine aim was to verify by using email and password...thanks for everyone..

burlresearch

Just worth a note, that you could do this query in Eloquent a little more directly:

$studentExists = Student::where('name', $request->name)
    ->where('age', $request->age)
    ->exists();
wilk_randall

I would do what @burlresearch is suggesting, except, like he also mentioned, what if two students have the same name and age? Why wouldn't you just use their id or have some other kind of unique identifier for each student?

devamit2018

i am registering a form with name,email and password.how to verify name and email has already been taken or already stored in database.

this is my code in controller

$name=$request->get('name'); $photo=$request->get('photo'); $email=$request->get('email'); $password=$request->get('password'); $date=Carbon::now(); $id='3';

    $data=array('name'=>$name,'photo'=>$photo,'email'=>$email,
        'password'=>bcrypt($password),'created_at'=>$date,'id_cms_privileges'=>$id);
    DB::table('cms_users')->insert($data);
 

return redirect()->route('getLogin');

this registers need to check

devamit2018

how to go with name and email check?

Please sign in or create an account to participate in this conversation.