Skip to content

Instantly share code, notes, and snippets.

@evarioooo
Last active December 22, 2023 20:40
Show Gist options
  • Save evarioooo/a4ee740c2f55db656be7cf3a32928ace to your computer and use it in GitHub Desktop.
Save evarioooo/a4ee740c2f55db656be7cf3a32928ace to your computer and use it in GitHub Desktop.
Laravel 10 | Articles + Category wedding
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\ArticleCategorys;
use Illuminate\Support\Str;
class Article extends Model
{
use HasFactory;
protected $fillable = [
'slug',
'category_id',
'author',
'headline',
'content'
];
public function categorys()
{
return $this->belongsTo(ArticleCategorys::class);
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Article;
class ArticleCategorys extends Model
{
use HasFactory;
protected $table = 'article_categorys';
public function articles()
{
return $this->hasMany(Article::class, 'category_id', 'id');
}
}
<?php
namespace App\Http\Controllers;
use App\Models\Article;
use App\Models\ArticleCategorys;
use Illuminate\Http\Request;
use Illuminate\View\View;
class ArticleCategorysController extends Controller
{
public function index(): View
{
$articlecategorys = ArticleCategorys::latest()->paginate(config('app.default_paginate'));
return view('categorys.index',compact('articlecategorys'))->with('i', (request()->input('page', 1) - 1) * 5);
}
public function show($slug)
{
$articlecategorys = ArticleCategorys::with('articles')->where('slug',$slug)->first();
//dd($articlecategorys);
return view('categorys.show',compact('articlecategorys'));
}
}
<x-app-layout>
<div class="bg-primary-c p-4 border border-primary-custom" id="articles_wrapper">
<h1>{{ __('categorys.PageTitle') }}</h1>
{{ $articlecategorys->name }}
<section class="content">
@foreach($articlecategorys->articles as $articles)
{{ $articles->headline }}
@endforeach
</section>
</div>
</x-app-layout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment