【发布时间】: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/控制器中处理这种类型的设置?
【问题讨论】:
-
只使用数组作为输入?