函数App \ Http \ Controllers \ Auth \ AdaugaJobController :: create()的参数太少,已传递0个且恰好期望1个

时间:2019-05-15 13:39:24

标签: php laravel eloquent

所以,我想在一个表中插入数据,但出现此错误 我真的不t know why. i不熟悉Laravel,请帮助:D

这是我的路线

Route::get('/adaugajob', 'Auth\AdaugaJobController@create')->name('adaugajob');

这是我的控制器

protected function validator(array $data)
    {

        if ($data) {
            return Validator::make($data, [
                'titlu' => ['required', 'string', 'max:255'],
                'descriere' => ['required', 'string', 'max:255'],
                'salariu_estimativ' => ['required', 'string', 'max:255'],
                'oras' => ['required', 'regex:^[0-9\-\+]{9,15}$^'],
            ]);

        }
    }

    protected function create(array $data)
    {
        if ($data) {
            Joburi::create([
                'titlu' => $data['titlu'],
                'descriere' => $data['descriere'],
                'salariu_estimativ' => $data['salariu_estimativ'],
                'oras' => $data['oras'],
            ]);
        }
    }

3 个答案:

答案 0 :(得分:1)

方法Create需要一个数组。但是laravel只能发送路径参数(如果存在)或等效的模型绑定.....,如果函数要求,则总是发送对象Request

array $data参数更改为

protected function create( \Illuminate\Http\Request $request)
{
    $data = $request->all();
    if ($data) {
        Joburi::create([
            'titlu' => $data['titlu'],
            'descriere' => $data['descriere'],
            'salariu_estimativ' => $data['salariu_estimativ'],
            'oras' => $data['oras'],
        ]);
    }
}

保存版本:

protected function create( \Illuminate\Http\Request $request)
{
    $data = $request->all();
    if ($data) {
        $joburi = new Joburi();
        $joburi->titlu = $data['titlu'];
        $joburi->descriere = $data['descriere'];
        $joburi->salariu_estimativ = $data['salariu_estimativ'];
        $joburi->oras = $data['oras'];
        $joburi->save();
        return $joburi;
    }
}

答案 1 :(得分:0)

尝试一下。并对所有插入操作使用存储方法。

use Illuminate\Http\Request;

public function store(Request $request)
{
    $validatedData = $request->validate([
        'titlu' => 'required|string|max:255',
        'descriere' => 'required|string|max:255',
        'salariu_estimativ' => 'required|string|max:255',
        'oras'=> 'required|regex:^[0-9\-\+]{9,15}$^',

    ]);

    Joburi::create([
        'titlu' => $data['titlu'],
        'descriere' => $data['descriere'],
        'salariu_estimativ' => $data['salariu_estimativ'],
        'oras' => $data['oras'],
    ]);
}

答案 2 :(得分:0)

在刀片文件中:

{{ Form::open(array('route' => 'adaugajob', 'method' => 'POST')) }}

     {{-- Your input fields here --}}

     {{ Form::submit('Save', array('class' => 'btn btn-primary')) }}

{{ Form::close() }}

在controller文件夹中创建一个控制器 您商店功能中的代码

public function store(Request $request){

   $validator = Validator::make($request->all(), [
        'titlu' => 'required|string|max:255',
        'descriere' => 'required|string|max:255',
        'salariu_estimativ' => 'required|string|max:255',
        'oras'=> 'required|regex:^[0-9\-\+]{9,15}$^',
   ]);

   if ($validator->fails()) {
        return redirect()->back()
               ->withErrors($validator)
               ->withInput();
   }

    $post = $request->all();

    Joburi::create($post);

    return redirect('/adaugajob');
}

在路由文件(web.php)中:

Route::post('/adaugajob', 'AdaugaJobController@store')->name('adaugajob');