如何在laravel中的表单值字段中获取数组数据?

时间:2017-04-14 05:40:29

标签: php laravel

我正在使用表单视图传递变量以从表单中获取数据库中的值。

public function getGeneralSettings()
    {

        $generalsetting = DB::table('generalsettings')
                ->where('id', '=', 1)
                ->get();
        return view('backend.generalsettings.add-general')->with('general', $generalsetting);
    }

我得到的是:

[{"id":1,"sitename":"eqwe","tagline":"ewqewq","logo":"1492084338.jpg","firstemail":"qwewq","secondemail":"ewqewq","firstphone":21321,"secondphone":32132,"address":"
dassa<\/p>","facebook":"dqw","twitter":"q","linkedin":"q","google":"q","youtube":"q","instagram":null,"reddit":"q","rss":"q","created_at":"2017-04-13 11:52:18","updated_at":"2017-04-13 11:52:18"}]

我在表单字段中显示这些值。

<div class="form-group">
                        <label for="sitename">Site Name</label>
                        <input type="text" id="sitename" name="sitename" class="form-control" value="" placeholder="Site name">
                    </div>

                    <div class="form-group">
                        <label for="tagline">Tagline</label>
                        <input type="text" id="tagline" name="tagline" placeholder="Tagline" value="" class="form-control">
                    </div>

如何在特定值字段中调用它们。请帮忙

2 个答案:

答案 0 :(得分:1)

只需使用{{}}

即可
<div class="form-group">
    <label for="sitename">Site Name</label>
    <input type="text" id="sitename" name="sitename" class="form-control" value="{{$general->sitename}}" placeholder="Site name">
</div>

<div class="form-group">
    <label for="tagline">Tagline</label>
    <input type="text" id="tagline" name="tagline" placeholder="Tagline" value="{{$general->tagline}}" class="form-control">
</div>

还可以使用first()代替get()来获取单个对象

答案 1 :(得分:1)

将控制器代码更改为

first()

每当您想要检索单行结果时,请使用get()代替get(),{{1}}用于获取多个记录。