I have no errors but yet my posts continue to be displayed entirely when I would like to limit them to 10 and have a "more posts" button. I am new to laravel development. I really block on this problem even by turning it over on all sides.
What am i doing wrong ? Thank you for your help !
PostsController
class PostsController extends Controller
{
public function index()
{
$posts = Post::orderBy('created_at', 'desc')
->take(10)
->get();
return view('posts.index', compact('posts'));
}
public function show(Post $post)
{
$posts = Post::orderBy('created_at', 'desc')
->take(10)
->get();
return view('posts.show', compact('post'));
}
public function more(Request $request)
{
$posts = Post::orderBy('created_at', 'desc')
->take(10)
->offset($request->offset)
->get();
return view('posts._list', compact('posts'));
}
}
In views :
@section('content')
<div class="row d-flex" id="list">
@include('posts._list', ['posts' => $posts])
</div>
<div id="wrapper-oldnew">
<div class="oldnew">
<div class="wrapper-oldnew-prev">
<a id="oldnew-prev" data-url="{{ route('ajax.posts.more') }}"></a>
</div>
</div>
</div>
@section('scripts')
<script>
let offset = 10;
$('#oldnew-prev').click(function(e) {
e.preventDefault();
$.get($(this).data('url'), {offset: offset})
.done(function(rep) {
$('#list').append(rep)
.find('.col-md-6:nth-last-of-type(-n+10)')
.addClass('fadeInUp')
.addClass('ftco-animated');
offset = offset + 10;
});
});
</script>
@endsection
<!-- Script Axios - Ajax -->
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
const oldnew = document.getElementbyId('oldnew-prev');
oldnew.onclick = function(){
axios.get('traitement.php')
.then(function(reponseServeur){
});
}
</script>