最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

laravel - The data update fails and is lost - Stack Overflow

programmeradmin1浏览0评论

Data update fails and data is lost.

Data was able to be updated before composer update.

web.php

Route::get('/schedules.index', [ScheduleController::class, 'index'])->name('schedule');

ScheduleController.php

/**
 * Display a listing of the resource.
 * 一覧表示
 */
public function index()
{
   // スタート時間が早い順にスケジュールを取得
$schedules = Schedule::orderBy('start_time', 'asc')->get();
return view('schedules.index', compact('schedules'));
}

/**
 * Update the specified resource in storage.
 * 更新処理
 */
public function update(Request $request, string $id)
{
$schedule = Schedule::find($id);
if (!$schedule) {
    return redirect()->route('schedule')->with('error', 'スケジュールが見つかりませんでした');
}

// バリデーション処理
$validated = $request->validate([
    'title' => 'required|string|max:255',
    'description' => 'nullable|string',
    'start_time' => 'required|date',
    'end_time' => 'required|date|after:start_time',
]);

// スケジュールを更新
$schedule->update($validated);

return redirect()->route('schedule')->with('success', 'スケジュールが更新されました。');
}

edit.blade.php

@if ($errors->any())
    <div class="alert alert-danger">
    <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
    </div>
@endif

@extends('layouts.app')

@section('content')
<header style="background-color: #66cdaa;">
</header>
<body style="background-color: #dcdcdc;">
<div class="container">
    <div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header" style="background-color: #e6e6fa;">{{ __('スケジュール編集') }}</div>

            <div class="card-body" style="background-color: #e6e6fa;">
                <form action="{{ route('schedules.update', $schedule->id) }}" style="background-color: #e6e6fa;" style="background-color: #f0f8ff;" method="POST">
                    @csrf
                    @method('PUT') <!-- 更新メソッド指定 -->
                    
                    <div class="mb-3" style="background-color: #e6e6fa;">
                        <label for="title" class="form-label">タイトル:</label>
                        <input type="text" class="form-control" style="background-color: #f0f8ff;" name="title" value="{{ old('title', $schedule->title) }}" required>
                    </div>

                    <div class="mb-3" style="background-color: #e6e6fa;">
                        <label for="description" class="form-label">説明:</label>
                        <textarea class="form-control" style="background-color: #f0f8ff;" name="description">{{ old('description', $schedule->description) }}</textarea>
                    </div>

                    <div class="mb-3" style="background-color: #e6e6fa;">
                        <label for="start_time" class="form-label">開始時間:</label>
                        <input type="datetime-local" class="form-control" style="background-color: #f0f8ff;" name="start_time" value="{{ old('start_time', \Carbon\Carbon::parse($schedule->start_time)->format('Y-m-d\TH:i')) }}" required>
                    </div>

                    <div class="mb-3" style="background-color: #e6e6fa;">
                        <label for="end_time" class="form-label">終了時間:</label>
                        <input type="datetime-local" class="form-control" style="background-color: #f0f8ff;" name="end_time" value="{{ old('end_time', \Carbon\Carbon::parse($schedule->end_time)->format('Y-m-d\TH:i')) }}" required>
                    </div>

                    <button type="submit" class="btn btn-primary">更新</button>
                    
                    <!-- 削除ボタン -->
                    <form action="{{ route('schedules.destroy', $schedule->id) }}" style="background-color: #f0f8ff;" method="POST" class="d-inline">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn btn-danger"
                            onclick="return confirm('本当に削除しますか?')">削除</button>
                    </form>                                               
                </form>

                <div class="mt-3" style="background-color: #e6e6fa;">
                    <a href="{{ route('schedule') }}" class="btn btn-secondary me-2">戻る</a>
                    <a href="{{ route('dashboard') }}" class="btn btn-outline-primary">{{ __('ダッシュボードへ戻る') }}</a>
                </div>
            </div>
        </div>
    </div>
    </div>
