Laravel - 获取所选项目的行ID

时间:2017-09-15 10:49:05

标签: php laravel razor

我是Laravel的新手。我想检查表单中是否有所选项目和用户的ID。这两个字段都在我的item_assignments表中。只要找不到ID,它就会创建一个新的作业并将其存储到item_assignments表中。这有效,但是当找到ID时,它应该通过递增输入的计数来更新现有ID。

以下是我的观点。

        <form class="form-horizontal" method="post" action="{{url('item/assign')}}" enctype="multipart/form-data">
            <input type="hidden" name="_token" value="{{ csrf_token() }}" >

            <legend>Assign Item</legend>

            <fieldset>

                <input id="eid" name="id" type="hidden">

                <!-- Text input-->
                <div class="form-group">
                    <label class="col-md-4 control-label" for="name">Item Name:</label>  
                    <div class="col-md-6">
                        <input id="ename" name="name" type="text" class="form-control input-md" readonly>

                    </div>
                </div>

                <!-- Select Basic -->
                    <div class="form-group{{ $errors->has('user') ? ' has-error' : '' }}">
                    <label class="col-md-4 control-label" for="user">Assign to:</label>
                    <div class="col-md-6">
                        <select id="user" name="user" class="form-control">
                            <option value="0">Select a User</option>
                            @foreach($users as $user)
                            <option value="{{$user->id}}">{{$user->name}}</option>
                            @endforeach
                        </select>
                            @if ($errors->has('user'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('user') }}</strong>
                                </span>
                            @endif

                    </div>
                </div>


                <!-- Text input-->
                    <div class="form-group{{ $errors->has('count') ? ' has-error' : '' }}">
                    <label class="col-md-4 control-label" for="count">Item Count:</label>  
                    <div class="col-md-6">
                        <input id="count" name="count" type="text" placeholder="Enter Item Count" class="form-control input-md">
                            @if ($errors->has('count'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('count') }}</strong>
                                </span>
                            @endif

                    </div>
                </div>

                <!-- Button (Double) -->
                <div class="form-group">
                    <label class="col-md-5 control-label" for="submit"></label>
                    <div class="col-md-7">
                        <button id="submit" name="submit" class="btn btn-success">Assign</button>
                    </div>
                </div>
        </form>

这是我的控制器

    // checks the ID of the selected item
    $assign1 = ItemAssign::where("item_id", $req->id)->where('user_id', $req->user)->first();

   // this works
    if (!($assign1)) {
        $assign = new ItemAssign();
        $assign->user_id = $req->user;
        $assign->item_id = $req->id;
        $assign->item_count = $req->count;
        $assign->balance = $req->count;
        $assign->save();
    } 
    // this is what I want to work
    else {
        $assign = ItemAssign::find($assign1);
        $assign->item_count += $req->count;
        $assign->balance += $req->count;
        $assign->save();
    }
    // stores to user table
    $user = User::find($req->user);
    $user->received += $req->count;
    $user->save();

    // stores to item table
    $item = Item::find($req->id);
    $count = $item->item_count;
    $item->assigned_count += $req->count;
    $item->remaining = $count - $item->assigned_count;
    $item->save();
    return redirect('assignment');

2 个答案:

答案 0 :(得分:1)

如果您已经在$ assign1中存储了ItemAssign实例,那么您应该只需更新该实例而不是使用查找。

IE:

ImportError: Traceback (most recent call last)
<ipython-input-5-23fcc4dbc1e0> in <module>()
----> 1 from bkcharts import Scatter, output_file, show
      2 import pandas
      3 
      4 df=pandas.DataFrame(columns=["X","Y"])
      5 df["X"]=[1,2,3,4,5]

c:\users\ellamm\appdata\local\programs\python\python36-32\lib\site-packages\bkcharts\__init__.py in <module>()
      3 
      4 # defaults and constants
----> 5 from bokeh.plotting.helpers import DEFAULT_PALETTE; DEFAULT_PALETTE
      6 
      7 # main components

ImportError: cannot import name 'DEFAULT_PALETTE'

答案 1 :(得分:0)

 else {
        $assign = ItemAssign::find($assign1);
        $assign->item_count += $req->count;
        $assign->balance += $req->count;
        $assign->update();
    }