?
<?php
namespace App\Http\Controllers;
use App\Models\Testimonial;
use Illuminate\Http\Request;
class TestimonialController extends Controller
{
public static function getTestimonialList(){
$testimonials = Testimonial::where('is_published', true)->latest()->paginate(2);
return view('testimonials.testimonial-page', compact('testimonials'));
}
/**
* Display a listing of the resource.
*/
public function index()
{
$testimonials = Testimonial::latest()->paginate(10);
return view('testimonials.index', compact('testimonials'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('testimonials.create');
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|string|max:255',
'content' => 'required|string',
'position' => 'nullable|string|max:255',
'company' => 'nullable|string|max:255',
'rating' => 'nullable|integer|min:1|max:5',
'is_published' => 'sometimes|boolean',
]);
// Handle file upload for avatar if you have one
// if ($request->hasFile('avatar')) {
// $validatedData['avatar_path'] = $request->file('avatar')->store('avatars', 'public');
// }
$validatedData['is_published'] = $request->has('is_published');
Testimonial::create($validatedData);
return redirect()->route('testimonials.index')->with('success', 'Testimonial created successfully.');
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Testimonial $testimonial)
{
return view('testimonials.edit', compact('testimonial'));
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Testimonial $testimonial)
{
$validatedData = $request->validate([
'name' => 'required|string|max:255',
'content' => 'required|string',
'position' => 'nullable|string|max:255',
'company' => 'nullable|string|max:255',
'rating' => 'nullable|integer|min:1|max:5',
'is_published' => 'sometimes|boolean',
]);
$validatedData['is_published'] = $request->has('is_published');
$testimonial->update($validatedData);
return redirect()->route('testimonials.index')->with('success', 'Testimonial updated successfully.');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Testimonial $testimonial)
{
$testimonial->delete();
return redirect()->route('testimonials.index')->with('success', 'Testimonial deleted successfully.');
}
}