Laravel 模型传递给视图

Laravel model pass to view

我正在使用这个库:https://www.laravelplay.com/packages/ycs77::laravel-wizard

我执行了所有步骤并得到了与示例中相同的结果。

我正在尝试从数据库获取数据到每个步骤。

型号(App/steps/intro/DropboxStep.php):

<?php

namespace App\Steps\Intro;

use Illuminate\Http\Request;
use Ycs77\LaravelWizard\Step;

use DB;

class DropboxStep extends Step
{
    /**
     * The step slug.
     *
     * @var string
     */
    protected $slug = 'dropbox';

    /**
     * The step show label text.
     *
     * @var string
     */
    protected $label = 'Dropbox';

    /**
     * The step form view path.
     *
     * @var string
     */
    protected $view = 'steps.intro.dropbox';

    /**
     * Set the step model instance or the relationships instance.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation|null
     */
    public function model(Request $request)
    {
        //
    }

    /**
     * Save this step form data.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  array|null  $data
     * @param  \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation|null  $model
     * @return void
     */
    public function saveData(Request $request, $data = null, $model = null)
    {
        //
    }

    /**
     * Validation rules.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function rules(Request $request)
    {
        return [];
    }

    public function getOptions()
    {

        $stepa2 = DB::table('tutorials')->where('id', '2')->first();

        return [
            'stepa2' => $stepa2,
            'Lucas',
        ];
    }
}

查看:

<div class="form-group">
    {{ $stepa2 }}
</div>

结果: 未定义变量:stepa2

也尝试通过控制器 (IntroWizardController.php)

这是默认控制器:

<?php

namespace App\Http\Controllers;

use App\Steps\Intro\DropboxStep;
use App\Steps\Intro\H2NStep;
use App\Steps\Intro\PT4Step;
use Ycs77\LaravelWizard\Wizardable;
use DB;



class IntroWizardController extends Controller
{
    use Wizardable;

    /**
     * The wizard name.
     *
     * @var string
     */
    protected $wizardName = 'intro';

    /**
     * The wizard title.
     *
     * @var string
     */
    protected $wizardTitle = 'Intro';

    /**
     * The wizard options.
     *
     * @var array
     */
    protected $wizardOptions = [];

    /**
     * The wizard steps instance.
     *
     * @var array
     */
    protected $steps = [
        DropboxStep::class,
        H2NStep::class,
        PT4Step::class,
    ];

}

我补充说:

public function getOptions()
    {

        $stepa2 = DB::table('tutorials')->where('id', '2')->first();

        return [
            'stepa2' => $stepa2,
            'Lucas',
        ];
    }

也尝试在控制器 return 中查看,但后来我在空白页面中从数据库中获取结果,而不是其他部分。

是否可以通过数据库查询来查看这个库? 谢谢

编辑: 使用这条路线:

Route::get('wizard/intro/dropbox', 'IntroWizardController@step1a', 'wizard.intro.dropbox');
Wizard::routes('wizard/intro', 'IntroWizardController', 'wizard.intro');

我从数据库中获取结果,但就像我之前在空白页中所说的那样:

但我想像其他人一样进入这个视图(无需查询):

要将任何数据从控制器传递到 blade 视图,只需使用这两个选项

选项 1:

public function someFunction()
{
    $model = DB::table('model_table')->where('id', '2')->first();

    return view('blade_view_name', compact('model'));
}

选项 2:

public function someFunction()
{
    $model = DB::table('model_table')->where('id', '2')->first();

    return view('blade_view_name')->with('model', $model);
}

如果您有更多或想要更多变量,您可以像这样链接 with() 方法:

return view('blade_view_name')
     ->with('model', $model)
     ->with('variable', 'Some other variable');