在laravel 5.4中调用null上的成员函数name()

When pressing my send button it's giving error like this-

enter image description here

Here is my routes web.php bellow-

Route::group(['prefix'=>'ajax', 'as'=>'ajax::'], function() {
   Route::resource('message/send', 'MessageController@ajaxSendMessage')->name('message.new');
   Route::delete('message/delete/{id}', 'MessageController@ajaxDeleteMessage')->name('message.delete');
});

Here is my controller MessageController.php bellow:

public function ajaxSendMessage(Request $request)
{
    if ($request->ajax()) {
        $rules = [
            'message-data'=>'required',
            '_id'=>'required'
        ];

        $this->validate($request, $rules);

        $body = $request->input('message-data');
        $userId = $request->input('_id');

        if ($message = Talk::sendMessageByUserId($userId, $body)) {
            $html = view('ajax.newMessageHtml', compact('message'))->render();
            return response()->json(['status'=>'success', 'html'=>$html], 200);
        }
    }
}

Resource routes should be named differently:

Route::prefix('ajax')->group(function () {
    Route::resource('messages', 'MessageController', ['names' => [
        'create' => 'message.new',
        'destroy' => 'message.destroy',
    ]]);
});

Resource routes also point to a controller, instead of a specific method. In MessageController, you should add create and destroy methods.

More info at https://laravel.com/docs/5.4/controllers#restful-naming-resource-routes

Look at web.php line 28.

Whatever object you think has a name() method, hasn't been set, therefore you try and call a method on null.

Look before that line and see where it is (supposed to be) defined, and make sure it is set to what it should be!

You can't name a resource. Laravel by default name it, if you want to name all routes you must specify each one explicitly. It should be like this:

Route::group(['prefix'=>'ajax', 'as'=>'ajax::'], function() {
   Route::get('message/send', 'MessageController@ajaxSendMessage')->name('message.new');
   Route::delete('message/delete/{id}', 'MessageController@ajaxDeleteMessage')->name('message.delete');
});

Update

Another mistake of yours was trying to resource a single method. A Route::resource() is used to map all basic CRUD routes in Laravel by default. Therefore, you have to pass the base route and the class i.e:

<?php
Route::resource('message', 'MessageController');