Codeigniter ion auth register insert user id into multiple tables - codeigniter

first sorry if its a stupid question but im lost a bit.
So when i use ion auth register function, and the register is ok, inserts the user data in the users table, and inserts the users id in to the groups table too.
Im a bit lost at this part, i would like to insert the user id in to the user profile table, and user settings table, after the registration is complete.
And i dont really know which function to modify. I was looking at the model and library searching for how the group is added into the database but couldnt really find any hint in the code.
So could someone please help me out on this? Pointing to a tutorial maybe or just giving me a hint where to start the modify
Thank you

In models/ion_auth_model.php at line 733 starts the register function.
You can extend somewhere on line 793, after the line that gets the id of the user $id = $this->db->insert_id(); just add your custom data you wish to insert:
$mdata = array(
'user_id' => $id,
// ... your other data here
);
$this->db->insert('my_table', $mdata);

Related

Don't show the message to all users? ( laravel )

I need to create to send a message to all new users registered on my website. I created a table called messages that admins can store (insert) the messages from the admin panel to this table and these messages are simply shown to all users with a foreach.
I don't know if this the best way to do something like that!
Anyway, the problem is that when any new user register and open his dashboard he just found the old messages
This is the table :
image
And this is the simple code for foreach
$msgForAll = Message::latest()->get();
I'm not sure how to display new messages to the users.
Again the way I made this idea is wrong, I know that ):
You can use eloquent's where spefically the whereDate queries in your case to get certain rows past or before date.
As an example relating to what you want to do, it can be along the lines of this:
// Given that you are using Auth to get the user's data...
// Get messages that were created after the user's creation date
$messages = Message::whereDate('created_at','>',Auth::User()->created_at)->get();

How to save a form into different tables

I am new with laravel
I used the default Authentication and I added one other input that I wanted it to be saved on an other table how can I do that
If you're talking about how to save registration form input into different table (not to users) when user is registering, add something like this to create() method in app\Http\Auth\RegisterController.php:
Profile::create(['custom_column' => $data->custom_form_field]);
I think in this situation will be better to use connections in model. If this data is about user of course. Also there is important thing about submit method(simple submit or AJAX).

Insert a row that contains relational data?

This is a simplified version but... a user can submit the form which contains their name and qualification.
I have two tables - users and qualifications. A user has many qualifications. A qualification has one user.
I save like so:
$user = new User;
$user->first_name = 'Derek';
$qualification = new Qualification;
$qualification->qualification = 'History Degree';
DB::transaction(function() use ($user, $qualification) {
$user->save();
User::find($user->id)->qualifications()->save($qualification);
});
My questions are:
Is this the correct way to insert a row to a table and it's relationship? Is there a better way?
And my 2nd question...this line:
User::find($user->id)->qualifications()->save($qualification);
This may be a bit crazy but I just would like this explaining to me, the $user->id gets the id of the previously inserted row, if two people were to insert at the same time, how would I be sure that it would get the correct id? As in, user A saves, then user B saves, then the first User::find bit runs for user A but this would find user B's id. Is this where the transaction comes in?
Before you save the user by doing this:
user->save();
I think you should first save the "qualification" to that user. As long as your code functions as you currently have it, I would think this would do the trick:
$user->qualifications()->save($qualification);
and then do
user->save();
If you can get it to work in that order, you'll never have to worry about obtaining the wrong user ID and switching the qualifications on the wrong user, if they were to enter their information at approximately the same time.

Storing remember_token to a separate table than users?

is it possible to set another table & column to store my remember tokens? I know that the framework tries to automatically find a remember_token column in my "users" model, but I want to store it separately from users. Is there a way to configure my default tokens table? Thank you
P.S - I'm using laravel 5
First, you need to create separate model for storing remember tokens and define a relationship on your User model like so
public function rememberToken() {
return $this->hasOne('RememberToken');
}
Then you need to override methods on your User model, originally defined in Authenticatable trait. Override getRememberToken() and setRememberToken() methods. You will also need to override getRememberTokenName() as it is used in where clause in EloquentUserProvider::retrieveByToken() see EloquentUserProvider line 60. In order for this to work properly you probably have to add global scope to your User model to join remember_tokens table on every query, and return 'remember_tokens.token' from getRememberTokenName() method.
Think twice as it seems more trouble than it is worth. Why would you want to store your tokens separately anyway?
I believe the way Laravel works uses a seperate column in the users table to store a single remember_me token. From my understanding it seems that logging out resets this token regardless of storing the token in a cookie (correct me if I'm wrong).
https://github.com/laravel/ideas/issues/971
If you log in with remember_me checked on your personal computer,
then again on your phone,
and maybe again with any other device,
then finally the act of signing out on any device using the remember_me token or not will reset this token in the DB.
If Laravel had a separate table, able to remember each device, it might solve the problem.

Laravel 4 - how to use a unique validation rule / unique columns with soft deletes?

Assume, you are trying to create a new user, with a User model ( using soft deletes ) having a unique rule for it's email address, but there exists a trashed user within the database.
When trying to validate the new user's data, you will get a validation error, because of the existing email.
I made some kind of extra validation within my Controllers, but wouldn't it be nice to have it all within the Model?
Would you suggest creating a custom validation rule?
As I haven't found a clean solution now, I am interessted in how others solved this problem.
You can validate passing extra conditions:
'unique:users,deleted_at,NULL'
This sounds like an issue with your business logic rather than a technical problem.
The purpose of a soft delete is to allow for the possibility that the soft-deleted record may be restored in the future. However, if your application needs uniqueness of email (which is completely normal), you wouldn't want to both create a new user with that email address and be able to restore the old one as this would contravene the uniqueness requirement.
So if there is a soft deleted record with the email address for which you are adding as a new record, you should consider instead restoring the original record and applying the new information to it as an update, rather than trying to circumvent the uniqueness check to create a new record.
This is the best approach
'email' => 'required|email|unique:users,email,NULL,id,deleted_at,NULL',
It give you this query
select count(*) as aggregate from `users` where `email` = ? and `deleted_at` is null
Laravel offers "Additional Where Clauses".
My url validation rule (from the update model method) looks like this:
$rules['url'] = 'required|unique:pages,url,'.$page->id.',id,deleted_at,NULL';
This means that the url must be unique, must ignore the current page and ignore the pages where deleted_at id not NULL.
Hope this helps.
Your Eloquent model should have the $softDeletes property set. If so, then when you perform a WHERE check, like User::where('username', 'jimbob'), Eloquent will automatically add in the query WHERE deleted_at IS NULL... which excludes soft deleted items.
In laravel 6.2+ you can use
'email' => ['required', Rule::unique('users')->whereNull('deleted_at')]

Resources