My database table has name, phone, email etc.. fields. Now I store particular field data in different variable and pass them. Here is my code. I tried it from controller function. What should I do?
$var = DB::select("SELECT * FROM reg where email = '$c_email' and Password = '$c_pass' and type = '$c_type'");
$var2 = $var->name;
$var3 = $var->phone;
return redirect('farmer')->with('key', $var2)->with('key2', $var3);
You may try the given way
return view('farmer',compact('var2', 'var3'));
Here 'farmer' is your view page
http://www.easylaravelbook.com/blog/2015/03/09/passing-multiple-variables-into-a-laravel-5-view/
When you're using redirect()->with(), you're flashing data to the session. So to get this data after redirect, use session() helper:
session('key')
https://laravel.com/docs/5.3/responses#redirecting-with-flashed-session-data
use this code. if farmer is view part. or use view part name.
$var=DB::table('reg')
->where('email', $c_email)
->where('password ', $c_pass)
->where('type', $c_pass)
->select('name','phone')
->first();
return view('farmer')->with('var', $var);
get data in view part,
$name=$var->name;
$phone=$var->phone;
Related
I have a model called CustomerInfo and i am trying to update it. update returns true but the changes are not reflecting on my db.
$customerInfo = CustomerInfo::where('machine_name',$username)->firstOrFail();
$result = $customerInfo->update($data);
$data varaible is a array having key value pair.
Also tried the following
$customerInfo = CustomerInfo::where('machine_name',$username)->update($data);
make sure $data variable that you want update record by its values, not be same with that record.
in this case, you don't have sql error but return of update will be zero.
Solved my questions.
Thanks Luciano.
i started eloquent manual db transaction and forgot to commit it at the end
DB::beginTransaction();//did this
$customerInfo = CustomerInfo::where('machine_name',$username)->firstOrFail();
$result = $customerInfo->update($data);
DB::Commit() //forgot to implement this part.
$result = $customerInfo->update($data);
$result variable will return only the boolean value. Try to return $customerInfo i.e.
return response()->json($customerInfo);
This will help you to find the actual problem and make sure the CustmerInfo model contains fillable properties and the $data variable contains all the information.
Try to use dd() like below to identify the issue:
dd($variable);
I want to fetch selected fields from my tables in laravel for that I assign my table fields in a variable. But always it gives me some database query error, here I added my code for that. This working fine when I use direct fields instead of them assign into a variable
$selectedfields = "'table1.*', 'table2.*','table3.column'"
$data = DB::table('table1')
->select($selectedfields)->get();
You can use DB:raw:
$selectedfields = 'table1.*, table2.*, table3.column';
$data = DB::table('table1')
->select(DB::raw($selectedfields))->get();
You might need to join table as well.
your query should be like this
$data = DB::table('table1')
->join('table2','table2.id','=','table1.id')
->join('table3','table3.id','=','table1.id')
->select('table1.*','table2.*','table3.*')->get();
I had an admin panel where 3 types of user are there Admin, Teacher and Student. Now I want that when student logged in he/she only see data uploaded by admin
According to his/her class and class group like class=10th and group=computer science. I have no idea how can i get this type of thing. I had used following code
$paper = Paper::where('paper_type','PaperSolution' && 'class',Auth::user()->class)->get();
this is not working properly as I am dumping data
dd($paper);
it is giving me null as answer.
you can use more granular wheres passed as array:
$query->where([
['column_1', '=', 'value_1'],
['column_2', '<>', 'value_2'],
[COLUMN, OPERATOR, VALUE],
...
])
Try this
$paper = Paper::where([['paper_type','PaperSolution'],['class', Auth::user()->class]])->get();
dd($paper);
OR
$paper = Paper::where([['paper_type','=','PaperSolution'],['class','=', Auth::user()->class]])->get();
dd($paper);
Reference for where query using array
ReferenceLink
Please refer to Laravel collection docs to correct syntax.
$paper = Paper::where('paper_type','PaperSolution')
->where('class', Auth::user()->class)
->get();
I did not exactly understand structure of your DB, but at first you need to have corresponding columns to then write any query. Your first option is uploaded by admin so in your uploads table (which is Paper model as I can see from your text) you should have something like uploader_role. Then you have student_class and student_group options. I want to get your attention in fact that this 2 options are users table columns NOT uploads (Paper). So you need to check some permissions of users, then get papers uploaded by admins. You can do that with middleware or just in your controller
$user = Auth::user();
if ($user->class == 10 && $user->group == 'computer_science') {
$papers = Paper::where('uploader_role', 'admin')
// you can add extra where options if that columns exist in papers table and you need to filter them also by class and group
->where( 'class_that_has_permissions', $user->class)
->where( 'group_that_has_permissions', $user->group)
->get();
return $papers;
}
abort(404);
Also be careful with columns that have name class that can return real user class name like App\User, try to use class_name or class_number.
I used this query to update the status column.
$val="1";
vehicles::where('id' , '=' , $veh_status)->update(['status' => $val]);
But when I submitted the status value doesn't change.
you can trace your query by using ->toSql() method !
try this to find whats happening in back
Not sure what the problem is there because you haven't given much info to work with, but you can check these suggestions:
Check if the column is set to be mass assignable in the model class, that is, it is in the fillable[] array.
make sure the id you pass to the where() function is valid.
Try using another function, save() which will achieve the same results you seek, like this;
// filter the vehicle
$vehicle = vehicles::where('id', '=', $veh_id)->first();
or
$vehicle = vehicles::find($veh_id);
$vehicle->status = 1;
$vehicle->save();
Lastly, I noticed your id variable you pass to the where the () function is called $veh_status "presumably - vehicle status" and not $veh_id, "presumably - vehicle id" so probably check that out.
Ref: Laravel Model Update documentation
I have this basic query i want to perform but an error keeps coming up. Probably due to my newness to laravel.
here is the code:
$userRecord = $this->where('email', $email)->where('password', $password);
echo "first name: " . $userRecord->email;
I am trying to get the user record matching the credentials where email AND password are a match. This is throwing an error:
Undefined property: Illuminate\Database\Eloquent\Builder::$email
I've checked the email and password being passed to the function, and they are holding values. what is the problem here?
Thanks,
$this->where('email', $email)->where('password', $password)
is returning a Builder object which you could use to append more where filters etc.
To get the result you need:
$userRecord = $this->where('email', $email)->where('password', $password)->first();
You either need to use first() or get() to fetch the results :
$userRecord = $this->where('email', $email)->where('password', $password)->first();
You most likely need to use first() as you want only one result returned.
If the record isn't found null will be returned. If you are building this query from inside an Eloquent class you could use self .
for example :
$userRecord = self::where('email', $email)->where('password', $password)->first();
More info here
$userRecord = Model::where([['email','=',$email],['password','=', $password]])->first();
or
$userRecord = self::where([['email','=',$email],['password','=', $password]])->first();
I`
think this condition is better then 2 where. Its
where condition array in array of where conditions;
The error is coming from $userRecord->email. You need to use the ->get() or ->first() methods when calling from the database otherwise you're only getting the Eloquent\Builder object rather than an Eloquent\Collection
The ->first() method is pretty self-explanatory, it will return the first row found. ->get() returns all the rows found
$userRecord = Model::where('email', '=', $email)->where('password', '=', $password)->get();
echo "First name: " . $userRecord->email;
After reading your previous comments, it's clear that you misunderstood the Hash::make function. Hash::make uses bcrypt hashing. By design, this means that every time you run Hash::make('password'), the result will be different (due to random salting). That's why you can't verify the password by simply checking the hashed password against the hashed input.
The proper way to validate a hash is by using:
Hash::check($passwordToCheck, $hashedPassword);
So, for example, your login function would be implemented like this:
public static function login($email, $password) {
$user = User::whereEmail($email)->first();
if ( !$user ) return null; //check if user exists
if ( Hash::check($password, $user->password) ) {
return $user;
} else return null;
}
And then you'd call it like this:
$user = User::login('email#aol.com', 'password');
if ( !$user ) echo "Invalid credentials.";
else echo "First name: $user->firstName";
I recommend reviewing the Laravel security documentation, as functions already exist in Laravel to perform this type of authorization.
Furthermore, if your custom-made hashing algorithm generates the same hash every time for a given input, it's a security risk. A good one-way hashing algorithm should use random salting.
Here is shortest way of doing it.
$userRecord = Model::where(['email'=>$email, 'password'=>$password])->first();
$userRecord = $this->where('email', $email)->where('password', $password);
in the above code , you are just requesting for Eloquent object , not requesting for the data,
$userRecord = $this->where('email', $email)->where('password', $password)->first();
so that, you can get the first data, from the given credentials by default ordering DESC with PK, in case of multiple data with the same credentials. but you have to handle the exception, in case of no matching data.
you have one more option to achieve the same.
$userRecord = $this->where('email', $email)->where('password', $password)->firstOrfail();
in the above snippets, in case of no data, it will automatically throw a 404 error.
also, you can have alternative snippets
$filter['email']=$email;
$filter['password']=$password;
$userRecord = $this->where($filter)->first();
That's it
After rigorous testing, I found out that the source of my problem is Hash::make('password'). Apparently this kept generating a different hash each time. SO I replaced this with my own hashing function (wrote previously in codeigniter) and viola! things worked well.
Thanks again for helping out :) Really appreciate it!