MIELICIUS / app / Http / Controllers / BahanController.php
BahanController.php
Raw
<?php

namespace App\Http\Controllers;

use App\Models\Bahan;
use Illuminate\Http\Request;

class BahanController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $bahans=Bahan::all();
        return view ('dashboards.bahan.index', [
            'bahans'=>$bahans,
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $code= 'MT'.rand(1111,9999);
        return view ('dashboards.bahan.create', [
            'code'=>$code,
        ]);
        // return view('dashboards.bahan.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'kodebahan' => 'required',
            'namabahan' => 'required',
            'hargabahan' => 'required',
            'satuanbahan' => 'required'
        ]);
    
        $nol = 0;
    
        Bahan::create(['kodebahan' => $request->kodebahan,
                        'namabahan' => $request->namabahan, 
                        'hargabahan' => $request->hargabahan,
                        'stockbahan' => $nol,
                        'satuanbahan' => $request->satuanbahan]);
     
        return redirect()->route('adminIndexBahan')
                        ->with('success','Data Telah Ditambahkan.');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Bahan  $Bahan
     * @return \Illuminate\Http\Response
     */
    public function show(Bahan $Bahan)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Bahan  $Bahan
     * @return \Illuminate\Http\Response
     */
    public function edit(Bahan $databahan)
    {
        return view ('dashboards.bahan.edit', ['databahan'=>$databahan]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Bahan  $Bahan
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Bahan $databahan)
    {
        $request->validate([
            'kodebahan' => 'required',
            'namabahan' => 'required',
            'hargabahan' => 'required',
            'satuanbahan' => 'required'
        ]);
    
        $databahan->update($request->all());
    
        return redirect()->route('adminIndexBahan')
                        ->with('success','Data Telah Diperbaharui.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Bahan  $Bahan
     * @return \Illuminate\Http\Response
     */
    public function destroy(Bahan $databahan)
    {

        $databahan->delete();
    
        return redirect()->route('adminIndexBahan')
                        ->with('success','Data Telah Dihapus.');
    }
}