我正在尝试将用户作为主持人分配到名为moderators
以下是表格
主持人:id,user_id,subreddit_id,created_at,updated_at
用户:id:name:email ...
subreddits:id,user_id,name ...
我创建了视图,控制器和模型,但数据不会提交到数据库,我没有收到任何错误。
注意:我需要访问此路线subreddit/{id}/moderators/create
才能进入视图并开始提交。
编辑:我刚刚清空store()
函数并将其替换为return 'done';
,但这似乎并未生效。单击提交按钮后,页面只会重新加载,没有任何反应。那么问题可能在视图中?
routes.php文件
Route::resource('subreddit', 'SubredditController');
Route::resource('subreddit.moderators', 'ModeratorsController');
RouteServiceProvider
$router->model('articles', 'App\Article');
$router->model('subreddit', 'App\Subreddit');
$router->model('posts', 'App\Post');
$router->model('moderators', 'App\Moderator');
Moderator
模型
protected $table = 'moderators';
protected $fillable = ['user_id', 'subreddit_id'];
public function subreddit() {
return $this->belongsTo('App\Subreddit');
}
public function user() {
return $this->belongsTo('App\User');
}
ModeratorsController
- 这里我试图手动提交user_id和subreddit_it,以测试它是否有效。
public function create(Moderator $moderator, Subreddit $subreddit, User $user)
{
$subreddit = Subreddit::with('user')->findOrFail($subreddit->id);
return view('user/moderators')->with(compact('subreddit'));
}
public function store(Requests\ModeratorRequest $request, Subreddit $subreddit)
{
$moderator = new Moderator;
$moderator->user_id = 2;
$moderator->subreddit_id = 17;
$moderator->save();
}
这是视图中的表单,我正在使用Typeahead.js,这就是为什么有id="remote"
{!! Form::open(['url' => 'subreddit/' . $subreddit->id . '/moderators', 'method' => 'POST']) !!}
<p>
<div id="remote">
<input class="form-control typeahead" type="text" placeholder="Choose a Username" name="user_name">
<input type="hidden" class="user_id" value="" name="user_id">
</div>
</p>
<p>
{!! Form::submit('Submit Post', ['id' => 'submit', 'class' => 'btn btn-primary']) !!}
</p>
{!! Form::close() !!}
ModeratorRequest
public function authorize()
{
return true;
}
public function rules()
{
return [
'user_id' => 'required',
'subreddit_id' => 'required'
];
}
答案 0 :(得分:2)
根据documentations尝试delete existing.product_code;
。
问题是您的foreach ($messages->all() as $message)
正在返回验证错误,您无法在视图上显示。