Saving user id from User table and facing error: Call to a member function save() on string












0















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.










share|improve this question























  • 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
















0















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.










share|improve this question























  • 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














0












0








0








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












1 Answer
1






active

oldest

votes


















0














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;;






share|improve this answer
























  • @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













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
});


}
});














draft saved

draft discarded


















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









0














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;;






share|improve this answer
























  • @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


















0














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;;






share|improve this answer
























  • @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
















0












0








0







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;;






share|improve this answer













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;;







share|improve this answer












share|improve this answer



share|improve this answer










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 (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





















  • @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



















@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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith