Cla*_*tta 2 database many-to-many laravel eloquent laravel-4
我正在尝试使用Laravel 4修复我的应用程序的问题.问题是关于Eloquent和M:M的关系.
这是我的情况:我有很多用户可以选择多种服务,每种服务都可以提供专用价格.在数据库中,我有名为"services","users"和"users_services"的表.所以目前我可以保存每个用户选择的服务和价格,但我的问题是我不知道如何回复并向用户展示,我的意思是我不知道如何设置"已检查"每个选定服务的复选框以及每个服务的价格.
您可以在Paste Laravel上查看代码以获取任何分支.我也把它贴在这里.
<?php
class Service extends Eloquent {
public static $rules = array();
protected $guarded = array('id');
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'services';
/**
* -------------------------------------
* All Joins w/ other tables
* -------------------------------------
**/
public function users()
{
return $this->belongsToMany('User','users_services','services_id','user_id');
}
}
......
<?php
class User extends Eloquent {
public static $rules = array();
protected $guarded = array('id');
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* -------------------------------------
* All Joins w/ other tables
* -------------------------------------
**/
public function services()
{
return $this->belongsToMany('Service','users_services','user_id','service_id');
}
}
................
// Inside the controller
$services = Service::all();
$this->layout->content = View::make('private.user.services')->with(compact('services'));
...............
// The view
@foreach($services as $service)
<div class="pull-left col-lg-6 service-container">
<div class="checkbox pull-left">
<label>
<input type="checkbox" name="services[]" value="{{ $service->id }}"><span>{{ $service->name }}</span>
</label>
</div>
<div class="input-group service-price pull-right">
<input type="number" min="0" name="prices[{{$service->id}}]" class="form-control pull-rigth" placeholder="0">
<span class="input-group-btn">
<button class="btn btn-default" type="button" disabled><i class="icon-eur"></i></button>
</span>
</div>
</div>
@endforeach
Run Code Online (Sandbox Code Playgroud)
在您看来,当您循环浏览所有可能的服务时,您需要检查当前用户是否已选择该服务.
一种方法是将第二个集合发送到用户服务的页面,然后使用该contains()方法进行检查.
在Controller中,将用户关系中的服务拉入新变量:
$services = Service::all();
$user_services = $user->services;
$this->layout->content = View::make('private.user.services')->with(compact('services'))->with('user_services', $user_services);
Run Code Online (Sandbox Code Playgroud)
在您的视图中,只需将选中的attr添加到复选框即可.
@foreach($services as $service)
<div class="pull-left col-lg-6 service-container">
<div class="checkbox pull-left">
<label>
<input type="checkbox" name="services[]" value="{{ $service->id }}" @if( $user_services->contains($service->id) ) selected@endif><span>{{ $service->name }}</span>
</label>
</div>
<div class="input-group service-price pull-right">
<input type="number" min="0" name="prices[{{$service->id}}]" class="form-control pull-rigth" placeholder="0">
<span class="input-group-btn">
<button class="btn btn-default" type="button" disabled><i class="icon-eur"></i></button>
</span>
</div>
</div>
@endforeach
Run Code Online (Sandbox Code Playgroud)
注意,它没有经过测试,我不知道模型之间的关系..但它应该没问题:)
(注意,你的某个课程中有拼写错误.. pull-rigth)
| 归档时间: |
|
| 查看次数: |
4106 次 |
| 最近记录: |