如何为每个商店自动增加特定的数据库表值。 (Laravel 6)
Posted
技术标签:
【中文标题】如何为每个商店自动增加特定的数据库表值。 (Laravel 6)【英文标题】:How to auto increment a specific database table value for every store. (Laravel 6) 【发布时间】:2020-05-20 22:45:32 【问题描述】:如何为每个商店自动增加特定的数据库表值。
我正在制作一个排队系统,并且我有一个 DEPARTMENTS 数据库表,其中包含“name”、“letter”和“number”作为表。
我有一个 QUEUES 数据库表,其中 'name' 'department' 'letter' 'number' 作为表。 - 'department =departments->name'、'letter = department-> letter' 和 'number =departments -> number' 表来自 DEPARTMENTS 数据库的表值。
如何使Queues->每次存储队列时增加数字。
<form method="post" action=" route('queues.store') " autocomplete="off">
@csrf
<h6 class="heading-small text-muted mb-4"> __('Queue information') </h6>
<div class="pl-lg-4">
<div class="form-group $errors->has('department') ? ' has-danger' : '' text-center">
<div class="col">
<label class="form-control-label text-lg" for="input-department"> __('Department') </label>
</div>
<div class="col text-center" data-toggle="buttons">
<div class="row text-center">
@foreach ($departments as $department)
<div class=" btn-group-toggle col-sm-12 col-md-4 text-center mt-2">
<label class="btn btn-secondary btn-lg w-100" onclick="getdept($department)">
<input type="radio" name="department" value=" $department->name" sr-only required> $department->name
</label>
</div>
@endforeach
</div>
@if ($errors->has('department'))
<span class="invalid-feedback" role="alert">
<strong> $errors->first('department') </strong>
</span>
@endif
</div>
</div>
<div class="form-group $errors->has('name') ? ' has-danger' : '' ">
<label class="form-control-label" for="input-name"> __('Name') </label>
<input type="text" name="name" id="input-name" class="form-control form-control-alternative $errors->has('name') ? ' is-invalid' : '' " placeholder=" __('Name') " value=" old('name') " required autofocus>
@if ($errors->has('name'))
<span class="invalid-feedback" role="alert">
<strong> $errors->first('name') </strong>
</span>
@endif
</div>
<div class="form-group $errors->has('snumber') ? ' has-danger' : '' ">
<label class="form-control-label" for="input-snumber"> __('Student number') </label>
<input type="number" name="snumber" id="input-snumber" class="form-control form-control-alternative $errors->has('snumber') ? ' is-invalid' : '' " placeholder=" __('Student number') " value=" old('snumber') " required autofocus>
@if ($errors->has('snumber'))
<span class="invalid-feedback" role="alert">
<strong> $errors->first('snumber') </strong>
</span>
@endif
</div>
<div class="form-group $errors->has('email') ? ' has-danger' : '' ">
<label class="form-control-label" for="input-email"> __('Email') </label>
<input type="email" name="email" id="input-email" class="form-control form-control-alternative $errors->has('email') ? ' is-invalid' : '' " placeholder=" __('Email') " value=" old('email') " required>
@if ($errors->has('email'))
<span class="invalid-feedback" role="alert">
<strong> $errors->first('email') </strong>
</span>
@endif
</div>
<div class="form-group $errors->has('transaction') ? ' has-danger' : '' ">
<label class="form-control-label" for="input-transaction"> __('Transaction') </label>
<select class="form-control form-control-md" name="transaction" required>
<option hidden value="">Choose Transaction...</option>
<option >Transaction 1</option>
<option >Transaction 2</option>
<option>Transaction 3</option>
</select>
@if ($errors->has('transaction'))
<span class="invalid-feedback" role="alert">
<strong> $errors->first('transaction') test</strong>
</span>
@endif
</div>
<div class="form-group $errors->has('remarks') ? ' has-danger' : '' ">
<label class="form-control-label" for="input-name"> __('Notes / Remarks') </label>
<textarea type="textarea" rows="5" name="remarks" id="input-name" class="form-control form-control-alternative $errors->has('remarks') ? ' is-invalid' : '' " placeholder=" __('Notes / Remarks for the transaction...') " value=" old('remarks') " autofocus></textarea>
@if ($errors->has('remarks'))
<span class="invalid-feedback" role="alert">
<strong> $errors->first('remarks') </strong>
</span>
@endif
</div>
<input type="hidden" name="letter" id="letter" value="">
<input type="hidden" name="number" id="number" value="">
<div class="text-center">
<button type="submit" class="btn btn-success mt-4"> __('Save') </button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<div class="separator separator-bottom separator-skew zindex-100">
<svg x="0" y="0" viewBox="0 0 2560 100" preserveAspectRatio="none" version="1.1" xmlns="http://www.w3.org/2000/svg">
<polygon class="fill-light" points="2560 0 2560 100 0 100"></polygon>
</svg>
</div>
</div>
<div class="container mt--10 pb-5"></div>
<script>
function getdept(dept)
document.getElementById('letter').value = dept.letter;
document.getElementById('number').value = dept.number;
</script>
这是我的队列控制器
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Department;
use App\Queue;
class QueueController extends Controller
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
$departments=Department::all();
return view('queues.index',['departments' => $departments]);
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
$departments=Department::all();
return view('queues.create',['departments' => $departments]);
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
$validatedData = $request->validate([
'name' => 'required|max:255',
'snumber' => 'required|max:255',
'email' => 'required|max:255',
'department' => 'required|max:255',
'transaction' => 'required|max:255',
'letter' => 'required|max:255',
'number' => 'required|max:255',
'remarks' => 'nullable|max:255'
]);
$queue = new Queue([
'name' => $request->get('name'),
'snumber' => $request->get('snumber'),
'email' => $request->get('email'),
'department' => $request->get('department'),
'transaction' => $request->get('transaction'),
'letter' => $request->get('letter'),
'number' => $request->get('number'),
'remarks' => $request->get('remarks'),
'called' => 'no',
]);
$queue->save();
return redirect('/')->withStatus(__('Queue added successfully.'));
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
//
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
//
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
//
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
//
【问题讨论】:
【参考方案1】:试试这个。
public function store(Request $request)
$validatedData = $request->validate([
'name' => 'required|max:255',
'snumber' => 'required|max:255',
'email' => 'required|max:255',
'department' => 'required|max:255',
'transaction' => 'required|max:255',
'letter' => 'required|max:255',
'number' => 'required|max:255',
'remarks' => 'nullable|max:255'
]);
//get the last number in the database
$number = Queue::orderBy('number', 'DESC')->first();
$queue = new Queue([
'name' => $request->get('name'),
'snumber' => $request->get('snumber'),
'email' => $request->get('email'),
'department' => $request->get('department'),
'transaction' => $request->get('transaction'),
'letter' => $request->get('letter'),
'number' => $number + 1,
'remarks' => $request->get('remarks'),
'called' => 'no',
]);
$queue->save();
return redirect('/')->withStatus(__('Queue added successfully.'));
【讨论】:
我收到此错误 ErrorException 类 App\Queue 的对象无法转换为数字以上是关于如何为每个商店自动增加特定的数据库表值。 (Laravel 6)的主要内容,如果未能解决你的问题,请参考以下文章