簡體   English   中英

Laravel 錯誤必須是數組類型,字符串給定,調用

[英]Laravel error must be of the type array, string given, called in

錯誤

Symfony \\ Component \\ Debug \\ Exception \\ FatalThrowableError (E_RECOVERABLE_ERROR) 類型錯誤:傳遞給 Illuminate\\Database\\Eloquent\\Model::__construct() 的參數 1 必須是數組類型,字符串給定,在 mypath\\app\\Http\\Controllers 中調用\\PostsController.php 第 65 行

控制器

  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('/');        

}

......

您需要提供請求參數以像這樣以數組格式創建新帖子,

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

所以你的代碼看起來像,

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

);

我希望你會明白。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM