我在将数据插入数据库时遇到问题。假设我有一个名为HeroController的控制器,我想创建一个新的hero对象,并将其作为新英雄插入到我的数据库中。
我的控制器包含以下方法:
<?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)
{
//
}
}
我的routes.php文件:
<?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');
这是我得到的错误。类型错误:函数App \ Http \ Controllers \ UserController :: attendance()的参数太少,传递了0个且恰好期望有1个。”我是laravel的新手。
答案 0 :(得分:0)
您正在使用get请求来调用create
函数。
Route::get('/users/create', 'UserController@create');
因此,您没有将任何数据传递到该控制器。因此是错误。
答案 1 :(得分:0)
首先,您需要更改create
和validator
的可见性:
protected function create(array $data)
验证器方法:
protected function validator(array $data)
然后,您需要将/users/create
路由更改为post
,并使用store
方法:
Route::post('/users/create', 'UserController@store');
在您的UserController
中,将您的store
方法更新为:
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);
}
如果您需要某种形式的表格来注册或创建用户,请在控制器中添加其他方法:
public function register()
{
return view('user.register');
}
然后定义另一条get
路线:
Route::get('/users/register', 'UserController@register');
答案 2 :(得分:0)
Route::delete('/file/{id}','FileController@destroy')->name('deletefile');
希望对您有所帮助