Saving user id from User table and facing error: Call to a member function save() on string
I tried to store the userid from user table in another car table as a foreign key.
User Table:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('userid')->unique();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Car table:
public function up()
{
Schema::create('cars', function (Blueprint $table) {
$table->string('plateno')->primary()->unique();
$table->string('brand');
$table->string('model');
$table->string('user_id')->nullable();
$table->foreign('user_id')->references('userid')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
}
User Model and Car Model have 1 to 1 relationship.
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'userid', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function cars()
{
return $this->hasOne('AppCar', 'plateno' );
}
}
and here is the the store function for CarController
public function store(Request $request)
{
$this->validate($request, [
'plateno' => 'required',
'brand' => 'required',
'model' => 'required'
]);
$cars= new AppCar;
$cars->plateno=$request->get('plateno');
$cars->brand=$request->get('brand');
$cars->model=$request->get('model');
$cars=Auth::user()->userid;
$cars->save();
return redirect('car')->with('success', 'User car has been added.');
}
thank you.
laravel eloquent
add a comment |
I tried to store the userid from user table in another car table as a foreign key.
User Table:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('userid')->unique();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Car table:
public function up()
{
Schema::create('cars', function (Blueprint $table) {
$table->string('plateno')->primary()->unique();
$table->string('brand');
$table->string('model');
$table->string('user_id')->nullable();
$table->foreign('user_id')->references('userid')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
}
User Model and Car Model have 1 to 1 relationship.
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'userid', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function cars()
{
return $this->hasOne('AppCar', 'plateno' );
}
}
and here is the the store function for CarController
public function store(Request $request)
{
$this->validate($request, [
'plateno' => 'required',
'brand' => 'required',
'model' => 'required'
]);
$cars= new AppCar;
$cars->plateno=$request->get('plateno');
$cars->brand=$request->get('brand');
$cars->model=$request->get('model');
$cars=Auth::user()->userid;
$cars->save();
return redirect('car')->with('success', 'User car has been added.');
}
thank you.
laravel eloquent
What error do you get?
– ako
Nov 20 '18 at 13:41
SymfonyComponentDebugExceptionFatalThrowableError thrown with message "Call to a member function save() on string" Stacktrace: #0 SymfonyComponentDebugExceptionFatalThrowableError in C:UnitenParkingappHttpControllersCarController.php:59
– Wan Ahmad Imran Izzat Wan Zain
Nov 20 '18 at 13:43
add a comment |
I tried to store the userid from user table in another car table as a foreign key.
User Table:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('userid')->unique();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Car table:
public function up()
{
Schema::create('cars', function (Blueprint $table) {
$table->string('plateno')->primary()->unique();
$table->string('brand');
$table->string('model');
$table->string('user_id')->nullable();
$table->foreign('user_id')->references('userid')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
}
User Model and Car Model have 1 to 1 relationship.
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'userid', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function cars()
{
return $this->hasOne('AppCar', 'plateno' );
}
}
and here is the the store function for CarController
public function store(Request $request)
{
$this->validate($request, [
'plateno' => 'required',
'brand' => 'required',
'model' => 'required'
]);
$cars= new AppCar;
$cars->plateno=$request->get('plateno');
$cars->brand=$request->get('brand');
$cars->model=$request->get('model');
$cars=Auth::user()->userid;
$cars->save();
return redirect('car')->with('success', 'User car has been added.');
}
thank you.
laravel eloquent
I tried to store the userid from user table in another car table as a foreign key.
User Table:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('userid')->unique();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
Car table:
public function up()
{
Schema::create('cars', function (Blueprint $table) {
$table->string('plateno')->primary()->unique();
$table->string('brand');
$table->string('model');
$table->string('user_id')->nullable();
$table->foreign('user_id')->references('userid')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
}
User Model and Car Model have 1 to 1 relationship.
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'userid', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function cars()
{
return $this->hasOne('AppCar', 'plateno' );
}
}
and here is the the store function for CarController
public function store(Request $request)
{
$this->validate($request, [
'plateno' => 'required',
'brand' => 'required',
'model' => 'required'
]);
$cars= new AppCar;
$cars->plateno=$request->get('plateno');
$cars->brand=$request->get('brand');
$cars->model=$request->get('model');
$cars=Auth::user()->userid;
$cars->save();
return redirect('car')->with('success', 'User car has been added.');
}
thank you.
laravel eloquent
laravel eloquent
asked Nov 20 '18 at 13:35


Wan Ahmad Imran Izzat Wan ZainWan Ahmad Imran Izzat Wan Zain
1
1
What error do you get?
– ako
Nov 20 '18 at 13:41
SymfonyComponentDebugExceptionFatalThrowableError thrown with message "Call to a member function save() on string" Stacktrace: #0 SymfonyComponentDebugExceptionFatalThrowableError in C:UnitenParkingappHttpControllersCarController.php:59
– Wan Ahmad Imran Izzat Wan Zain
Nov 20 '18 at 13:43
add a comment |
What error do you get?
– ako
Nov 20 '18 at 13:41
SymfonyComponentDebugExceptionFatalThrowableError thrown with message "Call to a member function save() on string" Stacktrace: #0 SymfonyComponentDebugExceptionFatalThrowableError in C:UnitenParkingappHttpControllersCarController.php:59
– Wan Ahmad Imran Izzat Wan Zain
Nov 20 '18 at 13:43
What error do you get?
– ako
Nov 20 '18 at 13:41
What error do you get?
– ako
Nov 20 '18 at 13:41
SymfonyComponentDebugExceptionFatalThrowableError thrown with message "Call to a member function save() on string" Stacktrace: #0 SymfonyComponentDebugExceptionFatalThrowableError in C:UnitenParkingappHttpControllersCarController.php:59
– Wan Ahmad Imran Izzat Wan Zain
Nov 20 '18 at 13:43
SymfonyComponentDebugExceptionFatalThrowableError thrown with message "Call to a member function save() on string" Stacktrace: #0 SymfonyComponentDebugExceptionFatalThrowableError in C:UnitenParkingappHttpControllersCarController.php:59
– Wan Ahmad Imran Izzat Wan Zain
Nov 20 '18 at 13:43
add a comment |
1 Answer
1
active
oldest
votes
Here you have an error:
$cars= new AppCar;
$cars->plateno=$request->get('plateno');
$cars->brand=$request->get('brand');
$cars->model=$request->get('model');
$cars=Auth::user()->userid; // Here must be $cars->user_id = Auth::user()->userid;
$cars->save();
Replace $cars=Auth::user()->userid;
with $cars->user_id = Auth::user()->userId;
;
@WanAhmadImranIzzatWanZain When you specify your primary key and foreign key in migrations (table creation) it is not reasonable to refer your foreign key to something else later (hereid
). See here
– ako
Nov 20 '18 at 14:01
Hereuser_id
incars
table refersuserid
inusers
table, If you want to referuser_id
incars
toid
inusers
then you must change the foreign key definition in table creation (migration here) and absolutely your code must be changed like$cars->user_id = Auth::id();
.
– ako
Nov 20 '18 at 14:04
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394232%2fsaving-user-id-from-user-table-and-facing-error-call-to-a-member-function-save%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here you have an error:
$cars= new AppCar;
$cars->plateno=$request->get('plateno');
$cars->brand=$request->get('brand');
$cars->model=$request->get('model');
$cars=Auth::user()->userid; // Here must be $cars->user_id = Auth::user()->userid;
$cars->save();
Replace $cars=Auth::user()->userid;
with $cars->user_id = Auth::user()->userId;
;
@WanAhmadImranIzzatWanZain When you specify your primary key and foreign key in migrations (table creation) it is not reasonable to refer your foreign key to something else later (hereid
). See here
– ako
Nov 20 '18 at 14:01
Hereuser_id
incars
table refersuserid
inusers
table, If you want to referuser_id
incars
toid
inusers
then you must change the foreign key definition in table creation (migration here) and absolutely your code must be changed like$cars->user_id = Auth::id();
.
– ako
Nov 20 '18 at 14:04
add a comment |
Here you have an error:
$cars= new AppCar;
$cars->plateno=$request->get('plateno');
$cars->brand=$request->get('brand');
$cars->model=$request->get('model');
$cars=Auth::user()->userid; // Here must be $cars->user_id = Auth::user()->userid;
$cars->save();
Replace $cars=Auth::user()->userid;
with $cars->user_id = Auth::user()->userId;
;
@WanAhmadImranIzzatWanZain When you specify your primary key and foreign key in migrations (table creation) it is not reasonable to refer your foreign key to something else later (hereid
). See here
– ako
Nov 20 '18 at 14:01
Hereuser_id
incars
table refersuserid
inusers
table, If you want to referuser_id
incars
toid
inusers
then you must change the foreign key definition in table creation (migration here) and absolutely your code must be changed like$cars->user_id = Auth::id();
.
– ako
Nov 20 '18 at 14:04
add a comment |
Here you have an error:
$cars= new AppCar;
$cars->plateno=$request->get('plateno');
$cars->brand=$request->get('brand');
$cars->model=$request->get('model');
$cars=Auth::user()->userid; // Here must be $cars->user_id = Auth::user()->userid;
$cars->save();
Replace $cars=Auth::user()->userid;
with $cars->user_id = Auth::user()->userId;
;
Here you have an error:
$cars= new AppCar;
$cars->plateno=$request->get('plateno');
$cars->brand=$request->get('brand');
$cars->model=$request->get('model');
$cars=Auth::user()->userid; // Here must be $cars->user_id = Auth::user()->userid;
$cars->save();
Replace $cars=Auth::user()->userid;
with $cars->user_id = Auth::user()->userId;
;
answered Nov 20 '18 at 13:45


akoako
7711221
7711221
@WanAhmadImranIzzatWanZain When you specify your primary key and foreign key in migrations (table creation) it is not reasonable to refer your foreign key to something else later (hereid
). See here
– ako
Nov 20 '18 at 14:01
Hereuser_id
incars
table refersuserid
inusers
table, If you want to referuser_id
incars
toid
inusers
then you must change the foreign key definition in table creation (migration here) and absolutely your code must be changed like$cars->user_id = Auth::id();
.
– ako
Nov 20 '18 at 14:04
add a comment |
@WanAhmadImranIzzatWanZain When you specify your primary key and foreign key in migrations (table creation) it is not reasonable to refer your foreign key to something else later (hereid
). See here
– ako
Nov 20 '18 at 14:01
Hereuser_id
incars
table refersuserid
inusers
table, If you want to referuser_id
incars
toid
inusers
then you must change the foreign key definition in table creation (migration here) and absolutely your code must be changed like$cars->user_id = Auth::id();
.
– ako
Nov 20 '18 at 14:04
@WanAhmadImranIzzatWanZain When you specify your primary key and foreign key in migrations (table creation) it is not reasonable to refer your foreign key to something else later (here
id
). See here– ako
Nov 20 '18 at 14:01
@WanAhmadImranIzzatWanZain When you specify your primary key and foreign key in migrations (table creation) it is not reasonable to refer your foreign key to something else later (here
id
). See here– ako
Nov 20 '18 at 14:01
Here
user_id
in cars
table refers userid
in users
table, If you want to refer user_id
in cars
to id
in users
then you must change the foreign key definition in table creation (migration here) and absolutely your code must be changed like $cars->user_id = Auth::id();
.– ako
Nov 20 '18 at 14:04
Here
user_id
in cars
table refers userid
in users
table, If you want to refer user_id
in cars
to id
in users
then you must change the foreign key definition in table creation (migration here) and absolutely your code must be changed like $cars->user_id = Auth::id();
.– ako
Nov 20 '18 at 14:04
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394232%2fsaving-user-id-from-user-table-and-facing-error-call-to-a-member-function-save%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What error do you get?
– ako
Nov 20 '18 at 13:41
SymfonyComponentDebugExceptionFatalThrowableError thrown with message "Call to a member function save() on string" Stacktrace: #0 SymfonyComponentDebugExceptionFatalThrowableError in C:UnitenParkingappHttpControllersCarController.php:59
– Wan Ahmad Imran Izzat Wan Zain
Nov 20 '18 at 13:43