首页 文章

Laravel错误必须是类型数组,给定的字符串,调用

提问于
浏览
-4

ERROR

Symfony \ Component \ Debug \ Exception \ FatalThrowableError(E_RECOVERABLE_ERROR)类型错误:传递给Illuminate \ Database \ Eloquent \ Model :: __ construct()的参数1必须是类型数组,字符串给定,在mypath \ app \ Http \中调用第65行的Controllers \ PostsController.php

Controller

public function store(Request $request)
       {


    $this->validate($request, [
        'title' =>  'required',
        'image' => 'mimes:jpeg,png,jpg,gif,svg|max:2048',
        'body'  =>  'required'
    ]);

    //dd($request); 

    auth()->user()->publish(
        Storage::putFile('images', new File($request['image']), 'public'),
        new Post(request('title', 'image', 'body'))

    );

    session()->flash('message', 'your post has now been published');

    return redirect('/');        

}

......

1 回答

  • 3

    你需要提供请求参数来创建像这样的数组格式的新帖子,

    $request->only('title', 'image', 'body')
    

    所以你的代码看起来像,

    auth()->user()->publish(
        Storage::putFile('images', new File($request['image']), 'public'),
        Post::create($request->only('title', 'image', 'body')))
    
    );
    

    我希望你能理解 .

相关问题