Laravel将不相关的数据传递给注册视图


Laravel passing unrelated data to the register view

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

这是Auth控制器中的内容:

public function __construct(Guard $auth, Registrar $registrar)
{
    $this->auth = $auth;
    $this->registrar = $registrar;
    $this->middleware('guest', ['except' => 'getLogout']);
}

当我遇到类似的问题时,我得到了解决方法。

第一步

你找到了旅行报名帖子的动作路线。只需输入"php artisan route:list"

如果你正在使用Laravel 5.3,它可以在showRegistrationForm Method中找到。

第二步。

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

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

在旧版本中,它使用getRegister方法来重写动作。

解决方案比我预期的要简单得多。要将数据传递给注册视图,您只需要重写AuthController中的getRegister方法并将数据传递给视图:

public function getRegister()
{
    $products = 'App'Product::all();
    $data = [
        'products' => $products
    ];
    return view('auth.register')->with($data);
}

在Laravel 5.7中,您只需要重写vendor' Laravel 'framework'src'Illuminate'Foundation'Auth

上的方法showRegistrationForm()
public function showRegistrationForm()
{
    //Custom code here
    return view('auth.register');
}

是否有一个加载注册视图的控制器?如果你想传递数据给它,只需使用"with" return view('path.to.registration.view')->withData($someDateFromAnyModel)

在本例中,您现在可以访问注册视图中的"$data"变量,该变量等于$someDateFromAnyModel。