### 1. Folder Structure Overview (Laravel default + news portal specific)
– app/
– Models/
– News.php
– Category.php
– database/
– migrations/
– xxxx_xx_xx_create_news_table.php
– xxxx_xx_xx_create_categories_table.php
– routes/
– web.php
– app/
– Http/
– Controllers/
– NewsController.php
– CategoryController.php
– resources/
– views/
– layouts/
– app.blade.php
– home.blade.php
– news/
– index.blade.php
– show.blade.php
– categories/
– index.blade.php
– public/
– images/
### 2. Migrations
// create_categories_table.php
“`php
Schema::create(‘categories’, function (Blueprint $table) {
$table->id();
$table->string(‘name’);
$table->timestamps();
});
“`
// create_news_table.php
“`php
Schema::create(‘news’, function (Blueprint $table) {
$table->id();
$table->string(‘title’);
$table->text(‘content’);
$table->string(‘image’)->nullable();
$table->string(‘slug’)->unique();
$table->foreignId(‘category_id’)->constrained();
$table->foreignId(‘user_id’)->constrained();
$table->timestamp(‘published_at’)->nullable();
$table->timestamps();
});
“`
### 3. Models
// News.php
“`php
class News extends Model
{
use HasFactory;
protected $fillable = [‘title’, ‘content’, ‘slug’, ‘image’, ‘category_id’, ‘user_id’, ‘published_at’];
public function category() {
return $this->belongsTo(Category::class);
}
public function user() {
return $this->belongsTo(User::class);
}
}
“`
// Category.php
“`php
class Category extends Model
{
use HasFactory;
protected $fillable = [‘name’];
public function news() {
return $this->hasMany(News::class);
}
}
“`
### 4. Controllers
// NewsController.php
“`php
class NewsController extends Controller
{
public function index() {
$news = News::latest()->paginate(10);
return view(‘news.index’, compact(‘news’));
}
public function show($slug) {
$article = News::where(‘slug’, $slug)->firstOrFail();
return view(‘news.show’, compact(‘article’));
}
}
“`
### 5. Routes
// web.php
“`php
use App\Http\Controllers\NewsController;
Route::get(‘/’, [NewsController::class, ‘index’])->name(‘home’);
Route::get(‘/news/{slug}’, [NewsController::class, ‘show’])->name(‘news.show’);
“`
### 6. Blade Templates
// layouts/app.blade.php
“`blade
“`
// home.blade.php
“`blade
@extends(‘layouts.app’)
@section(‘content’)
Latest News
@foreach($news as $article)