Laravel: Too few arguments to function 0 passed and exactly 1 expected"
17,925
Use Request
Your create function is expecting an array. However, Laravel is passing a POST request. Therefore, use the Request class instead. Your code should read:
namespace App\Http\Controllers;
use User;
use Illuminate\Http\Request;
protected function create(Request $data)
{
$userId = Auth::id();
return User::create([
'userid'=> $data[$userId],
'shopname' => $data['shopname'],
'address' => $data['address'],
'postal' => $data['postal'],
'city' => $data['city'],
'phone' => $data['phone'],
]);
}
Author by
Demeteor
Updated on June 05, 2022Comments
-
Demeteor about 2 years
I know this has been asked before. ive seen the questions and answers but cant find something that I can get help from.
I am trying to pass simple data into my database. so far so good. I believe my problem arises when I try to add the userID since it has to be pulled from Auth. so here is my controller code.
side node , userID is from a foreign table called users. and userID will be used as a foreign key. in the userstable its called "id"
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class ShopController extends Controller { /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth'); } public function index() { return view('/shop/addShop'); } protected function validator(array $data) { return Validator::make($data, [ 'userid' => ['required', 'string', 'max:255'], 'shopname' => ['required', 'string', 'max:255'], 'address' => ['required', 'string', 'max:255'], 'postal' => ['required', 'string', 'max:255'], 'city' => ['required', 'string', 'max:255'], 'phone' => ['required', 'string', 'max:255'], ]); } /** * Add Users shop after a validation check. * * @param array $data * @return \App\ */ protected function create(array $data) { $userId = Auth::id(); return User::create([ 'userid'=> $data[$userId], 'shopname' => $data['shopname'], 'address' => $data['address'], 'postal' => $data['postal'], 'city' => $data['city'], 'phone' => $data['phone'], ]); } }
and right here you can see my route.
<?php Route::get('/', function () { return view('welcome'); }); Auth::routes(); Route::get('/home', 'HomeController@index')->name('home'); Route::post('/addShop', 'ShopController@create')->name('addShop'); Route::get('/addShop', 'ShopController@index')->name('addShop');
-
Petay87 over 5 yearsTry changing protected function create(array $data) to protected function create(Request $data)
-
Demeteor over 5 yearsDoing that throws me the following error: "Class 'App\Http\Controllers\Auth' not found" . am I not allowed to call Auth from another controller ?
-
Mozammil over 5 yearsAdd
use Auth;
to your class or replaceAuth::id()
withauth()->user()->id
. -
Demeteor over 5 yearsyeah so a new error now that its unrelated i get an error here. return user ::create([ . errror -> Class 'App\Http\Controllers\User' not found" So yes ofc it cant find User.. what should I add there?
-
Mozammil over 5 yearsAdd
App\User
to your class.
-
-
Demeteor over 5 yearsHello yes, I have done that as you requested above. my problem now is that return User. it cant find user. I know that is logical that it cant find it. but what do I declare there?
-
Petay87 over 5 yearsUpdated the answer, you just need to import the User class by adding "use User;" to the top
-
Demeteor over 5 yearsHello again. I still get this "Class 'User' not found" even though I added use User.
-
Petay87 over 5 yearsHave you actually created a User Model?
-
Demeteor over 5 yearsI Should have, changed it to app/user and I got a new error. "SQLSTATE[HY000]: General error: 1364 Field 'fname' doesn't have a default value (SQL: insert into
users
(updated_at
,created_at
) values (2019-01-10 11:21:59, 2019-01-10 11:21:59)) it tries to add data in users table. the table I want to use is not users is shop -
Petay87 over 5 yearsYou are moving on to a totally different question now. You haven't passed anything for the column fname and as you have no default value I'm guessing it's Not Null so it needs something. Meaning you are getting an error because you are not passing anything.
-
Demeteor over 5 yearsyeap, its cool ill try and figure this one out my self. cheers for solving my initial problem !