Laravel将不相关的数据传递到寄存器视图

Kev*_*ton 1 php laravel

我需要使用内置的auth将数据从不相关的模型传递到寄存器视图。实现这一目标的最干净的方法是什么?

这是Auth控制器中的内容:

public function __construct(Guard $auth, Registrar $registrar)
{
    $this->auth = $auth;
    $this->registrar = $registrar;

    $this->middleware('guest', ['except' => 'getLogout']);
}
Run Code Online (Sandbox Code Playgroud)

小智 6

当我遇到与您类似的问题时,我在这里得到了解决方案。

第一步

你找到laravel注册帖的动作路线。只需输入“ php artisan route:list

如果您使用的是Laravel 5.3,则可以在showRegistrationForm Method 中找到它。

第二步。

添加新方法作为RegisterController.php中showRegistrationForm方法的覆盖

public function showRegistrationForm()
{
    $product = Product::all();
    return view("auth.register", compact("product"));
}
Run Code Online (Sandbox Code Playgroud)

在旧版本中,它使用 getRegister 方法来覆盖操作。

希望这可以帮助。


Kev*_*ton 5

该解决方案比我预期的容易得多。要将数据传递到寄存器视图,您只需要覆盖getRegister您的方法AuthController并将数据传递到视图:

public function getRegister()
{
    $products = \App\Product::all();
    $data = [
        'products' => $products
    ];

    return view('auth.register')->with($data);
}
Run Code Online (Sandbox Code Playgroud)

  • 在Laravel 5.3中,它称为`showRegistrationForm`。 (2认同)