Eloquent One Of Many Polymorphic Tutorial in Laravel 11
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class CommentController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}
Above File is app\Http\Controllers\CommentController FileBelow File is app\Http\Controllers\PostController File
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$post = Post::create([
"title" => "News Title Six",
"description" => "vel illum qui dolorem eum fugiat quo voluptas nulla pariatur"
]);
$post->comments()->create([
"detail" => "This is post six comment",
"likes" => 3
]);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}
Below File is app\Http\Controllers\VideoController File
<?php
namespace App\Http\Controllers;
use App\Models\Video;
use Illuminate\Http\Request;
class VideoController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
// $video = Video::find(1);
// return $video->comments;
$video = Video::with("comments")->find(1);
return $video;
// $video = Video::find(1);
// foreach($video->comments as $data) {
// echo $data->detail;
// echo "<hr>";
// }
// $videos = Video::with("latestComment")->find(1);
// return $videos;
// $videos = Video::with("oldestComment")->find(1);
// return $videos;
// $videos = Video::with("bestComment")->find(1);
// return $videos;
// $videos = Video::with("leastComment")->find(1);
// return $videos;
// $videos = Video::find(1);
// return $videos->leastComment;
// $videos = Video::find(1);
// return $videos->bestComment;
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$videos = Video::find(1);
$videos->comments()->create([
"detail" => "Wonderful Video",
"likes" => 5
]);
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
//
}
}
Below File is app\Models\Comment File
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public $timestamps = false;
protected $table = "comments";
protected $fillable = ["detail", "likes", "commentable_id", "commentable_type"];
public function commentable()
{
return $this->morphTo();
}
}
Below File is app\Models\Post File
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public $timestamps = true;
protected $table = "posts";
protected $fillable = ["title", "description"];
public function comments()
{
return $this->morphMany(Comment::class, "commentable");
}
}
Below File is app\Models\Video File
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Video extends Model
{
public $timestamps = true;
protected $table = "videos";
protected $fillable = ["title", "url"];
public function comments()
{
return $this->morphMany(Comment::class, "commentable");
}
public function latestComment()
{
return $this->morphOne(Comment::class, "commentable")->latestOfMany();
}
public function oldestComment()
{
return $this->morphOne(Comment::class, "commentable")->oldestOfMany();
}
public function bestComment()
{
return $this->morphOne(Comment::class, "commentable")->ofMany("likes", "max");
}
public function leastComment()
{
return $this->morphOne(Comment::class, "commentable")->ofMany("likes", "min");
}
}
Below File is database\json\videos File
[
{
"title": "Video Title One",
"url": "videos/first.mp4"
},
{
"title": "Video Title Two",
"url": "videos/second.mp4"
},
{
"title": "Video Title Three",
"url": "videos/third.mp4"
}
]
Below File is database\migrations\2024_12_16_114203_create_posts_table File
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string("title");
$table->longText("description");
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('posts');
}
};
Below File is database\migrations\2024_12_16_114354_create_videos_table File
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('videos', function (Blueprint $table) {
$table->id();
$table->string("title");
$table->string("url");
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('videos');
}
};
Below File is database\migrations\2024_12_16_114405_create_comments_table File
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->string("detail");
$table->unsignedBigInteger("likes");
$table->unsignedBigInteger("commentable_id");
$table->string("commentable_type");
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('comments');
}
};
Below File is database\seeders\DatabaseSeeder File
<?php
namespace Database\Seeders;
use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
// User::factory(10)->create();
// User::factory()->create([
// 'name' => 'Test User',
// 'email' => 'test@example.com',
// ]);
$this->call([
VideoSeeder::class
]);
}
}
Below File is database\seeders\VideoSeeder File
<?php
namespace Database\Seeders;
use App\Models\Video;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\File;
class VideoSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$json = File::get(path: "database/json/videos.json");
$videos = collect(json_decode($json));
$videos->each(function ($video) {
Video::create([
"title" => $video->title,
"url" => $video->url
]);
});
}
}
Below File is routes\web File
<?php
use App\Http\Controllers\CommentController;
use App\Http\Controllers\PostController;
use App\Http\Controllers\VideoController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::resource("video", VideoController::class);
Route::resource("post", PostController::class);
Route::resource("comment", CommentController::class);
.png)
.png)
.png)
Comments
Post a Comment