When you register as a user add the field and data from another table?

There is a user Administrator to do the registration of the user and the device (so when you create the user device displays to the user which is logged)
June 27th 19 at 15:35
1 answer
June 27th 19 at 15:37
In migrations and add a field, for example, user_agent in the Users table. And user-agent it is possible to obtain JS-coy using navigator.userAgent and pass in the query or via PHP $_SERVER['HTTP_USER_AGENT'].
according to the data from another table - not easier joint just? Otherwise, the duplication is obtained - Abe_Wuckert commented on June 27th 19 at 15:40
: Yes, easier, thanks , just such a problem when registering a user , from the Administrator and it in the table assigns the device id of the administrator - thomas_Fay commented on June 27th 19 at 15:43
: so now the two tables, do I understand you?
users : id,name,bla-bla-bla
and
user_agent : id,name, user_id?
And user_id is assigned the user id admin and the new user, right? - Abe_Wuckert commented on June 27th 19 at 15:46
: There are Two tables users and device
And user_id is assigned the user id admin and the new user, right? Yes, exactly - thomas_Fay commented on June 27th 19 at 15:49
: then read about the method insertGetId() . It allows you to get the last id that was added to the database.
Ie:
1)the first query add the data to users.
2) Get its id using insertGetId()
3) the second query adds the data in the device and user_id = result insertGetId(). - Abe_Wuckert commented on June 27th 19 at 15:52
: Good thank you - thomas_Fay commented on June 27th 19 at 15:55
What is not realized I had completely read the documentation....Can you describe how it should look like? - thomas_Fay commented on June 27th 19 at 15:58
: here described: itsolutionstuff.com/post/how-to-get-last-inserted-...
You have 1 request:
$id = DB::table('users')->insertGetId(['name'=>$request->name]);

Second query:
DB:table('device')->insert([
 ['user_agent' => $request->user_agent, 'user_id' => $id]
]);


Something like that. - Abe_Wuckert commented on June 27th 19 at 16:01
: Thank You very much - thomas_Fay commented on June 27th 19 at 16:04
Only question is what controller to perform - thomas_Fay commented on June 27th 19 at 16:07
: https://paste.laravel.io/jEGpw - thomas_Fay commented on June 27th 19 at 16:10
: Yes, simply create in same controller method, which will put everything in the database =) - Abe_Wuckert commented on June 27th 19 at 16:13
: Created and got the error
SQLSTATE[HY000]: General error: 1364 Field 'email' doesn't have a default value (SQL: insert into `users` (`name`) values ()) - thomas_Fay commented on June 27th 19 at 16:16
:
Controller registration
public function register()
{
if(Sentinel::check()){
 $companys = Company::where('user_id', Sentinel::getUser()->id)->get();
 $dv = Device::where('user_id', Sentinel::getUser()->id)->get();
 $hom = House::where('user_id', Sentinel::getUser()->id)->get();
 $ass = Access::where('user_id', Sentinel::getUser()->id)->get();
 $asc = apartment::where('user_id', Sentinel::getUser()->id)->get();
}else{
 $hom = House::get();
 $companys = Company::get();
 $dv = Device::get();
 $ass = Access::get();
 $asc = apartment::get();
}
 return view('auth.reg',compact('companys','hom','dv','ass','asc'));
}
 public function postRegister(Request $request)
{

 $token = str_random(32);
 $users = [
 'name' => request('name'),
 'email' => request('email'),
 'password' => request('password'),
 'user_token' => ($token),
];


 $user = Sentinel::registerandActivate($users);
 $rolechosen = ($request->input('role'));

 //Assigning roles

 $admin_role = Sentinel::findRoleBySlug('admin');
 $manager_role = Sentinel::findRoleBySlug('manager');
 $user_role = Sentinel::findRoleBySlug('user');

 if ($rolechosen == 'admin') {
$admin_role->users()->attach($user);
 } elseif ($rolechosen == 'manager') {
$manager_role->users()->attach($user);
 } elseif($rolechosen == 'user') {
$user_role->users()->attach($user);
}

 return redirect('/profile');

}

 public function devus(Request $request){
 $id = DB::table('users')->insertGetId(['name'=>$request->name]);
DB:table('device')->insert([
 ['device' => $request->device, 'user_id' => $id]
]);
print_r($id);
 }
- thomas_Fay commented on June 27th 19 at 16:19
:
$id = DB::table('users')->insertGetId(['name'=>$request->name, 'email'= $request->email]);

He's writing You that email field has no default value)) You are trying only to save the name in database, and we need all the data from the form)) - Abe_Wuckert commented on June 27th 19 at 16:22
That is
$id = DB::table('users')->insertGetId($users-> $request->name]);

Just like a hundred times have read the documentation I can not understand..(( - thomas_Fay commented on June 27th 19 at 16:25
: But if I created another table user_device (user_id, device_id) , because user can have many devices... I ordered the models, but this table does not saves.. - thomas_Fay commented on June 27th 19 at 16:28
: it means that something is incorrectly spelled. Read the documentation on relations many-to-many. Or using QueryBuilder to do the insert - Abe_Wuckert commented on June 27th 19 at 16:31
: Yes, everything is finally sorted out)))
The only thing I have empty fields in the table to view
$us= User::find($id);
$device ID's = Device::where("id", "=", $us->id)->get();

And error
Type error: Too few arguments to function App\Http\Controllers\DeviceController::list_index(), 0 passed and exactly 1 expected - thomas_Fay commented on June 27th 19 at 16:34

Find more questions by tags Laravel