Reputation: 4526
I am trying to assign users as moderators to forum categories (subreddits) in a pivot table called moderators
Here are the tables
moderators: id, user_id, subreddit_id, created_at, updated_at
users: id: name: email...
subreddits: id, user_id, name...
I have created the view, controller and model but data won't submit to the database and I get no error.
Note: I need to visit this route subreddit/{id}/moderators/create
in order to get to the view and start submitting.
EDIT: I just emptied the store()
function and replaced it with return 'done';
but that doesn't seem to take effect. Upon clicking the submit button, the page just reloads and nothing happens. So maybe the problem is in the view?
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
Model
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
- here I am trying to submit the user_id and subreddit_it manually just to test that it is working.
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();
}
And this is the form inside the view, I am using Typeahead.js that's why there's div with 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'
];
}
Upvotes: 3
Views: 2055
Reputation: 1454
Try foreach ($messages->all() as $message)
according to the documentations.
The problem is that your ModeratorRequest
is returning validations error, which you don't display on the view.
Upvotes: 3