Laravel 在 get() 中缺少参数 1

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37909799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:01:37  来源:igfitidea点击:

Laravel Missing argument 1 in get()

phpdatabaselaravelmodel

提问by scottbear

I am trying to get all records from a table named 'producers', but I get the following error.

我试图从名为“producers”的表中获取所有记录,但出现以下错误。

Missing argument 1 for Illuminate\Support\Collection::get(), called in /var/www/html/wines/storage/framework/views/ac350063efb624ac50d199628897fd7d72bc196c.php on line 63 and defined (View: /var/www/html/wines/resources/views/admin/producers.blade.php)

Here are my files: (migration)

这是我的文件:(迁移)

<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateProducerTable extends Migration    {
    public function up()
    {
        Schema::create('producers', function (Blueprint $table) {
            $table->increments('id');
            $table->string('producer_name');
            $table->string('phone_number');
            $table->string('user_id')->unique();
            $table->string('avatar')->default('default.jpg');
            $table->timestamps();
        });
    }
    public function down()
    {
        Schema::drop('producers');
    }
}

My ProducerController

我的 ProducerController

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class ProducerController extends Controller    {
    public function __construct()        {
        $this->middleware('isProducer');
    }    }

The producer model:

生产者模型:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Producer extends Model    {
    protected $table = 'producers';
    protected $fillable = [
        'id','producer_name', 'phone_number','user_id','avatar'
    ];
    protected $hidden = [
    ];
}

Then when I use

然后当我使用

public function admin_producers()    {
        return view('admin.producers',array('user'=>Auth::user(),'producer'=>Producer::get()));
    }

on the controller to get all my records I get that error. I have also tried with Producer::all() but with the same result, no luck. I am doing something stupid here?

在控制器上获取我的所有记录我得到那个错误。我也尝试过 Producer::all() 但结果相同,没有运气。我在这里做一些愚蠢的事情?

回答by Muhammad Sumon Molla Selim

The Eloquent all()method will return all of the results in the model's table. Use get()only, when you need to add constraints to queries.

Eloquentall()方法将返回模型表中的所有结果。使用get()而已,当你需要添加约束查询。

So, use all()instead of get().

因此,使用all()代替get().