mercredi 25 novembre 2020

laravel 5.6 search keep send me 404 not found page

hi im sorry if I do this wrong but my search function always send me to 404 not found despite having the necessary routes...

here the code:

Web.php

Route::get('/pegawai')->name('pegawai.index')->uses('pegawaicontroller@index');
Route::get('/pegawai/search')->uses('pegawaicontroller@search');

pegawaicontroller.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Pegawai;


class pegawaicontroller extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $pegawais = Pegawai::orderBy('id', 'ASC')->paginate(5);
        return view('pegawai.index', compact('pegawais')); 
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'kategori' => 'required',
            'jabatan' => 'required',
            'nama_pegawai' => 'required',
            'alamat' => 'required',
            'gambar_pegawai' => 'required'
            ]);
            
            // upload file
            $filename = time() . '.png';
            $request->file('gambar_pegawai')->storeAs('public/images', $filename);

            $pegawai = new Pegawai;
            $pegawai->kategori = $request->input('kategori');
            $pegawai->jabatan = $request->input('jabatan');
            $pegawai->nama_pegawai = $request->input('nama_pegawai');
            $pegawai->alamat = $request->input('alamat');
            $pegawai->gambar_pegawai = $filename;
            $pegawai->save();
            // $pegawai = Pegawai::create($request->all()); 
            return redirect()->route('pegawai.index')->with('message','Data berhasil dibuat!'); 
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $pegawai = Pegawai::findOrFail($id);
        return view('pegawai.show', compact('pegawai'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $pegawai = Pegawai::findOrFail($id);
        return view('pegawai.edit', compact('pegawai')); 
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'kategori' => 'required',
            'jabatan' => 'required',
            'nama_pegawai' => 'required',
            'alamat' => 'required',
            'gambar_pegawai' => 'required'
            ]);
            
            // script delete tapii gagal
            // if ($request->produk()->gambar_pegawai){
            //     Storage::delete('images/' . $request->produk()->gambar_pegawai);
            // }

            // update file
            $filename = time() . '.png';
            $request->file('gambar_pegawai')->storeAs('public/images', $filename);

            $pegawai = Pegawai::find($id);
            $pegawai->kategori = $request->input('kategori');
            $pegawai->jabatan = $request->input('jabatan');
            $pegawai->nama_pegawai = $request->input('nama_pegawai');
            $pegawai->alamat = $request->input('alamat');
            $pegawai->gambar_pegawai = $filename;
            $pegawai->save();

            // $pegawai = Pegawai::findOrFail($id)->update($request->all());
            return redirect()->route('pegawai.index')->with('message', 'Data berhasil diubah!'); 
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $pegawai = Pegawai::findOrFail($id)->delete();
        return redirect()->route('pegawai.index')->with('message', 'Data berhasil dihapus!');
    }

public function search(Request $request)
{
    // menangkap data pencarian
    $search = $request->search;

    // mengambil data dari table pegawai sesuai pencarian data
    $pegawai = DB::table('pegawais')
    ->where('nama_pegawai','LIKE',"%".$search."%")
    ->paginate();

        // mengirim data pegawai ke view index
    return view('pegawai.index',['pegawais' => $pegawai]);

    }
}

Index.php

<form action="/pegawai/search" method="GET">
    <input type="text" name="search" placeholder="Cari Pegawai .." value="">
    <input type="submit" value="Search">
</form>

P.S : im sorry if I post it wrong since this is my first post... and sorry for my terrible english

any help is truly appreciated.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3laRRpY
via IFTTT

Aucun commentaire:

Enregistrer un commentaire