如何修复“Illuminate\Support\Collection::get(), 0passed in /AMPPS/www/lsapp/vendor/laravel/framework/src/

Posted

技术标签:

【中文标题】如何修复“Illuminate\\Support\\Collection::get(), 0passed in /AMPPS/www/lsapp/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php”【英文标题】:How to fix"Illuminate\Support\Collection::get(), 0passed in /AMPPS/www/lsapp/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php"如何修复“Illuminate\Support\Collection::get(), 0passed in /AMPPS/www/lsapp/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php” 【发布时间】:2019-11-01 15:50:11 【问题描述】:

为了对搜索栏进行编程,我创建了一个 GET 方法并添加了一个新控制器,用于获取相关数据并将其与视图一起返回。

    //This is the form in the view named "index.blade.php"

    !! Form::open(['action' => 'SearchesController@search', 'method' => 'GET']) !!   
        <form class="form-inline md-form mr-auto mb-4">

            Form::text('search', '', ['class'=>'form-control', 'placeholder'=>'Search Anything'])

            <button class="btn aqua-gradient btn-rounded btn-sm my-0" type="submit">Search</button>

       </form>
    !! Form::close() !!

    //---------------------------------------------------------

    //This is in the "web.php"

    Route::get('/posts/search', 'SearchesController@search');

    //---------------------------------------------------------

    //The following code is the controller named "SearchesController.php"

    <?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;

    use Illuminate\Support\Facades\Storage;
    use App\Post;
    use DB;

    use Illuminate\Support\Facades\Auth;

    class SearchesController extends Controller
    
        public function search(Request $request)
        
            $this->validate($request, [
                 'search' => 'required',
            ]);

            $search = metaphone($request->input('search'));


            $posts = Post::where('sounds_like','LIKE',"%$search%")
                ->orderBy('title', 'desc')
                ->paginate(10)
                ->get();

            return view('posts.index')->with('posts',$posts);
        
    

我期望一个视图的输出包含视图返回的所有数据,但却得到了 错误 消息:

“函数 Illuminate\Support\Collection::get() 的参数太少,在 /Applications/AMPPS/www/lsapp/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php 中传递了 0第 23 行,预计至少有 1 行”

【问题讨论】:

【参考方案1】:

如果您使用的是paginate(),则不需要get()。 Paginate 也将执行查询并创建一个集合,get() 方法将作为此 get() 的实例执行,并且需要 key 参数。所以你可以删除它:

public function search(Request $request)

    // ...


    $posts = Post::where('sounds_like','LIKE','%'. $search . '%')
        ->orderBy('title', 'desc')
        ->paginate(10);

    return view('posts.index')->with('posts',$posts);

与错误无关,但你也有一个form inside a form。选择一个并删除另一个:

!! Form::open(['action' => 'SearchesController@search', 'method' => 'GET', 'class'=>'form-inline md-form mr-auto mb-4']) !! 

        Form::text('search', '', ['class'=>'form-control', 'placeholder'=>'Search Anything'])

        <button class="btn aqua-gradient btn-rounded btn-sm my-0" type="submit">Search</button>

!! Form::close() !!

【讨论】:

以上是关于如何修复“Illuminate\Support\Collection::get(), 0passed in /AMPPS/www/lsapp/vendor/laravel/framework/src/的主要内容,如果未能解决你的问题,请参考以下文章

如何修复drv?

如何修复漏洞

如何修复WMI

PHP网站漏洞怎么修复 如何修补网站程序代码漏洞

如何修复这些漏洞? (npm audit fix 无法修复这些漏洞)

如何修复AppScan漏洞