簡體   English   中英

Laravel model 通過查看

[英]Laravel model pass to view

我正在使用這個庫: https://www.laravelplay.com/packages/ycs77::laravel-wizard

我完成了所有步驟並得到了與示例中相同的結果。

我試圖從數據庫中獲取數據到每個步驟。

Model(應用程序/步驟/介紹/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

也通過 controller (IntroWizardController.php) 進行了嘗試

這是默認的 controller:

<?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',
        ];
    }

在 controller 中也嘗試過返回視圖,但隨后我從空白頁面中的數據庫中獲取結果,而不是其他部分。

是否可以通過數據庫查詢來查看這個庫? 謝謝

編輯:有了這條路線:

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

我從數據庫中得到我的結果,但就像我之前在白色空白頁中所說的那樣: 空白頁

但我想像其他人一樣進入這個視圖(無需詢問): 腳步

要將任何數據從 controller 傳遞到刀片視圖,只需使用這 2 個選項

選項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');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM