How to add table from database my SQL in laravel 6/7

  laravel, laravel-6.2, php

i wanna ask how to add get data from ProdukTable as p_nama into StockTable ?
Im using Laravel 6.2 instead. what should i do ?

Model : AppProduk.php

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Produk extends Model
{
    public $table = 'produk';
    protected $primaryKey = 'pid';
    protected $fillable = [
        'pid',
        'sku',
        'p_nama',
        'p_stock_min',
        'p_stock',
        'p_harga_beli',
        'p_harga',
        'status'
    ];
}

Controllers : AppHttpControllersProdukController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppProduk;
use RealRashidSweetAlertFacadesAlert;

class ProdukController extends Controller
{
    public function index()
    {
        $datas = Produk::paginate(5); // 5 record per pages, silahkan disesuaikan
        return view('produk.list', compact('datas'));
    }

    public function create()
    {
        return view('produk.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'sku'           => 'required',
            'p_nama'        => 'required',
            'p_stock_min'   => 'required',
            'p_stock'       => 'required',
            'p_harga_beli'  => 'required',
            'p_harga'       => 'required',
            'status'        => 'required',
        ]);

        $data = new Produk([
            'sku'          => $request->get('sku'),
            'p_nama'       => $request->get('p_nama'),
            'p_stock_min'  => $request->get('p_stock_min'),
            'p_stock'      => $request->get('p_stock'),
            'p_harga_beli' => $request->get('p_harga_beli'),
            'p_harga'      => $request->get('p_harga'),
            'status'       => $request->get('status')
        ]);
        $data->save();
        Alert::success('Sukses', 'Produk berhasil ditambahkan!');
        return redirect('/produk');
    }

    public function show($pid)
    {
        $data = Produk::find($pid);
        return view('produk.view', compact('data'));
    }

    public function edit($pid)
    {
        $data = Produk::find($pid);
        return view('produk.edit', compact('data'));
    }

    public function update(Request $request, $pid)
    {
        $request->validate([
            'sku'           => 'required',
            'p_nama'        => 'required',
            'p_stock_min'   => 'required',
            'p_stock'       => 'required',
            'p_harga_beli'  => 'required',
            'p_harga'       => 'required',
            'status'        => 'required',
        ]);
        $data = Produk::find($pid);
        $data->sku          = $request->get('sku');
        $data->p_nama       = $request->get('p_nama');
        $data->p_stock_min  = $request->get('p_stock_min');
        $data->p_stock      = $request->get('p_stock');
        $data->p_harga_beli = $request->get('p_harga_beli');
        $data->p_harga      = $request->get('p_harga');
        $data->status       = $request->get('status');
        $data->save();
        Alert::success('Diubah', 'Data Produk telah diupdate!');
        return redirect('/produk');
    }

    public function delete($pid)
    {
        $data = Produk::find($pid);
        return view('produk.delete', compact('data'));
    }

    public function destroy($pid)
    {
        $data = Produk::find($pid);
        $data->delete();
        Alert::success('Dihapus!', 'Data ini sudah dihapus!');
        return redirect('/produk');
    }
}

Model : AppStock.php

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Stock extends Model
{
    public $table = 'stock';
    protected $primaryKey = 'sid';
    protected $fillable = [
        'sid',
        'mesin',
        'sku',
        'p_nama',
        'stock_min',
        'stock',
        'status'
    ];
}

Controllers : AppHttpControllersStockController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppStock;
use AppProduk;
use RealRashidSweetAlertFacadesAlert;

class StockController extends Controller
{
    public function index()
    {
        $datas = Stock::paginate(5); // 5 record per pages, silahkan disesuaikan
        return view('stock.list', compact('datas'));
    }

    public function create()
    {
        return view('stock.create');
    }

    public function store(Request $request)
    {
        $request->validate([
            'sid'           => 'required',
            'sku'           => 'required',
            'p_nama'        => 'required',
            'stock_min'     => 'required',
            'stock'         => 'required',
            'status'        => 'required',
        ]);

        $data = new Stock([
            'sid'           => $request->get('sid'),
            'sku'           => $request->get('sku'),
            'p_nama'        => $request->get('p_nama'),
            'stock_min'     => $request->get('stock_min'),
            'stock'         => $request->get('stock'),
            'status'        => $request->get('status')
        ]);
        $data->save();
        Alert::success('Sukses', 'Stock berhasil ditambahkan!');
        return redirect('/stock');
    }

    public function show($sid)
    {
        $data = Stock::find($sid);
        return view('stock.view', compact('data'));
    }

