File resources/js/modules/web/pages/home/Page.js
(link to Github)
import React, { useLayoutEffect } from "react"
import PropTypes from "prop-types"
import Header from "./components/Header"
import Articles from "../../../../common/articles/listing"
import { articleListRequest } from '../../../article/service'
export default function Page({ dispatch }) {
useLayoutEffect(() => {
dispatch(articleListRequest({ url: 'api/v1/articles/published' }))
}, [])
return <div>
<Header/>
<Articles/>
</div>
}
Page.displayName = 'HomePage'
Page.propTypes = {
dispatch: PropTypes.func.isRequired,
}
File routes/api/articles.php
(link to Github)
use Illuminate\Support\Facades\Route;
Route::get('published', 'ArticleController@publishedArticles')->name('articles.published.index');
//
File app/Http/Controllers/Api/ArticleController.php
(link to Github)
use App\Models\Article;
use App\Http\Controllers\Controller;
class ArticleController extends Controller
{
//
public function publishedArticles()
{
return Article::loadAllPublished();
}
//
}
File app/Models/Article.php
(link to Github)
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Pagination\Paginator;
class Article extends Model
{
//
public static function loadAllPublished(): Paginator
{
return static::with([
'user' => function (BelongsTo $query) {
$query->select('id', 'name');
},
])
->latest()
->published()
->paginate();
}
//
}