gpt4 book ai didi

php - 使用 laravel5 将数据插入数据库时​​出错

转载 作者:搜寻专家 更新时间:2023-10-31 21:30:43 24 4
gpt4 key购买 nike

我是 laravel5 框架的新手。当我当时使用 laravel5 将数据插入数据库时​​,我得到类似...的错误。

ClientFormRequest.php 第 10 行中的 FatalErrorException: 无法在类 App\Http\Requests\ClientFormRequest 中使静态方法 Symfony\Component\HttpFoundation\Request::create() 非静态

我所有的文件都在下面...

app/Http/Controller/RegisterController.php

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\ClientFormRequest;

use Illuminate\Http\Request;

class RegisterController extends Controller {

public function create()
{
return view('Client.client');
}

public function store(ClientFormRequest $request)
{


return \Redirect::route('Client.client')
->with('message', 'Record Inserted!');
}

}

app/Http/Requests/ClientFormRequest.php

<?php namespace App\Http\Requests;


use Stringy\create;

use App\User;
use Validator;
use App\Http\Requests\ClientFormRequest;

class ClientFormRequest extends Request {

/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{

}

public function validator(array $data)
{
return Validator::make($data, [
'fullname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
]);
}

public function create(array $data)
{
return client::create([
'fullname' => $data['fullname'],
'email' => $data['email'],
]);
}
}

路线

Route::get('client', 'RegisterController@create');
Route::post('contact_store', 'RegisterController@store');

最佳答案

首先,我建议您观看Laravel 5 Fundamentals反复因为它是免费的。其他系列也提供了很好的信息。

其次,我建议您至少使用 Sublime Text和一些有用的包,能够检查系统文件的深度嵌套关系(命名空间、接口(interface)、继承树等...)。如果你不会/可能不会,这位 friend 随时为你服务Laravel API

第三,据我所知,Laravel Request 构建在 Symfony 的 Request Component 之上。由于您试图将其核心功能之一重载为非静态,因此您会收到此错误。

此外,老实说,我不会将我的用户/客户端模型创建逻辑放入请求中。 Laravel 为这种误解提供了一个很好的例子。在 App\Services 文件夹中,您将找到 Laravel oem 用户模型的注册服务。

让我们检查不同情况下的问题。

但首先,基本...

假设所有逻辑都应该放在 Controller 中。

RegisterController.php

<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Request;

class RegisterController extends Controller {
public function create()
{
return view('Client.client');
}

public function store()
{
$data = Request::all(); //requested data via Facade
//prepare validatation
$validation = Validator::make($data, [
'fullname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
]);
//validate
if ($validation->fails())
{
return redirect()->back()->withErrors($v->errors());
}
// create the client
Client::create([
'fullname' => Request::input('fullname'),
'email' => Request::input('email'),
]);

return \Redirect::route('Client.client')
->with('message', 'Record Inserted!');
}

}

第二种解决方案

您可能愿意分离验证逻辑并应用一些依赖注入(inject)。

RegisterController.php

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\ClientFormRequest;

class RegisterController extends Controller {

public function create()
{
return view('Client.client');
}

public function store(ClientFormRequest $request)
{
// create the client
Client::create([
'fullname' => $request->input('fullname'),
'email' => $request->input('email'),
]);

return \Redirect::route('Client.client')
->with('message', 'Record Inserted!');
}

}

ClientFormRequest.php

use Stringy\create;

use App\User;
use Validator;
use App\Http\Requests\ClientFormRequest;
class ClientFormRequest extends Request {

public function authorize()
{
return true;
}


public function rules()
{
return [
'fullname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users'
];
}

}

第三种解决方案

您可能愿意更进一步,甚至将对象创建逻辑分离为服务以便在任何地方使用它。现在您的请求文件将保持不变。然而,

RegisterController.php

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\ClientFormRequest;
use App\Services\ClientRegistrar;

class RegisterController extends Controller {

private $registrar;

public function __construct(ClientRegistrar $registrarService)
{
$this->registrar = $registrarService;
}
public function create()
{
return view('Client.client');
}

public function store(ClientFormRequest $request)
{

$newClient = $this->registrar->create($request->all());
return \Redirect::route('Client.client')
->with('message', 'Record Inserted!')->compact('newClient');
}

}

App\Services\ClientRegistrar.php

 use App\Client;
use Validator;
use Illuminate\Contracts\Auth\Registrar as RegistrarContract;

class ClientRegistrar implements RegistrarContract {

/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
public function validator(array $data)
{
return Validator::make($data, [
'fullname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
]);
}

/**
* Create a new client instance after a valid registration.
*
* @param array $data
* @return Client
*/
public function create(array $data)
{
// create the client
return Client::create([
'fullname' => $data['fullname'],
'email' => $data['email'],
]);
}

}

我的结论没有解决问题的正确和最佳方法。坚持最适合您和您的项目规模的适用方式。

您可能还对以下内容感兴趣;

Jeffrey Way's Laravel Auto Validate on Save

关于php - 使用 laravel5 将数据插入数据库时​​出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30341119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com