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'],

        ]);
    }
Share:
17,925
Demeteor
Author by

Demeteor

Updated on June 05, 2022

Comments

  • Demeteor
    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
      Petay87 over 5 years
      Try changing protected function create(array $data) to protected function create(Request $data)
    • Demeteor
      Demeteor over 5 years
      Doing that throws me the following error: "Class 'App\Http\Controllers\Auth' not found" . am I not allowed to call Auth from another controller ?
    • Mozammil
      Mozammil over 5 years
      Add use Auth; to your class or replace Auth::id() with auth()->user()->id.
    • Demeteor
      Demeteor over 5 years
      yeah 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
      Mozammil over 5 years
      Add App\User to your class.
  • Demeteor
    Demeteor over 5 years
    Hello 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
    Petay87 over 5 years
    Updated the answer, you just need to import the User class by adding "use User;" to the top
  • Demeteor
    Demeteor over 5 years
    Hello again. I still get this "Class 'User' not found" even though I added use User.
  • Petay87
    Petay87 over 5 years
    Have you actually created a User Model?
  • Demeteor
    Demeteor over 5 years
    I 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
    Petay87 over 5 years
    You 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
    Demeteor over 5 years
    yeap, its cool ill try and figure this one out my self. cheers for solving my initial problem !