I have a search() method in SubredditController
public function search(Request $request, Subreddit $subreddit)
{
$query = $request->input('search');
$subreddit = Subreddit::with('posts.votes')->with('moderators.user')->where('id', 24)->first();
$posts = $subreddit->posts()->where('title', 'LIKE', '%' . $query . '%')->get();
$isModerator = $subreddit->moderators()->where('user_id', Auth::id())->exists();
$modList = Moderator::where('subreddit_id', '=', $subreddit->id)->get();
return view('subreddit.search', compact('query', 'subreddit', 'posts', 'isModerator', 'modList'));
}
As you can see $subreddit is getting all posts from subreddit ID 24 and the query works.
But when I replace 24 with $subreddit->id the query fails and gives null.
EDIT: Looks like it works on $modList because I have hardcoded it 24 in the first variable $subreddit but that still doesn't answer why $subreddit->id is inaccessible when it works fine on all other methods of the same controller
EDIT 2: I have also tried changing the search route to accept a parameter, like this Route::post('search/{subreddit}') but that gives the error NotFoundHttpException and the URL redirects to localhost/reddit/public/search/%7Bsubreddit%7D it's not recognizing any parameter.
EDIT 3: if I change the search route to Route::post('subreddit/{id}/search', ...) I get no errors, but $subreddit->id remains inaccessible and the URL points to subreddit/%7Bid%7D/search (with %7B characters)
These are my bindings
$router->model('subreddit', 'App\Subreddit');
$router->model('posts', 'App\Post');
$router->model('moderators', 'App\Moderator');
My Routes
Route::resource('subreddit', 'SubredditController');
Route::resource('subreddit.moderators', 'ModeratorsController');
Route::get('mysubreddits', [
'as' => 'mysubreddits',
'uses' => 'SubredditController@mySubreddits'
]);
Route::post('search', ['as' => 'search', 'uses' => 'SubredditController@search']);
Route::resource('posts', 'PostsController');
Route::resource('votes', 'VotesController');
Route::resource('profile', 'ProfilesController');
The view (which is a partial) from where I'm sending the search request
<h4>Search {{ $subreddit->name }}</h4>
{!! Form::open(['action' => 'SubredditController@search']) !!}
<div id="custom-search-input">
<div class="input-group col-md-12">
<input type="text" name="search" class="search-query form-control" placeholder="Search" />
<span class="input-group-btn">
<button class="btn btn-danger" type="submit">
<span class=" glyphicon glyphicon-search"></span>
</button>
</span>
</div>
</div>
{!! Form::close() !!}
$subreddit->idisn't working at all, even though it works on all other methods in the same controller.searchone, and you don't specify any route parameters (Route::post('search', ...);ratehr thanRoute::post('search/{subreddit}', ...)so that's why it's not being populated. Right?Form::open(['route' => ['search', $subreddit->getKey()]])(I may be wrong about the format of theForm::openwith route specified but you get the idea - you need to pass the ID in so that it turns into/search/24and so that your model can be bound correctly.