Skip to main content

JaskaranSingh4704/laravel-react

0 stars
4 code files
View JaskaranSingh4704/laravel-react on GitHub

resources/js/modules/web/pages/home/Page.js

Open in 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,
}

routes/api/articles.php

Open in GitHub
use Illuminate\Support\Facades\Route;
 
Route::get('published', 'ArticleController@publishedArticles')->name('articles.published.index');
//

app/Http/Controllers/Api/ArticleController.php

Open in GitHub
use App\Models\Article;
use App\Http\Controllers\Controller;
 
class ArticleController extends Controller
{
//
public function publishedArticles()
{
return Article::loadAllPublished();
}
//
}

app/Models/Article.php

Open in 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();
}
//
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.