    public function edit($sid)
    {
        $data = Stock::find($sid);
        return view('stock.edit', compact('data'));
    }

    public function update(Request $request, $sid)
    {
        $request->validate([
            'sid'           => 'required',
            'mesin'         => 'required',
            'sku'           => 'required',
            'p_nama'        => 'required',
            'stock_min'     => 'required',
            'stock'         => 'required',
            'status'        => 'required',
        ]);
        $data = Stock::find($sid);
        $data->mesin        = $request->get('mesin');
        $data->sid          = $request->get('sid');
        $data->sku          = $request->get('sku');
        $data->p_nama       = $request->get('p_nama');
        $data->stock_min    = $request->get('stock_min');
        $data->stock        = $request->get('stock');
        $data->status       = $request->get('status');
        $data->save();
        Alert::success('Diubah', 'Data Stock telah diupdate!');
        return redirect('/stock');
    }

    public function delete($sid)
    {
        $data = Stock::find($sid);
        return view('stock.delete', compact('data'));
    }

    public function destroy($sid)
    {
        $data = Stock::find($sid);
        $data->delete();
        Alert::success('Dihapus!', 'Data ini sudah dihapus!');
        return redirect('/stock');
    }
}

AddStock.blade.php

@extends('layout.master')
@section('title', 'Tambah Stock')
@section('parentPageTitle', 'Stock')
@section('page-style')
<link rel="stylesheet" href="{{asset('assets/plugins/bootstrap-material-datetimepicker/css/bootstrap-material-datetimepicker.css')}}"/>
<link rel="stylesheet" href="{{asset('assets/plugins/bootstrap-select/css/bootstrap-select.css')}}"/>
@stop
@section('content')
@include('sweetalert::alert')

<div class="row clearfix">
    <div class="col-sm-8 offset-sm-2">
        <div class="card">
            <div class="body">
                @if ($errors->any())
                <div class="alert alert-danger">
                    <ul>
                        @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
                <br />
                @endif
                <form method="post" action="{{ route('stock.store') }}">
                    @csrf
                    <div class="row clearfix">
                        <div class="col-sm-3">
                            <div class="form-group">
                                <label for="sku">SKU:</label>
                                <input type="text" class="form-control text-uppercase" name="sku" placeholder="SKU Barang" />
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label for="p_nama">Nama Barang:</label>
                                <input type="text" class="form-control text-uppercase" name="p_nama" placeholder="Nama Barang" />
                            </div>
                        </div>
                        <div class="col-sm-3">
                            <div class="form-group">
                                <label for="status">Status:</label>
                                <select name="status" id="status" class="form-control select2">
                                    <option value="Aktif">Tersedia</option>
                                    <option value="Menipis" color="red">Menipis</option>
                                    <option value="Habis" color="red">Habis</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-sm-3">
                            <div class="form-group">
                                <label for="p_stock_min">Minimal Stock:</label>
                                <input type="number" class="form-control" name="p_stock_min" placeholder="1000" />
                            </div>
                        </div>
                        <div class="col-sm-3">
                            <div class="form-group">
                                <label for="p_stock">Stock Saat ini:</label>
                                <input type="number" class="form-control" name="p_stock" placeholder="1000" />
                            </div>
                        </div>
                        <div class="col-sm-3">
                            <div class="form-group">
                                <label for="p_harga_beli">Harga Beli:</label>
                                <input type="number" class="form-control" name="p_harga_beli" placeholder="Tulis Nominalnya saja" />
                            </div>
                        </div>
                        <div class="col-sm-3">
                            <div class="form-group">
                                <label for="p_harga">Harga Jual:</label>
                                <input type="number" class="form-control" name="p_harga" placeholder="Tulis Nominalnya saja" />
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <a href="{{ route('stock.index')}}" class="btn btn-default btn-lg"><i class="zmdi zmdi-undo"></i>&nbsp;Kembali</a>&nbsp;
                            <button type="submit" class="btn btn-success btn-lg"><i class="zmdi zmdi-check-circle"></i>&nbsp;Tambah</button>
                        </div>
                    </div>
                </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
@section('page-script')
<script src="{{asset('assets/plugins/momentjs/moment.js')}}"></script>
<script src="{{asset('assets/plugins/bootstrap-material-datetimepicker/js/bootstrap-material-datetimepicker.js')}}"></script>
<script src="{{asset('assets/js/pages/forms/basic-form-elements.js')}}"></script>
@stop

thankyou for any help.

Source: Laravel

Leave a Reply