I need a recommendation. I want to have a sidebar with all the titles of the posts and by clicking on any of them that this is loaded with its content in the main section of the page. The idea is that when loading the page not all the contents of each post are loaded, but only when clicking. What system can I use if I am using Laravel?
You can return your post to the view using your controller:
public function someFunction()
{
$posts = Post::all();
return view('your-view', compact('posts'));
}
Now, within your blade template, use the laravel @foreach
blade directive to create your links to each post:
Wihtin your view, where you want to display the links to the posts in your sidebar:
@foreach($post as $post)
<a href="{{ route('posts.show', $post->id) }}>
{{ $post->title }}
</a>
@endforeach
This code will create a link to each of the posts you have. In order to the route('posts.show', $post->id)
work, you need to have a resource route for your post controller or define this route:
Route::get('posts/{post}', 'YourPostController@show')->name('posts.show');
Then, just write your show
method withing your post controller:
public function show($id)
{
$post = Post::find($id);
return view('your-view', compact('post');
}
Hope it helps.