</div>
@endsection

Is there a problem? I fixed the error but it didn't solve the problem.

Data update fails and data is lost.

Data was able to be updated before composer update.

web.php

Route::get('/schedules.index', [ScheduleController::class, 'index'])->name('schedule');

ScheduleController.php

/**
 * Display a listing of the resource.
 * 一覧表示
 */
public function index()
{
   // スタート時間が早い順にスケジュールを取得
$schedules = Schedule::orderBy('start_time', 'asc')->get();
return view('schedules.index', compact('schedules'));
}

/**
 * Update the specified resource in storage.
 * 更新処理
 */
public function update(Request $request, string $id)
{
$schedule = Schedule::find($id);
if (!$schedule) {
    return redirect()->route('schedule')->with('error', 'スケジュールが見つかりませんでした');
}

// バリデーション処理
$validated = $request->validate([
    'title' => 'required|string|max:255',
    'description' => 'nullable|string',
    'start_time' => 'required|date',
    'end_time' => 'required|date|after:start_time',
]);

// スケジュールを更新
$schedule->update($validated);

return redirect()->route('schedule')->with('success', 'スケジュールが更新されました。');
}

edit.blade.php

@if ($errors->any())
    <div class="alert alert-danger">
    <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
    </div>
@endif

@extends('layouts.app')

@section('content')
<header style="background-color: #66cdaa;">
</header>
<body style="background-color: #dcdcdc;">
<div class="container">
    <div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header" style="background-color: #e6e6fa;">{{ __('スケジュール編集') }}</div>

            <div class="card-body" style="background-color: #e6e6fa;">
                <form action="{{ route('schedules.update', $schedule->id) }}" style="background-color: #e6e6fa;" style="background-color: #f0f8ff;" method="POST">
                    @csrf
                    @method('PUT') <!-- 更新メソッド指定 -->
                    
                    <div class="mb-3" style="background-color: #e6e6fa;">
                        <label for="title" class="form-label">タイトル:</label>
                        <input type="text" class="form-control" style="background-color: #f0f8ff;" name="title" value="{{ old('title', $schedule->title) }}" required>
                    </div>

                    <div class="mb-3" style="background-color: #e6e6fa;">
                        <label for="description" class="form-label">説明:</label>
                        <textarea class="form-control" style="background-color: #f0f8ff;" name="description">{{ old('description', $schedule->description) }}</textarea>
                    </div>

                    <div class="mb-3" style="background-color: #e6e6fa;">
                        <label for="start_time" class="form-label">開始時間:</label>
                        <input type="datetime-local" class="form-control" style="background-color: #f0f8ff;" name="start_time" value="{{ old('start_time', \Carbon\Carbon::parse($schedule->start_time)->format('Y-m-d\TH:i')) }}" required>
                    </div>

                    <div class="mb-3" style="background-color: #e6e6fa;">
                        <label for="end_time" class="form-label">終了時間:</label>
                        <input type="datetime-local" class="form-control" style="background-color: #f0f8ff;" name="end_time" value="{{ old('end_time', \Carbon\Carbon::parse($schedule->end_time)->format('Y-m-d\TH:i')) }}" required>
                    </div>

                    <button type="submit" class="btn btn-primary">更新</button>
                    
                    <!-- 削除ボタン -->
                    <form action="{{ route('schedules.destroy', $schedule->id) }}" style="background-color: #f0f8ff;" method="POST" class="d-inline">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn btn-danger"
                            onclick="return confirm('本当に削除しますか?')">削除</button>
                    </form>                                               
                </form>

                <div class="mt-3" style="background-color: #e6e6fa;">
                    <a href="{{ route('schedule') }}" class="btn btn-secondary me-2">戻る</a>
                    <a href="{{ route('dashboard') }}" class="btn btn-outline-primary">{{ __('ダッシュボードへ戻る') }}</a>
                </div>
            </div>
        </div>
    </div>
    </div>
