Auth:attempt fails on duplicate email - laravel-5.4

Im using Laravel then I've encountered a problem when logging in. I have a duplicate email in my database but I'm tagging them as different users by a column class_id. So how do I use class_id in logging in? When im using the email that has no duplicates it works.

Try this..
Specifying Additional Conditions
If you wish, you may also add extra conditions to the authentication query in addition to the user's e-mail and password.
Auth::attempt(['email' => $email, 'password' => $password, 'class_id' => 'user's class id']);
Laravel expects email as unique field. You may use an additional field like username.
Laravel Doc
Hope it helps. If any error occurred let me know..

Related

Laravel | Unique validation where clause

I am trying to validate the input of a email address that exists but only when the company_id is the same as the company_id which is passed in with the request.
I am getting this error...
SQLSTATE[42S22]: Column not found: 1054 Unknown column '1' in 'where clause' (SQL: select count(*) as aggregate from company_users where email_address = myemail.com and 1 <> company_id)
I have read online and the way to do it is to associate the table and the column inside of the validation which is what I am doing.
This is my current code...
required|email|unique:company_users,email_address,company_id,' . $request->company_id
Here is a rough idea with this you can achieve what you
You can use Rule class to customize the validation rule for you.
'email' => ['required', 'string', 'email', 'max:191',Rule::unique('users')->where(function ($query) use ($request) {
return $query->where('company_id', $request->company_id);
})],
Hope this helps
This should answer your question.
'required|email|unique:company_users,email_address,NULL,id,company_id,' . $request->company_id
This means, the email must be unique while ignoring a row with an id of NULL and where its company_id is the same as $request->company_id.
The except parameter ignores rows to compare against the validation. E.g., an email address of abc#gmail.com has already been taken by user 1 but you did this:
'unique:company_users,email_address,1'
It will ignore user with id of 1. So inputting the same email will still pass since the existing user with the same email has been ignored.
Which is not the same in your case, because you just dont want to ignore a special row, but you want to ignore a row based on a condition. So that is how this answer helps you, by adding more field validations after the ignore parameter.
You might want to take a look at this: laravel 4: validation unique (database) multiple where clauses
It has to be like:
required|email|unique:company_users,email_address,' . $request->company_id
The format is:
unique:table,column,'id to exclude'
Or if you want to validate based on a column you can do a separate query:
$exists = Model::where('company_id','!=',$request->company_id)->where('email',$request->email)->count()
if($exists) {
// return an error or a validation error
}
Make $request->company_id a string by adding ' single quotes around your variable. Otherwise MySQL will think is a column in your table.
required|email|unique:company_users,email_address,company_id,"'" . (int) $request->company_id . "'"

MySQL SELECT: is it possible to use part of the result in the SELECT?

(Massively edited because the responses were focusing on the wrong thing, which means I didn't formulate the question properly.)
I have a MySQL table with id, username and password fields. The password is encrypted. Part of the encryption uses the id of the record, specifically (in PHP):
md5 (md5('id') . $formPasswordCleaned)
To a validate a login form, I do the following (pseudo-code):
query database for the email address from login form
if num_rows > 0, $row = fetch_array
use $row['id'] to encrypt the password provided in the login form.
compare encrypted password with the one found in $row 4.
But, what I'd really like to do is one single SELECT command, that looks for a match on both email address and the encrypted password. But, since the password was encrypted using the row's id.....
Question: is it possible to write a SELECT statement that uses part of the forthcoming result as some of the arguments? Something like: SELECT * from users where email=$email and password=md5(md5($futureResult['id]).$password)
I realise it's self-referential, but I know just the basics of database usage and so I don't know what to search for (google or here) to find out if it's possible or how to do it, if it is indeed possible.

Using Concat within a Rails Scope

I have a Contact model. It has the attributes: first_name and last_name. The user enters text in a search field with the prompt: Enter the contact's full name. Rails then needs to find all contact records LIKE the name entered.
Here is a picture of the contact records:
-If the user types in "JOE" then rails will return two records (because it is case insensitive)
-If the user types in "joe s" then rails will return two records
-If the user types in "doe" then rails will return one record.
#models/contact.rb
class Contact < ActiveRecord::Base
scope :by_entered_name, -> (full_name){where("CONCAT('first_name',' ','last_name') LIKE ?", full_name)}
end
Generated sql when I run Contact.by_entered_name("joe") in the rails console:
SELECT "contacts".* FROM "contacts" WHERE (CONCAT('first_name',' ','last_name') LIKE 'joe'
I am using mysql in case that detail is important. For this example app however, I am using sqlite, and it is throwing a syntax error. Ultimately what is most important is that I get this to work on mysql.
Update: It was expressed that my question was not clear. My question is:
How do I properly create a query which takes text entered by a user, and finds all contacts whose concatenated first_name and last_name are LIKE that submitted text by the user? I also need it to be case insensitive. My attempted scope above does not appear to work.
There are some quotes in there you don't need. And you need the wildcard % in the parameter. Also, ILIKE is needed to disregard the case with Postgres.
class Contact < ActiveRecord::Base
scope :by_entered_name, -> (full_name){where("CONCAT(first_name,' ',last_name) ILIKE ?", "%#{full_name}%")}
end
SQLite doesn't use the CONCAT function, it uses || as a concatenation operator. Swards' answer will get you going in MySQL.

Can I force Yii to use particular alias in generated SQL

I'm retrieving my records using CActiveRecord with a with() statement:
Probes::model()->with(array
(
'user',
'results',
'results.answer',
'survey',
'survey.questions'
))->findByPk($id)
I wanted to use GROUP BY on question_id field of survey.questions relation, so I changed above to:
'survey.questions'=>array('group'=>'survey.questions.question_id'),
This caused a SQL exception 1054 Unknown column. However, by analyzing attached SQL code:
`questions`.`question_id` AS `t6_c2`
I managed to find out, that I have to use t6_c2 alias (auto-generated by Yii?). So, another change to:
'survey.questions'=>array('group'=>'t6_c2'),
and the problem is solved.
But, then again, alias t6_c2 seems quite... "unstable" (auto-generated?) for me. Can I force Yii to in this part of generated SQL some other alias, provided by me? Or how certain can I be, that this part of SQL code won't change upon next (some later) generation? Or -- is there any other way to achieve, what I want to achieve?
you can assign alias to your relation
,
'survey.questions'=>array(
'alias' => 'surq'
'group'=>'surq.question_id',
),
read this and this for more info
You can set specific and unique alias for each relation table in a relations method in your model. For example,"user"=>array(self::HAS_MANY, "User", "user_id", "alias"=>"your_alias_for_this_relation")
Try this (i'm asumming 'survey.questions' is a field of the table probe)
Prove::model()->findByPk($id)->survey.questions, CHtml::listData(Prove::model()->findAll(array("order"=>"survey.questions")),

Thinking Sphinx "no field found in schema" error

I am pretty new to Sphinx.
Trying to find user with name "bob" and company_id "14".
Controller:
#users = User.search 'bob', :conditions => { :company_id => '14'}
Model:
define_index do
indexes :name
indexes :company_id
end
Error:
index user_core: query error: no field 'company_id' found in schema
I have the 'company_id' in table & I re-indexed everything several times.
When I am just searching for the 'name' everything works properly.
Just as another helpful hint: turns out I had to change the way I called Model.search(), since my field was listed as an attribute (i.e. using has), I needed to call the search method with :with instead of :conditions (for fields).
Attributes should be declared as:
has company_id
So, in you case:
Model:
define_index do
indexes :name
has :company_id
end
And one more helpful hint, if you happen to be an idiot like me:
If you are getting this error and you are correctly using attributes and fields, it may be that you forgot to restart your dev server after adding a new field to the index.