【问题标题】:Laravel RESTful edit screen with multiple tables带有多个表的 Laravel RESTful 编辑屏幕
【发布时间】:2015-02-06 02:37:08
【问题描述】:

我有 3 个用于存储业务详细信息的表。主表和电话号码表,作为企业可以附加任意数量的电话号码。

表 1 - 商家信息 表 2 - 公司电话号码 表 3 - 将表 1 链接到表 2,如下所示

1   id                  int(11) No  None    AUTO_INCREMENT  
2   listings_id         int(11) No  None        
3   listingsphone_id    int(11) No  None        
4   created_at          timestamp           
5   updated_at          timestamp           

我的控制器从数据库中提取数据并将其传递给视图

public function edit($id)
{
    // get the listing
    $listing = DB::table('listings')
        ->where('listings.id', '=', $id)
        ->first();

    $listingphone = DB::table('listings')
        ->leftjoin('listings_listingsphone_rel', 'listings.id', '=', 'listings_listingsphone_rel.listings_id')
        ->leftjoin('listingsphone', 'listings_listingsphone_rel.listingsphone_id', '=', 'listingsphone.id')
        ->where('listings.id', '=', $id)
        ->get();

    // show the edit form and pass the listing
    return View::make('admin.listings.edit')
        ->with('listing', $listing)
        ->with('listingphone', $listingphone);
}

Blade 然后需要在表单中创建多个 Input 行以输出到屏幕,每一行对应一个存在的行。

{{ Form::model($listing, array('route' => array('admin.listings.update', $listing->id), 'method' => 'PUT')) }}

<div class="form-group">
    {{ Form::label('listingname', 'Company Name') }}
    {{ Form::text('listingname', null, array('class' => 'form-control')) }}
</div>


@foreach($listingphone as $key => $value)
<div class="form-group">
    {{ Form::label('phone', 'Phone number') }}
    {{ Form::text('phone', null, array('class' => 'form-control')) }}
</div>
@endforeach

{{ Form::submit('Edit the Listing!', array('class' => 'btn btn-primary')) }}

{{ Form::close() }}

代码的问题是,它会为我生成多个具有相同名称和 ID 的输入行

<label for="phone">Phone</label>
<input class="form-control" name="phone" type="text" id="phone">

<label for="phone">Phone</label>
<input class="form-control" name="phone" type="text" id="phone">

我显然需要使用如下代码(伪代码)将数据存储在控制器中:-

    // store
        $listing = Listing::find($id);
        $listing->listings_company        = Input::get('listings_company'); 

        $listing->save();


        // store
        $listingphone = ListingPhone::find($id);

        foreach ($listingphone as $key => $value)
        {
            $listingphone->listings_phone        = Input::get('phone['$key']'); 
            $listingphone->save();
        }

我应该如何在 Blade/控制器中处理这种类型的设置?

【问题讨论】:

  • 只使用数组作为输入?

标签: laravel blade


【解决方案1】:

我假设您遇到的文本表单字段问题与您在拥有复选框时需要处理的问题相同。无论如何,您有多个同名字段需要引用。

为此,您需要在表单字段名称上使用方括号。

您可以在此处查看与复选框相关的类似示例:

How to get the values for a series of checkboxes in Laravel 4 controller (if checked)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-26
    • 2017-10-04
    • 1970-01-01
    • 2019-03-22
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    相关资源
    最近更新 更多