Last active
December 22, 2023 20:40
-
-
Save evarioooo/a4ee740c2f55db656be7cf3a32928ace to your computer and use it in GitHub Desktop.
Laravel 10 | Articles + Category wedding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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')); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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