如何从数据库中添加 table 我的 SQL in laravel 6/7
How to add table from database my SQL in laravel 6/7
我想问一下如何将 ProdukTable
的获取数据作为 p_nama
添加到 StockTable
中?
我改用 Laravel 6.2。我该怎么办?
Model : App\Produk.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
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 : App\Http\Controllers\ProdukController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Produk;
use RealRashid\SweetAlert\Facades\Alert;
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 : App\Stock.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Stock extends Model
{
public $table = 'stock';
protected $primaryKey = 'sid';
protected $fillable = [
'sid',
'mesin',
'sku',
'p_nama',
'stock_min',
'stock',
'status'
];
}
Controllers : App\Http\Controllers\StockController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Stock;
use App\Produk;
use RealRashid\SweetAlert\Facades\Alert;
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" />
<select class="form-control select2" name="p_name">
{{-- <!-- I want to add list from product table into here--> --}}
<option value="{{ $produk->id }}">{{ $produk>p_name }}</option>
{{-- <!-- I want to add list from product table into here--> --}}
</select>
</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> Kembali</a>
<button type="submit" class="btn btn-success btn-lg"><i class="zmdi zmdi-check-circle"></i> 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
感谢您的帮助。
假设您想在创建 and/or 编辑股票记录时提供 select 选项。您可以将 Produk
列表传递给控制器中的视图。
public function create()
{
return view('stock.create', ['produks' => \App\Produk::all()]);
}
public function edit($sid)
{
$data = Stock::find($sid);
$produks = \App\Produk::all();
return view('stock.edit', compact('data', 'produks'));
}
现在 $produks
将在各自的视图中可用,您可以填充 select 选项。
我想问一下如何将 ProdukTable
的获取数据作为 p_nama
添加到 StockTable
中?
我改用 Laravel 6.2。我该怎么办?
Model : App\Produk.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
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 : App\Http\Controllers\ProdukController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Produk;
use RealRashid\SweetAlert\Facades\Alert;
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 : App\Stock.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Stock extends Model
{
public $table = 'stock';
protected $primaryKey = 'sid';
protected $fillable = [
'sid',
'mesin',
'sku',
'p_nama',
'stock_min',
'stock',
'status'
];
}
Controllers : App\Http\Controllers\StockController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Stock;
use App\Produk;
use RealRashid\SweetAlert\Facades\Alert;
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" />
<select class="form-control select2" name="p_name">
{{-- <!-- I want to add list from product table into here--> --}}
<option value="{{ $produk->id }}">{{ $produk>p_name }}</option>
{{-- <!-- I want to add list from product table into here--> --}}
</select>
</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> Kembali</a>
<button type="submit" class="btn btn-success btn-lg"><i class="zmdi zmdi-check-circle"></i> 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
感谢您的帮助。
假设您想在创建 and/or 编辑股票记录时提供 select 选项。您可以将 Produk
列表传递给控制器中的视图。
public function create()
{
return view('stock.create', ['produks' => \App\Produk::all()]);
}
public function edit($sid)
{
$data = Stock::find($sid);
$produks = \App\Produk::all();
return view('stock.edit', compact('data', 'produks'));
}
现在 $produks
将在各自的视图中可用,您可以填充 select 选项。