【发布时间】:2014-04-28 17:44:52
【问题描述】:
我是 Laravel 的新手,我肯定对此很愚蠢,因为我已经阅读了文档并且我已经在谷歌上进行了搜索,但我不知道如何解决这个问题。我在画廊和艺术家之间有着 M:M 的关系。在每个图库编辑页面中,我都有一个表单来更新运行正常的图库的名称和 url。在同一页面中,我还有另外 2 个选择表单,一个用于将艺术家添加到画廊,另一个用于删除艺术家,它们需要更新一个名为“galleries_artists”的数据透视表。我为这两种表单创建了 2 个自定义方法,称为“postAdd”和“postRemove”,但无论我尝试什么,我都无法让它们发挥作用。 下面是我到目前为止的代码。希望有人能帮助我理解我正在犯的愚蠢错误。
模型 - Artist.php
class Artist extends Eloquent {
protected $fillable = array('name');
public static $rules = array(
'name'=>'required|min:2'
);
public function galeries() {
return $this->belongsToMany('Gallery', 'galeries_artists', 'artist_id', 'gallery_id', 'stand_id');
}
}
模型 - Gallery.php
class Gallery extends Eloquent {
protected $fillable = array('name', 'stand_id', 'url');
public static $rules = array(
'stand_id'=>'required|integer'
);
public function stand() {
return $this->belongsTo('Stand');
}
public function artist() {
return $this->belongsToMany('Artist', 'galleries_artists', 'gallery_id', 'artist_id', 'stand_id');
}
}
控制器 - GalleriesController.php
public function postAdd($id, $aid) {
$input = array_except(Input::all(), '_method');
$v = Validator::make(Input::all(), Artist::$rules);
if ($v->passes()) {
$gallery = Gallery::find($id);
$add_artist = Input::get();
$add_artist->galleries()->attach(Input::get('add_artist'));
$add_artist->save();
return Redirect::route('admin.galleries.edit')
->with('message', 'Artist added successfully.');
}
return Redirect::route('admin.galleries.edit')
->with('message', 'Something went wrong')
->withErrors($v)
->withInput();
}
public function postRemove($id, $aid) {
$input = array_except(Input::all(), '_method');
$v = Validator::make(Input::all(), Artist::$rules);
if ($v->passes()) {
$gallery = Gallery::find($id);
$remove_artist = Input::get();
$remove_artist->galleries()->detach(Input::get('remove_artist'));
$remove_artist->save();
return Redirect::route('admin.galleries.edit')
->with('message', 'Artist removed successfully.');
}
return Redirect::route('admin.galleries.edit')
->with('message', 'Something went wrong')
->withErrors($v)
->withInput();
}
edit.blade.php 添加表单
{{ Form::open(array('class' => '', 'method' => 'put', 'action'=> array('GalleriesController@postAdd', $gallery->id , $add_artist->id ))) }}
<div class="form-group">
{{ Form::label('Add Artist:') }}
{{ Form::select('add_artist', $other_artists_name, null, array('class'=>'form-control')) }}
</div>
{{ Form::button('Add Artist', array('type' => 'submit', 'class'=>'btn btn-primary')) }}
{{ Form::close() }}
edit.blade.php 删除表格
{{ Form::open(array('class' => '', 'method' => 'put', 'action'=>array('GalleriesController@postRemove', $id , 'aid'))) }}
<div class="form-group">
{{ Form::label('Remove Artist:') }}
{{ Form::select('remove_artist', $gallery_artists_name, null, array('class'=>'form-control')) }}
</div>
{{ Form::button('Remove Artist', array('type' => 'submit', 'class'=>'btn btn-danger')) }}
{{ Form::close() }}
Routes.php
Route::post('admin/galleries/{galleries}/add/{aid}', 'GalleriesController@postAdd');
Route::post('admin/galleries/{galleries}/remove/{aid}', 'GalleriesController@postRemove');
Route::resource('admin/galleries', 'GalleriesController');
我一直在对代码进行如此多的更改,以至于很多事情可能会混淆。对不起,如果是这样的话。
【问题讨论】:
标签: select laravel laravel-4 many-to-many pivot-table