Laravel模型传递查看

问题描述 投票:0回答:1

我使用这个库。https:/www.laravelplay.compackagesycs77::laravel-wizard

我做了所有的步骤,结果和例子中一样。

我试图从数据库获取数据到每一步。

模型 (AppstepsintroDropboxStep.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',
        ];
    }

尝试在控制器中也返回到视图,但我从数据库中得到的结果是空白页,而不是与其他部分。

是否可以用这个库将数据库查询传递给视图?

EDIT:有了这个路线,

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

我从数据库中得到了我的结果,但就像我之前说的,在白色的空白页中。Blank page

但我想在这个视图中得到像其他人一样的结果(无需查询)。Steps

php laravel wizard
1个回答
0
投票

要将任何数据从控制器传递到刀片视图,只需使用以下两个选项。

选择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');
© www.soinside.com 2019 - 2024. All rights reserved.