Demeteor
Demeteor

Reputation: 1223

Laravel: Too few arguments to function 0 passed and exactly 1 expected"

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

Upvotes: 3

Views: 25518

Answers (2)

Ahmed Meshaal
Ahmed Meshaal

Reputation: 119

I got this error on my project it was because this line of code Auth::guard()->logoutOtherDevices(); I just comment it and my logout function worked correctly.

Upvotes: 0

Petay87
Petay87

Reputation: 1773

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

        ]);
    }

Upvotes: 8

Related Questions