编辑功能在CRUD表单中不工作(Laravel)

编辑功能在CRUD表单中不工作(Laravel),laravel,Laravel,我创建了一个名为“组”的CRUD表单。创建和删除功能可以工作,但“编辑”会引发错误: “未定义变量:组(视图: C:\wamp64\www\sites\jointpromote2\resources\views\groups\edit.blade.php @section('main') <div class="row"> <div class="col-sm-8 offset-sm-2"> <h1 class="display-3">

我创建了一个名为“组”的CRUD表单。创建和删除功能可以工作,但“编辑”会引发错误:

“未定义变量:组(视图: C:\wamp64\www\sites\jointpromote2\resources\views\groups\edit.blade.php

@section('main')
<div class="row">
    <div class="col-sm-8 offset-sm-2">
        <h1 class="display-3">Update a Group</h1>

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

        <form method="post" action="{{ route('groups.update', '$group->id') }}">
            @method('PATCH')
            @csrf
            <div class="form-group">
                <label for="group_name">Group Name:</label>
                <input type="text" class="form-control" name="group_name" value="{{ $group->group_name }}" />
            </div>

            <div class="form-group">
                <label for="group_description">Group Description:</label>
                <input type="text" class="form-control" name="group_description" value="{{ $group->group_description }}" />
            </div>
            <button type="submit" class="btn btn-primary">Update</button>
        </form>
    </div>
</div>
@endsection
错误指向这行代码:

<input type="text" class="form-control" name="group_name" value="<?php echo e($group->group_name); ?>" />
这是我的
数据库/migrations/create_groups_table.php

    {
        $event = Group::find($id);
        return view('groups.edit', compact('group'));
    }
 public function down()

{
    Schema::dropIfExists('groups');
}
下面是edit.blade.php

@section('main')
<div class="row">
    <div class="col-sm-8 offset-sm-2">
        <h1 class="display-3">Update a Group</h1>

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

        <form method="post" action="{{ route('groups.update', '$group->id') }}">
            @method('PATCH')
            @csrf
            <div class="form-group">
                <label for="group_name">Group Name:</label>
                <input type="text" class="form-control" name="group_name" value="{{ $group->group_name }}" />
            </div>

            <div class="form-group">
                <label for="group_description">Group Description:</label>
                <input type="text" class="form-control" name="group_description" value="{{ $group->group_description }}" />
            </div>
            <button type="submit" class="btn btn-primary">Update</button>
        </form>
    </div>
</div>
@endsection
@节('main')
更新组
@如果($errors->any())
    @foreach($errors->all()作为$error)
  • {{$error}}
  • @endforeach

@恩迪夫 @方法('补丁') @csrf 组名: 组说明: 更新 @端部
您以错误的方式使用了“压缩”方法。

试试这个

{
  $event = Group::find($id);
  return view('groups.edit', compact($event));
}

您将错误的变量从
GroupController
传递到
edit.blade.php
视图。请将
GroupController
更改为:

    {
        $group= Group::find($id);
        return view('groups.edit', compact('group'));
    }

现在一切都应该正常了。

您的变量名是
事件
,但您正在压缩
,并在blade中使用
变量。更改控制器中的变量名,一切都会好起来

public function edit($id) {
    $group = Group::find($id);
    return view('groups.edit', compact('group'));
}

您以错误的方式编写操作。请尝试复制并粘贴下面的代码

<form method="post" action="{{ route('groups.update', 
$group->id) }}">


您必须从$group->id

中删除“.”当然,我的名字不正确,谢谢。我实现了这一点,现在它转到了我可以编辑的编辑页面,但单击“更新”会将我带到一个错误页面?错误是“从空值创建默认对象”。单击“更新”后我看到的web URL是:似乎有点奇怪g这行是错误的:$group->group_name=$request->get('group_name');你能在更新记录的地方发布你的控制器代码吗?当然可以,但如果我在这个评论部分发布,它将全部放在一行上?我应该如何发布它以便你容易阅读?(谢谢你的帮助)指定此代码时出错:
公共函数更新(请求$Request,$id){$Request->validate(['group\u name'=>'required','group\u description'=>'required',]);
$group=group::find($id);$group->group\u name=$Request->get('group\u name')$group->group_description=$request->get('group_description');$group->save();return redirect('/groups')->with('success','group updated!');
,谢谢,但这不起作用。我实现了zlatan建议的解决方案,但它只解决了一半问题。当我添加了你的“compact($event”)时'在代码中,它恢复为完全不工作?很好!我实现了这一点,现在它成功地转移到编辑页面,我可以编辑,但单击“更新”会将我带到一个错误页面?你能给你看控制器吗?你是说GroupController.php吗?你能共享你的GroupController.php代码吗?当然,但如果我复制并粘贴它,他会告诉我请注意,它将全部显示在一行上!人们通常如何在注释中共享代码?请在粘贴之前留出空间谢谢。我使用不同的教程重新编辑了整个内容,现在可以使用了!我将记录您的注释以备将来使用。如果您找到帮助,请单击向上箭头标记进行投票,以便其他人可以通过查看此内容找到有用的内容post.Done。虽然记录了声誉低于15的人的投票,但不更改公开显示的post分数。好的,没问题