不同视图中的不同日期将显示在laravel的数据库中

时间:2017-05-18 18:40:49

标签: laravel

我正在创建一个允许借出书籍的网站,我有3个选项用于1周,2周和4周的贷款,当用户点击一本书时,他们将被带到一个详细页面显示3个贷款选项,如果您点击1周贷款,您将获得一周贷款页面,您可以在此确认或取消贷款,页面看起来像这样

<?php
use Carbon\Carbon;
?>

@extends('layouts.app')


@section('content')
<div class="container">

        <div class="panel panel-default">
            <div class="panel-heading"><h1>1 Week Loan</h1></div>

            <div class="panel-body">
                <h4>You have attempted to loan out: </h4>
                <h4>{{$book->title}}</h4>
                <h4>By: {{$book->author}}</h4>
                <br/>
                <h4>To agree, press the confirm button, to cancel the loan press the cancel button and you will be returned to the home screen.</h4>
                <br/> 

                 <form action="{{url('loan')}}" method="POST">
                        {{ csrf_field() }}
                        <input type="hidden" name="l_userid" value="{{ Auth::user()->userid }}" id="l_userid">
                        <input type="hidden" name="l_f_name" value="{{ Auth::user()->f_name }}" id="l_f_name">
                        <input type="hidden" name="l_l_name" value="{{ Auth::user()->l_name }}" id="l_l_name">
                        <input type="hidden" name="l_bookid" value="{{$book->bookid}}" id="l_bookid">
                        <input type="hidden" name="l_title" value="{{$book->title}}" id="l_title">    
                        <input type="hidden" name="ddate" value="" id="ddate">  

                            <input type="submit" name="requaestbtn" value="Confirm">
                        </form>

                <a href="{{url('home')}}" class="btn btn-primary" role="button">
                        Cancel</a>
            </div>
        </div>

@endsection

在每个贷款页面上都会有隐藏的输入类型,它们会将数据发送到控制器,但如果用户在1周的贷款页面上,我希望“ddate”隐藏输入从现在开始具有1周的值,如果他们在2周的贷款页面等,2周后,

这是我控制器中的功能:

公共职能贷款(申请$请求)     {

    $loan = new Loan();
    $loan->userid = $request->l_userid;
    $loan->f_name = $request->l_f_name;
    $loan->l_name = $request->l_l_name;
    $loan->bookid = $request->l_bookid;
    $loan->title = $request->l_title;
    $loan->startdate = Carbon::now();
    $loan->duedate = $request->ddate
    $loan->save();
    return view('home');
} 

2 个答案:

答案 0 :(得分:0)

你能不能放入视图:

<input type="hidden" name="ddate" value="1" id="ddate"> 

然后在控制器中:

$loan->duedate = Carbon::now()->addWeeks($request->ddate);

答案 1 :(得分:0)

好的问题的解决方案是,您可以将值设置为与贷款期数相对应的数字。在您的情况下,值为1,2和4.然后在控制器中,您可以根据您收到的贷款期限编号生成日期。

但与此同时,我必须警告你,你所遵循的不良做法。用户可以编辑您的表单以将书籍作为另一个人借出。永远不要在表单中发送用户信息,最终用户可以编辑它。而是直接在控制器中使用当前登录的用户信息。

<form action="{{url('loan')}}" method="POST">
    {{ csrf_field() }}
    <input type="hidden" name="l_bookid" value="{{$book->bookid}}" id="l_bookid">
    <input type="hidden" name="l_title" value="{{$book->title}}" id="l_title">
    <input type="hidden" name="ddate" value="1" id="ddate">
    <input type="submit" name="requaestbtn" value="Confirm">
</form>

public function loan(Request $request)
{
    $loanWeeks = min($request->ddate, 4);

    $loan = new Loan();
    $loan->userid = auth()->user()->userid;
    $loan->f_name = auth()->user()->f_name;
    $loan->l_name = auth()->user()->l_name;
    $loan->bookid = $request->l_bookid;
    $loan->title = $request->l_title;
    $loan->startdate = Carbon::now();
    $loan->duedate = Carbon::now()->addWeeks($loanWeeks);
    $loan->save();

    return view('home');
}

我还添加了一张小支票,以确保用户最多只能租借这本书4周。如果您不添加此检查,那么最终用户可以再次编辑该值并随时借出书籍。