【问题标题】:Unable to edit data on the same view page laravel无法在同一视图页面 laravel 上编辑数据
【发布时间】:2022-01-15 08:22:23
【问题描述】:

我想在同一页面上更新营业时间。我在 stackoverflow 中查看了其他答案,但我仍然无法管理。非常感谢您的帮助。

型号


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Hour extends Model
{
    public $table = 'biz_hour';
    protected $primaryKey = 'day_id';
    protected $fillable = [
        'day_id', 'day_name', 'start_time', 'end_time', 'day_off'
    ];
    public $timestamps = false;

}

控制器

public function update(Request $request, Hour $biz_hour)
    {
        
        $request->validate([
            'start_time' => 'required',
        ]);
    
        $start_time = \App\Models\Hour::find($biz_hour);
    
        ['start_time' => $request->start_time];
        foreach($biz_hour as $biz_hour){
        $start_time->save();
        }
        return redirect('start_time');

        //$biz_hour->update($request->all());
        //return redirect()->route('biz_hour.index');
    }

Biz_hour.blade.php

<div class="table-responsive">
                        <table class="table">
                            <thead class="text-uppercase">
                                <tr>
                                    <th scope="col">Day</th>
                                    <th scope="col">Start Time</th>
                                    <th scope="col">End Time</th>
                                    <th scope="col">is Day off?</th>
                                </tr>
                            </thead>
                            @foreach($biz_hour as $biz_hour)
                            <form action="{{ route('biz_hour.update',$biz_hour->day_id) }}" method="POST">
                                @csrf
                                @method('PUT')
                            <tbody>
                                <tr>
                                    <th scope="row"><br>{{$biz_hour->day_name}}</th>
                                    <td><div class="form-group">
                                        <input class="form-control" type="time" value={{ Carbon::parse($biz_hour->start_time)->format('h:i') }} name="start_time"></div>
                                    </td>
                                    <td><div class="form-group">
                                        <input class="form-control" type="time" value={{$biz_hour->end_time}} name="end_time"></div>
                                    </td>
                                    <td><br><label class="switch">
                                        <input type="checkbox">
                                        <span class="slider round"></span>
                                      </label></td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>    
                <div class="main-content-inner">
                    <div class="col-lg-6 mt-5">
                        <button type="submit" class="btn btn-primary mb-3" name="upload">Save</button>
                    </div>
                </div>
            </form> 

点击保存按钮后,页面只刷新,数据不发送到数据库。提前致谢。

【问题讨论】:

    标签: javascript php laravel


    【解决方案1】:

    更新未完成,因为您实际上没有为要更新的 start_time 分配新的 start_time。 试试这个

    public function update(Request $request, Hour $biz_hour)
    {
        
        $request->validate([
            'start_time' => 'required',
        ]);
    
        $start_time = \App\Models\Hour::find($biz_hour);
    
        $start_time->start_time => $request->start_time;
    
        foreach($biz_hour as $biz_hour){
             $start_time->save();
        }
        return redirect()->back()->with('status', 'updated');
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 2019-08-28
      相关资源
      最近更新 更多