</div>
@endsection

Is there a problem? I fixed the error but it didn't solve the problem.

Share Improve this question edited Jan 19 at 22:01 matiaslauriti 8,1224 gold badges36 silver badges47 bronze badges asked Jan 19 at 21:57 hiroppy123hiroppy123 111 bronze badge 3
  • Do you have any error thrown? also I can see a nested form in your blade file might not be the problem but nesting form tags might not be a great idea – Soneye Oluwasina Commented Jan 20 at 1:46
  • 1 nested forms not allowed in HTML check this link Is it valid to have an HTML form inside another HTML form? – ash Commented Jan 20 at 6:34
  • After I fixed it like this it worked fine. Thank you. – hiroppy123 Commented Jan 21 at 4:53
Add a comment  | 

2 Answers 2

Reset to default 0

In your code snippet I see that your delete form is nested inside the update form. I assume that sometimes the wrong form gets triggered and that is causing the issue.

In the w3 docs you can read the following: "Note you are not allowed to nest FORM elements!"

<!-- edit.blade.php -->
@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif


@extends('layouts.app')

@section('content')
<header style="background-color: #66cdaa;">
</header>
<body style="background-color: #dcdcdc;">
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header" style="background-color: #e6e6fa;">{{ __('スケジュール編集') }}</div>

                <div class="card-body" style="background-color: #e6e6fa;">
                    <form action="{{ route('schedules.update', $schedule->id) }}" style="background-color: #e6e6fa;" style="background-color: #f0f8ff;" method="POST">
                        @csrf
                        @method('PUT') <!-- 更新メソッド指定 -->
                        
                        <div class="mb-3" style="background-color: #e6e6fa;">
                            <label for="title" class="form-label">タイトル:</label>
                            <input type="text" class="form-control" style="background-color: #f0f8ff;" name="title" value="{{ old('title', $schedule->title) }}" required>
                        </div>

                        <div class="mb-3" style="background-color: #e6e6fa;">
                            <label for="description" class="form-label">説明:</label>
                            <textarea class="form-control" style="background-color: #f0f8ff;" name="description">{{ old('description', $schedule->description) }}</textarea>
                        </div>

                        <div class="mb-3" style="background-color: #e6e6fa;">
                            <label for="start_time" class="form-label">開始時間:</label>
                            <input type="datetime-local" class="form-control" style="background-color: #f0f8ff;" name="start_time" value="{{ old('start_time', \Carbon\Carbon::parse($schedule->start_time)->format('Y-m-d\TH:i')) }}" required>
                        </div>

                        <div class="mb-3" style="background-color: #e6e6fa;">
                            <label for="end_time" class="form-label">終了時間:</label>
                            <input type="datetime-local" class="form-control" style="background-color: #f0f8ff;" name="end_time" value="{{ old('end_time', \Carbon\Carbon::parse($schedule->end_time)->format('Y-m-d\TH:i')) }}" required>
                        </div>

                        <button type="submit" class="btn btn-primary">更新</button>
                    </form>
                </div>
                <div class="card-body" style="background-color: #e6e6fa;">                  
                    <!-- 削除ボタン -->
                    <form action="{{ route('schedules.destroy', $schedule->id) }}" style="background-color: #f0f8ff;" method="POST" class="d-inline">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn btn-danger"
                            onclick="return confirm('本当に削除しますか?')">削除</button>
                    </form>
                </div>
                <div class="card-body" style="background-color: #e6e6fa;">              
                    <div class="mt-3" style="background-color: #e6e6fa;">
                        <a href="{{ route('schedules') }}" class="btn btn-secondary me-2">戻る</a>
                        <a href="{{ route('dashboard') }}" class="btn btn-outline-primary">{{ __('ダッシュボードへ戻る') }}</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
发布评论

评论列表(0)

  1. 暂无评论