<?php

namespace App\Http\Controllers;
use App\Models\Student;
use Illuminate\Http\Request;

class StudentHelloworldController extends Controller
{
  /**
   * Display a listing of the resource.
   *
   * @return \Illuminate\Http\Response
   */
  public function index()
  {
    $students = Student::orderBy('id', 'desc')->paginate(10);
    return view('students_helloworld.index', compact('students'));
  }

  /**
   * Show the form for creating a new resource.
   *
   * @return \Illuminate\Http\Response
   */
  public function create()
  {
    return view('students_helloworld.create');
  }

  /**
   * Store a newly created resource in storage.
   *
   * @param  \Illuminate\Http\Request  $request
   * @return \Illuminate\Http\Response
   */
  public function store(Request $request)
  {
    $request->validate([
      'name' => 'required',
      'email' => 'required',
      'address' => 'required',
    ]);

    Student::create($request->post());

    return redirect()
      ->route('students_helloworld.index')
      ->with('success', 'Student has been created successfully.');
  }

  /**
   * Display the specified resource.
   *
   * @param  \App\Student  $Student
   * @return \Illuminate\Http\Response
   */
  public function show(Student $Student)
  {
    return view('students_helloworld.show', compact('Student'));
  }

  /**
   * Show the form for editing the specified resource.
   *
   * @param  \App\Student  $Student
   * @return \Illuminate\Http\Response
   */
  public function edit(Student $Student)
  {
    return view('students_helloworld.edit', compact('Student'));
  }

  /**
   * Update the specified resource in storage.
   *
   * @param  \Illuminate\Http\Request  $request
   * @param  \App\Student  $Student
   * @return \Illuminate\Http\Response
   */
  public function update(Request $request, Student $Student)
  {
    $request->validate([
      'name' => 'required',
      'email' => 'required',
      'address' => 'required',
    ]);

    $Student->fill($request->post())->save();

    return redirect()
      ->route('students_helloworld.index')
      ->with('success', 'Student Has Been updated successfully');
  }

  /**
   * Remove the specified resource from storage.
   *
   * @param  \App\Student  $Student
   * @return \Illuminate\Http\Response
   */
  public function destroy(Student $Student)
  {
    $Student->delete();
    return redirect()
      ->route('students_helloworld.index')
      ->with('success', 'Student has been deleted successfully');
  }
}
