0
votes

I have a problem with inserting Data into Database. Let's say I have a controller named HeroController and I want to create a new hero object and insert it to my database as a new hero.

My controller contains the following method:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
//        return response()->json([
//            'name' => 'Abigail',
//            'state' => 'CA'
//        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6',
        ]);
    }
    public function create(array $data)
    {

        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data[password]),
        ]);
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $user = $request->isMethod('put');

        $user = new User;
        $user->name = $request->name;
        $user->email = $request->email;
        $user->password = $request->password;
        $user->save();

        return response()->json($usere, 201);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

My routes.php file:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});


Route::get('/users/create', 'UserController@create');

This is the error that i got. Type error: Too few arguments to function App\Http\Controllers\UserController::attendance(), 0 passed and exactly 1 expected". I am new to laravel.

3
where is array $data coming from ? in create function.Oukaasha Habib

3 Answers

0
votes

You are using a get request to call create function.

Route::get('/users/create', 'UserController@create');

So, you are not passing any data to that controller. Hence the error.

0
votes

First, you'll want to change the visibility of create and validator:

protected function create(array $data)

And the validator method:

protected function validator(array $data)

Then you'll want to change your /users/create route to a post and to use the store method:

Route::post('/users/create', 'UserController@store');

In your UserController update your store method to:

public function store(Request $request)
{
    // validate the request
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        return response()
        ->json(422,$validator->errors()->messages());
    }

    $user = $this->create($request->all());

    return response()->json($user, 201);
}

If you need some sort of form to register or create a user, add an additional method in your controller:

public function register()
{
    return view('user.register');
}

Then define another get route:

Route::get('/users/register', 'UserController@register');
0
votes
 Route::delete('/file/{id}','FileController@destroy')->name('deletefile');


Hope it will be helpful