Laravel: how to filter data by category, for example the user click the information technology category, it should display the information technology related datas on the same page. Please include the migration files:
this is my code:
PostController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
use Auth;
class PostController extends Controller
{
/**
* The original code that works
*/
public function publicHome() {
$posts = Post:aginate(3);
return view('tukma/home', [
'posts' => $posts
]);
}
public function index()
{
$userId = Auth::id();
$posts = Post::all()->where('user_id', $userId);
return view('admin/home' , [
'posts' => $posts
]);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('admin/create');
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validatedData = $request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
'company' => 'required|string',
'title' => 'required|string',
'body' => 'required|string',
'number' => 'required|numeric',
'email' => 'required|email'
]);
$image = $request->file('image');
$post = new Post();
$post->user_id = Auth::id();
$post->company = $validatedData['company'];
$post->title = $validatedData['title'];
$post->body = $validatedData['body'];
$post->number = $validatedData['number'];
$post->email = $validatedData['email'];
if ($request->hasFile('image')) {
$imageName = time() . '.' . $image->extension();
$image->move(public_path('images'), $imageName);
$post->image = 'images/' . $imageName;
}
$post->save();
return redirect()->route('admin.home')->with('success', 'Post has been created');
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
$post = Post::findOrFail($id);
return view('admin.edit', compact('post'));
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
$validatedData = $request->validate([
'image' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
'company' => 'required|string',
'title' => 'required|string',
'body' => 'required|string',
'number' => 'required|numeric',
'email' => 'required|email'
]);
$post = Post::findOrFail($id);
$post->company = $validatedData['company'];
$post->title = $validatedData['title'];
$post->body = $validatedData['body'];
$post->number = $validatedData['number'];
$post->email = $validatedData['email'];
if ($request->hasFile('image')) {
$image = $request->file('image');
$imageName = time() . '.' . $image->extension();
$image->move(public_path('images'), $imageName);
$post->image = 'images/' . $imageName;
}
$post->save();
return redirect()->route('admin.home')->with('success', 'Post has been updated');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
$post = Post::findOrFail($id);
$post->delete();
return redirect()->route('admin.home')->with('success', 'Successfully Deleted Post');
}
}
home.blade.php
@extends('layouts.app')
@section('title', 'Tukma | Professional Community')
@section('content')
<link
rel="stylesheet"
href="You do not have permission to view the full content of this post. Log in or register now."
integrity="sha512-..."
crossorigin="anonymous"
/>
<link rel="stylesheet" href="{{ asset('css/style.css') }}">
<body>
<h2 class="">Latest Job Hiring</h2>
<!-- Navbar -->
<nav class="navbar navbar-expand-lg fixed-top bg-light navbar-light">
<div class="container">
<a class="navbar-brand" href="{{ url('/') }}">
Tukma
</a>
<button
class="navbar-toggler"
type="button"
data-mdb-toggle="collapse"
data-mdb-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-label="Toggle navigation"
<i class="fas fa-bars"></i>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ms-auto">
<!-- Authentication Links -->
GuEsT
@if (Route::has('login'))
<li class="nav-item">
<a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
</li>
@endif
@if (Route::has('register'))
<li class="nav-item">
<a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
</li>
@endif
else
<li class="nav-item dropdown">
<a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
{{ Auth::user()->name }}
</a>
<div class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
{{ __('Logout') }}
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">
@csrf
</form>
</div>
</li>
@endguest
</ul>
</div>
</div>
</nav>
<!-- Navbar -->
<div class="container">
<div class="row">
<div class="col-2 custom-col1">
<div class="container-fluid">
<div class="row">
<div class="col-sm-auto position-fixed" style="left: 0;">
<div class="d-flex flex-sm-column flex-row flex-nowrap bg-light align-items-center m-4">
<ul class="nav nav-***** nav-flush flex-sm-column flex-row flex-nowrap mb-auto mx-auto text-center align-items-center">
<li class="nav-item">
<div class="input-group rounded">
<input type="search" class="form-control rounded" placeholder="find jobs" aria-label="Search" aria-describedby="search-addon" />
<span class="input-group-text border-0" id="search-addon">
<button class="btn btn-outline-secondary" type="button" id="search-button"><i class="fas fa-search"></i></button>
</span>
</div>
</li>
<li>
<a href="#" class="nav-link py-3 px-2" title="" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-original-title="Home">
Home
</a>
</li>
<li>
<a href="#" class="nav-link py-3 px-2" title="" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-original-title="JOBS">
Blog | Job Discussion
</a>
</li>
<li>
<a href="#" class="nav-link py-3 px-2" title="" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-original-title="JOBS">
About Us
</a>
</li>
<li>
<a href="#" class="nav-link py-3 px-2" title="" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-original-title="JOBS">
Contact Us
</a>
</li>
</ul>
</div>
</div>
<div class="col-sm p-3 min-vh-100">
<!-- content -->
</div>
</div>
</div>
</div>
<div class="col-6 custom-col">
<div class="card">
@foreach($posts as $post)
<div class="card-body">
<img class="rounded-circle shadow-4-strong" src="{{ asset($post->image) }}">
<div class="d-flex flex-row">
<div class="p-2">
<h4 class="card-title">{{ $post->company }}</h4>
<p class="card-text">{{ $post->title }}</p>
<p class="card-text">{{ $post->body }}</p>
<p class="card-text">{{ $post->number }}</p>
<p class="card-text">{{ $post->email }}</p>
</div>
</div>
</div>
@endforeach
<div class="pagination-container text-center">
{{ $posts->links('pagination::simple-bootstrap-4') }}
</div>
</div>
</div>
<div class="col-4 position-fixed custom-col" style="right: 0;">
<ul class="list-group list-group-flush">
<li class="list-group-item fw-bold">Job Categories</li>
<li class="list-group-item"><a href="">Information Technology</a></li>
<li class="list-group-item"><a href="">Accounting</a></li>
<li class="list-group-item"><a href="">Education</a></li>
<li class="list-group-item"><a href="">Food Processing</a></li>
</ul>
</div>
</div>
</div>
<script
src="You do not have permission to view the full content of this post. Log in or register now."
integrity="sha512-..."
crossorigin="anonymous"
@endsection
Post table migration
<?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->increments('id');
$table->unsignedBigInteger('user_id');
$table->string('image')->nullable();
$table->string('company');
$table->string('title');
$table->text('body');
$table->bigInteger('number');
$table->string('email');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('posts');
}
};
this is my code:
PostController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
use Auth;
class PostController extends Controller
{
/**
* The original code that works
*/
public function publicHome() {
$posts = Post:aginate(3);
return view('tukma/home', [
'posts' => $posts
]);
}
public function index()
{
$userId = Auth::id();
$posts = Post::all()->where('user_id', $userId);
return view('admin/home' , [
'posts' => $posts
]);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('admin/create');
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validatedData = $request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
'company' => 'required|string',
'title' => 'required|string',
'body' => 'required|string',
'number' => 'required|numeric',
'email' => 'required|email'
]);
$image = $request->file('image');
$post = new Post();
$post->user_id = Auth::id();
$post->company = $validatedData['company'];
$post->title = $validatedData['title'];
$post->body = $validatedData['body'];
$post->number = $validatedData['number'];
$post->email = $validatedData['email'];
if ($request->hasFile('image')) {
$imageName = time() . '.' . $image->extension();
$image->move(public_path('images'), $imageName);
$post->image = 'images/' . $imageName;
}
$post->save();
return redirect()->route('admin.home')->with('success', 'Post has been created');
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
$post = Post::findOrFail($id);
return view('admin.edit', compact('post'));
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
$validatedData = $request->validate([
'image' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
'company' => 'required|string',
'title' => 'required|string',
'body' => 'required|string',
'number' => 'required|numeric',
'email' => 'required|email'
]);
$post = Post::findOrFail($id);
$post->company = $validatedData['company'];
$post->title = $validatedData['title'];
$post->body = $validatedData['body'];
$post->number = $validatedData['number'];
$post->email = $validatedData['email'];
if ($request->hasFile('image')) {
$image = $request->file('image');
$imageName = time() . '.' . $image->extension();
$image->move(public_path('images'), $imageName);
$post->image = 'images/' . $imageName;
}
$post->save();
return redirect()->route('admin.home')->with('success', 'Post has been updated');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
$post = Post::findOrFail($id);
$post->delete();
return redirect()->route('admin.home')->with('success', 'Successfully Deleted Post');
}
}
home.blade.php
@extends('layouts.app')
@section('title', 'Tukma | Professional Community')
@section('content')
<link
rel="stylesheet"
href="You do not have permission to view the full content of this post. Log in or register now."
integrity="sha512-..."
crossorigin="anonymous"
/>
<link rel="stylesheet" href="{{ asset('css/style.css') }}">
<body>
<h2 class="">Latest Job Hiring</h2>
<!-- Navbar -->
<nav class="navbar navbar-expand-lg fixed-top bg-light navbar-light">
<div class="container">
<a class="navbar-brand" href="{{ url('/') }}">
Tukma
</a>
<button
class="navbar-toggler"
type="button"
data-mdb-toggle="collapse"
data-mdb-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent"
aria-expanded="false"
aria-label="Toggle navigation"
<i class="fas fa-bars"></i>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ms-auto">
<!-- Authentication Links -->
GuEsT
@if (Route::has('login'))
<li class="nav-item">
<a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
</li>
@endif
@if (Route::has('register'))
<li class="nav-item">
<a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
</li>
@endif
else
<li class="nav-item dropdown">
<a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
{{ Auth::user()->name }}
</a>
<div class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
{{ __('Logout') }}
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">
@csrf
</form>
</div>
</li>
@endguest
</ul>
</div>
</div>
</nav>
<!-- Navbar -->
<div class="container">
<div class="row">
<div class="col-2 custom-col1">
<div class="container-fluid">
<div class="row">
<div class="col-sm-auto position-fixed" style="left: 0;">
<div class="d-flex flex-sm-column flex-row flex-nowrap bg-light align-items-center m-4">
<ul class="nav nav-***** nav-flush flex-sm-column flex-row flex-nowrap mb-auto mx-auto text-center align-items-center">
<li class="nav-item">
<div class="input-group rounded">
<input type="search" class="form-control rounded" placeholder="find jobs" aria-label="Search" aria-describedby="search-addon" />
<span class="input-group-text border-0" id="search-addon">
<button class="btn btn-outline-secondary" type="button" id="search-button"><i class="fas fa-search"></i></button>
</span>
</div>
</li>
<li>
<a href="#" class="nav-link py-3 px-2" title="" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-original-title="Home">
Home
</a>
</li>
<li>
<a href="#" class="nav-link py-3 px-2" title="" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-original-title="JOBS">
Blog | Job Discussion
</a>
</li>
<li>
<a href="#" class="nav-link py-3 px-2" title="" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-original-title="JOBS">
About Us
</a>
</li>
<li>
<a href="#" class="nav-link py-3 px-2" title="" data-bs-toggle="tooltip" data-bs-placement="right" data-bs-original-title="JOBS">
Contact Us
</a>
</li>
</ul>
</div>
</div>
<div class="col-sm p-3 min-vh-100">
<!-- content -->
</div>
</div>
</div>
</div>
<div class="col-6 custom-col">
<div class="card">
@foreach($posts as $post)
<div class="card-body">
<img class="rounded-circle shadow-4-strong" src="{{ asset($post->image) }}">
<div class="d-flex flex-row">
<div class="p-2">
<h4 class="card-title">{{ $post->company }}</h4>
<p class="card-text">{{ $post->title }}</p>
<p class="card-text">{{ $post->body }}</p>
<p class="card-text">{{ $post->number }}</p>
<p class="card-text">{{ $post->email }}</p>
</div>
</div>
</div>
@endforeach
<div class="pagination-container text-center">
{{ $posts->links('pagination::simple-bootstrap-4') }}
</div>
</div>
</div>
<div class="col-4 position-fixed custom-col" style="right: 0;">
<ul class="list-group list-group-flush">
<li class="list-group-item fw-bold">Job Categories</li>
<li class="list-group-item"><a href="">Information Technology</a></li>
<li class="list-group-item"><a href="">Accounting</a></li>
<li class="list-group-item"><a href="">Education</a></li>
<li class="list-group-item"><a href="">Food Processing</a></li>
</ul>
</div>
</div>
</div>
<script
src="You do not have permission to view the full content of this post. Log in or register now."
integrity="sha512-..."
crossorigin="anonymous"
</body></script>
@endsection
Post table migration
<?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->increments('id');
$table->unsignedBigInteger('user_id');
$table->string('image')->nullable();
$table->string('company');
$table->string('title');
$table->text('body');
$table->bigInteger('number');
$table->string('email');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('posts');
}
};