Skip to content

Commit

Permalink
Foreign Key Set up Done.
Browse files Browse the repository at this point in the history
  • Loading branch information
subhadipghorui committed Dec 30, 2020
1 parent 2da930a commit 654420d
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 32 deletions.
1 change: 0 additions & 1 deletion app/Http/Controllers/Admin/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public function destroy($id)
{
$comment = Comment::findOrFail($id);
// Delete all replies
$replies = CommentReply::where('comment_id', $id)->delete();
$comment->delete();
Toastr::success('Comment successfully deleted :)');
return redirect()->back();
Expand Down
9 changes: 8 additions & 1 deletion app/Http/Controllers/Admin/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace App\Http\Controllers\Admin;

use App\Category;
use App\Comment;
use App\Http\Controllers\Controller;
use App\Post;
use App\User;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;
Expand All @@ -16,7 +19,11 @@ class DashboardController extends Controller
{
public function index()
{
return view('admin.index');
$posts = Post::all();
$comments = Comment::latest()->get();
$categories = Category::all();
$users = User::all();
return view('admin.index', compact('posts', 'comments', 'users', 'categories'));
}
public function showProfile()
{
Expand Down
5 changes: 3 additions & 2 deletions app/Http/Controllers/Admin/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,12 @@ public function destroy($id)
{
$post = Post::findOrfail($id);
// delete img if exists
if (Storage::disk('public')->exists('post/' . $post->image)) {
// delete 1st condition on Production
if ($post->image !== 'laravel-wiki-5f92a8e71c7bc1603447015.jpg' && Storage::disk('public')->exists('post/' . $post->image)) {
Storage::disk('public')->delete('post/' . $post->image);
}
// Delete Tags
$post->tags()->delete();
// $post->tags()->delete();
$post->delete();
Toastr::success('Post Successfully Deleted :)', 'success');

Expand Down
20 changes: 20 additions & 0 deletions database/factories/PostFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Post;
use Faker\Generator as Faker;
use Illuminate\Support\Str;

$factory->define(Post::class, function (Faker $faker) {
return [
'user_id' => 1,
'category_id'=> random_int(1,8),
'title' => $faker->sentence($nbWords = 10, $variableNbWords = true),
'slug' => Str::slug($faker->sentence($nbWords = 10, $variableNbWords = true)),
'image' => 'laravel-wiki-5f92a8e71c7bc1603447015.jpg',
'body' => $faker->paragraph($nbSentences = 20, $variableNbSentences = true),
'view_count' => random_int(10,100),
'status' => 1
];
});
7 changes: 6 additions & 1 deletion database/migrations/2020_09_12_072748_create_tags_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->id();
$table->string('postID');
$table->unsignedBigInteger('postID');
$table->string('name');


// Delete all tags on delete posts
$table->foreign('postID')->references('id')->on('posts')->onDelete('cascade');

});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public function up()
$table->unsignedBigInteger('user_id');
$table->text('message');
$table->timestamps();

// Delete all comments on delete posts
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
// Delete all comments on delete users
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public function up()
$table->unsignedBigInteger('user_id');
$table->text('message');
$table->timestamps();

// Delete all replies on delete comments
$table->foreign('comment_id')->references('id')->on('comments')->onDelete('cascade');
// Delete all comments on delete users
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function up()
$table->unsignedBigInteger('post_id');
$table->unsignedBigInteger('user_id');
$table->timestamps();

// Delete all favorite posts on delete posts
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
// Delete all comments on delete users
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}

Expand Down
64 changes: 64 additions & 0 deletions database/seeds/CategoriesTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

use App\Category;
use Illuminate\Database\Seeder;

class CategoriesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Category::create([
'name' => 'HTML',
'slug' => 'html',
'description' => 'html',
'image' => 'html.JPG',
]);
Category::create([
'name' => 'CSS',
'slug' => 'css',
'description' => 'css',
'image' => 'css.JPG',
]);
Category::create([
'name' => 'Javascript',
'slug' => 'javascript',
'description' => 'javascript',
'image' => 'javascript.JPG',
]);
Category::create([
'name' => 'GIS and Remote Sensing',
'slug' => 'gis',
'description' => 'gis',
'image' => 'gis.JPG',
]);
Category::create([
'name' => 'Data Base',
'slug' => 'mysql',
'description' => 'mysql',
'image' => 'mysql.JPG',
]);
Category::create([
'name' => 'Arduino',
'slug' => 'arduino',
'description' => 'arduino',
'image' => 'arduino.JPG',
]);
Category::create([
'name' => 'Web Mapping',
'slug' => 'webmapping',
'description' => 'webmapping',
'image' => 'webmapping.JPG',
]);
Category::create([
'name' => 'Web Designe',
'slug' => 'webdesigne',
'description' => 'webdesigne',
'image' => 'webdesigne.JPG',
]);
}
}
2 changes: 2 additions & 0 deletions database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public function run()
{
$this->call(UsersTableSeeder::class);
$this->call(RolesTableSeeder::class);
$this->call(CategoriesTableSeeder::class);
$this->call(PostsTableSeeder::class);
}
}
17 changes: 17 additions & 0 deletions database/seeds/PostsTableSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use App\Post;
use Illuminate\Database\Seeder;

class PostsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$posts = factory(App\Post::class, 20)->create();
}
}
4 changes: 2 additions & 2 deletions database/seeds/UsersTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public function run()
'userid' => 'admin101',
'role_id' => 1,
'name' => 'Admin',
'email' => 'admin@admin.com',
'email' => 'admin@example.com',
'password' => bcrypt('admin'),
]);
$user = User::create([
'userid' => 'user101',
'role_id' => 2,
'name' => 'User',
'email' => 'user@user.com',
'email' => 'user@example.com',
'password' => bcrypt('user'),
]);
}
Expand Down
40 changes: 15 additions & 25 deletions resources/views/admin/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="col-sm-4">
<div class="page-header float-left">
<div class="page-title">
<h1>Dashboard</h1>
<h1> Admin Dashboard</h1>
</div>
</div>
</div>
Expand Down Expand Up @@ -52,7 +52,7 @@ class="ti-file text-success border-success"
</div>
<div class="stat-content dib">
<div class="stat-text">Posts</div>
<div class="stat-digit">1,012</div>
<div class="stat-digit">{{$posts->count()}}</div>
</div>
</div>
</div>
Expand All @@ -70,7 +70,7 @@ class="ti-user text-primary border-primary"
</div>
<div class="stat-content dib">
<div class="stat-text">Users</div>
<div class="stat-digit">961</div>
<div class="stat-digit">{{$users->count()}}</div>
</div>
</div>
</div>
Expand All @@ -88,7 +88,7 @@ class="ti-comment-alt text-warning border-warning"
</div>
<div class="stat-content dib">
<div class="stat-text">Comments</div>
<div class="stat-digit">770</div>
<div class="stat-digit">{{$comments->count()}}</div>
</div>
</div>
</div>
Expand All @@ -105,8 +105,8 @@ class="ti-thumb-up text-warning border-warning"
></i>
</div>
<div class="stat-content dib">
<div class="stat-text">Likes</div>
<div class="stat-digit">7670</div>
<div class="stat-text">Categories</div>
<div class="stat-digit">{{$categories->count()}}</div>
</div>
</div>
</div>
Expand All @@ -122,30 +122,20 @@ class="ti-thumb-up text-warning border-warning"
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
<th scope="col">Comment</th>
<th scope="col">From</th>
<th scope="col">Post</th>
</tr>
</thead>
<tbody>
@foreach ($comments->take(10) as $key => $comment)
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
<th scope="row">{{$key+1}}</th>
<td>{{Str::limit($comment->message, 30)}}</td>
<td>{{$comment->user->name}}</td>
<td><a href="{{route('post', $comment->post->slug)}}">{{Str::limit($comment->post->title, 30)}}</a></td>
</tr>
@endforeach
</tbody>
</table>
</div>
Expand Down

0 comments on commit 654420d

Please sign in to comment.