我是Laravel技术的新手。我正在做一个简单的项目,管理员注册他有停车区空间,用户可以预订停车区空间。我面临的问题是用户可以在任何日期预订停车位。例如,有10个停车位。如果下周有5位用户预订,明天有2位用户预订。明天它应该显示8,然后后天它将重置为其原始值10和下周如果5个用户预订剩余空间应该是5.如何在Laravel中执行此操作。
我有以下代码。
USER_TABLE:
public function up()
{
Schema::create('user_table', function (Blueprint $table) {
$table->increments('id');
$table->string('userId');
$table->string('userName');
$table->string('userEmailId');
$table->string('area');
$table->integer('areaId');
$table->string('date');
$table->string('phNo');
$table->integer('spacecount');
//$table->timestamps();
});
}
public function down()
{
Schema::drop('user_table');
}
Vendor_table:
public function up()
{
Schema::create('vendor_table', function (Blueprint $table) {
$table->increments('id');
$table->string('vendorName');
$table->string('vendorArea');
$table->string('areaId');
$table->string('vendorEmailId');
$table->integer('vendorAvailableSpace');
$table->string('phNo');
$table->string('adress');
// $table->timestamps();
});
}
public function down()
{
Schema::drop('vendor_table');
}
用户模型:
protected $table = 'user_table';
public $timestamps = false;
供应商模型:
protected $table = 'vendor_table';
public $timestamps = false;
供应商控制器:
public function add(Request $request)
{
$generateUniqueId = new GenerateUUID();
$vendorId = $generateUniqueId->getUniqueId();
$generateUniqueId = new GenerateUUID();
$areaId = $generateUniqueId->getUniqueId();
$vendor = new Vendor();
$vendor->vendorId = $vendorId;
$vendor->vendorName = $request->input('vendorName');
$vendor->vendorArea = $request->input('vendorArea');
$vendor->areaId = $areaId;
$vendor->vendorEmailId = $request->input('vendorEmailId');
$vendor->vendorAvailableSpace = $request->input('vendorAvailableSpace');
$vendor->phNo = $request->input('phNo');
$vendor->address = $request->input('address');
$vendor->save();
if (!$vendor->save())
{
return 3001;
}
else
{
return 3000;
}
用户控制器:
public function book(Request $request)
{
$checkCount = Vendor::where('vendorAvailableSpace',$request->input('vendorAvailableSpace'))->first();
Log::info($checkCount);
if(count($checkCount) > 0) {
$generateUniqueId = new GenerateUUID();
$userId = $generateUniqueId->getUniqueId();
$user = new UserModel();
$user->userId = $userId;
$user->userName = $request->input('userName');
$user->userEmailId = $request->input('userEmailId');
$user->area = $request->input('area');
$user->areaId = $request->input('areaId');
$user->date = $request->input('date');
$user->phNo = $request->input('phNo');
$user->save();
if (!$user->save())
{
return 4001;
}
else
{
Vendor::where('vendor_table.areaId', $request->input('areaId'))
->join('user_table','user_table.areaId','=','vendor_table.areaId')
->where('user_table.date',$request->input('date'))->decrement('vendor_table.vendorAvailableSpace');
return 4000;
}
}
else
{
return 4002;
}
答案 0 :(得分:1)
这里需要一个单独的表来管理每天的计数。
<强>移植强>
public function up()
{
Schema::create('whateverNameYouLikeTo', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('vendor_id');
$table->integer('sunday');
$table->integer('monday');
$table->integer('tuesday');
$table->integer('wednesday');
$table->integer('thursday');
$table->integer('friday');
$table->integer('saturday');
$table->timestamps();
});
Schema::table('vendorTable', function (Blueprint $table) {
//Foreign Keys
$table->foreign('vendor_id')->references('id')->on('vendorTable')->onDelete('cascade')->onUpdate('cascade');
});
}
首先,您需要在注册时在此表中设置这些值,就像您在与外键聊天中提到的那样,您将在注册时获得vendor_id。
然后,每当你减少一个值,你需要更新值,在这里你需要检查一天是否有星期日你需要修改星期日数据,你可以使用这样的急切加载
对于日期操作,我建议您使用Carbon阅读此处的文档以获得更好的洞察力,然后您可以使用以下碳:
if($today == sunday) {
$vendorAvailableSpace->sunday = $value;
}