php


Laravel function associate() on null


<?php
namespace App\Services\v1;
use Validator;
use App\User;
use App\UserInformation;
class UserService {
protected $rules = [
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'type' => 'required|user_type',
'package_size' => 'required|integer',
'package_expiration_date' => 'required|date_format:d/m/Y|after:today',
'title' => 'alpha_spaces|max:50',
'name' => 'required|alpha_spaces|max:50',
'surname' => 'required|alpha_spaces|max:50',
'gender' => 'required|boolean'
];
public function validate($user) {
$validator = Validator::make($user, $this->rules);
$validator->validate();
}
public function registerUser($request) {
$user = new User();
$user->email = $request->input('email');
$user->password = bcrypt($request->input('password'));
$user->type = $request->input('type');
$user->package_size = $request->input('package_size');
$user->package_expiration_date = date('Y-m-d', strtotime(str_replace('/', '-', $request->input('package_expiration_date'))));
$user->save();
$user_information = new UserInformation();
$user_information->title = $request->input('title');
$user_information->name = $request->input('name');
$user_information->surname = $request->input('surname');
$user_information->gender = $request->input('gender');
$user_information->users()->associate($user);
$user_information->save();
return $user;
}
}
It insert the user but user_information does not work. They have one-to-one relation. Here how i did :
User_Information Model :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserInformation extends Model {
public function users() {
$this->belongsTo('App\User', 'id');
}
}
User Model :
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable {
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function userInformation() {
$this->hasOne('App\UserInformation', 'user_id');
}
}
If i remove the associate() function, it works well. But user_information has user_id. So i have to add user_id.

Related Links

Combine records from two different arrays in php
Why Apache is slow on LAN when serving for Android client?
Send push notifications using FCM XMPP protocol within the PHP application
PHP while loop curl IFisset error
php code not redirecting to the user role [duplicate]
Php wont input data into database
how to compute the dynamic price of items in cart and output the total
Descending order in yajra not working properly for second page onwards
Fatal error: Cannot redeclare <method>
Where is the best place to check role permissions?
Stripe Webhook JSON_DECODE not working (500)
getting image with if condition in laravel 5.4
Laravel 5 remember me not working
Need a way to post a url to facebook than will display html in the preview and then redirect
How can I join a posts table and comments table so that I can sort my results by the posts with the highest number of comments?
convert amazon mws data into csv format in php

Categories

HOME
php
nam
ionic2
memory
yaml
focus
mirc
dependencies
openflow
mosquitto
implicit-conversion
capistrano
onvif
azure-logic-apps
azure-graph-api
locationmanager
parsley.js
nsstring
grocery-crud
sql-injection
max-msp-jitter
xbox360
c#-7.0
gravity
cloudsim
azure-machine-learning
fido-u2f
exchange-server-2010
high-availability
unrar
openbravo
history.js
mdns
splunk-query
monitor
premake
wampsharp
microsoft-certifications
devtools
directfb
spring-rabbitmq
jslint
djcelery
identify
cartopy
hidden-markov-models
python-sounddevice
get-event-store
absolute
sonicmq
winmerge
nslayoutconstraint
emacs25
gawk
ternary-operator
streamwriter
e
livecycle
nvda
qpixmap
yii2-model
noclassdeffounderror
as3-api
management-studio-express
teensy
html.actionlink
opencyc
unhandled
dynamics-ax-2012-r2
embeddedwebserver
algebraixlib
fuseesb
camus
dos2unix
dc
iplimage
tabris
xcode6.3.1
commenting
refit
windows-taskbar
principalcontext
cloo
unrealscript
sql-view
insertonsubmit
stty
libxml-js
tnt4j
insertion
proc-open
red-system
gprof
onconfigurationchanged
hardcode
dropshadow
motodev-studio
filelock
django-pagination
reliability
dm
fxruby
chronic

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile