簡體   English   中英

Laravel array_merge():參數 #2 不是數組錯誤

[英]Laravel array_merge(): Argument #2 is not an array Error

所以,我是 Laravel 的新手,我目前正在學習技術日記的教程( https://www.techiediaries.com/php-laravel-crud-mysql-tutorial/

我真的沒有任何使用 Laravel 或任何框架的經驗,但我在 PHP 方面有經驗,這是 Laravel 的第一次嘗試。

我收到以下消息:

ErrorException array_merge():參數 #2 不是數組

當我查看http://localhost/laravel-first-crud-app/public/

Route::get('/', function () {
    return view('contacts.index', 'ContactController');
    //return 'Hello';
});

Route::resource('contacts', 'ContactController');
//Route::apiResource('contacts', 'ContactController'); //Not sure if this should be here or not

在 routes/web.php 但我在教程中看不到如何訪問我創建的頁面

不確定我的contactController.php文件是否有幫助:

public function index()
    {
         $contacts = Contact::all();

         return view('contacts.index', compact('contacts'));
    }

它是 Laravel 6 和 PHP7.1

任何幫助將非常感激

謝謝

您的問題是以下聲明:

return view('contacts.index', 'ContactController');

view function 將字符串作為第一個參數,將數組作為第二個參數,以便將數據傳遞給視圖。 不確定您將 Controller 名稱的字符串作為數據傳遞的意圖是什么。

如果沒有數據要發送到視圖,請不要使用第二個參數:

return view('contacts.index');

Laravel 6.x 文檔 - 視圖 - 將數據傳遞給視圖

教程中的路線:

Route::get('/', function () {
    return view('welcome');
});

Route::resource('contacts', 'ContactController');

Laravel 使用此語法將路由鏈接到 controller 方法

我想這就是你想要做的

Route::get('/', 'ContactController@index');

順便說一句,由於 Laravel 5.5... 您也可以直接鏈接到沒有 controller 的視圖並傳遞參數

Route::view('/', 'contacts.index', ['contacts' => App\Contact::all()]);

查看查看路線希望這會有所幫助

暫無
暫無

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

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