【问题标题】:Laravel - ErrorException Undefined index: firstname [duplicate]Laravel - ErrorException未定义索引:名字[重复]
【发布时间】:2017-09-17 12:14:51
【问题描述】:

大家好,我是 Laravel 的新手,我正在尝试通过添加、修改、删除作者和书籍来制作 crud 操作示例。所以我的作者 crud 工作正常,但是当我尝试通过获取来创建一本书时创建的作者我得到这个错误:

ErrorException (E_NOTICE)
"Undefined index: firstname"

这是我的代码: 我的 Authors.php

class Authors extends Model
{
    protected $table ='authors';
    protected $fillable = ['firstname','lastname'];

    public function book()
    {
        return $this->hasMany('Books','author');
    }
}

我的 Books.php:

class Books extends Model
{
    protected $table = 'books';
    protected $fillable = ['title'];
    public function author()
    {
        return $this->belongsTo('App\Authors');
    }
}

我的 BooksController.php(它实际上得到了异常):

 public function create()
    {
        return view('books.index');
    }

    public function store(Request $request)
    {

        $inputs = $request->all();

        $book = new Books();
        $book->title = $inputs['title'];

        $book->author()->attach($inputs['firstname']);
        $book->author()->attach($inputs['lastname']);

        $book->save();
        return redirect('/books');
    }

我的 index.php:

@section('content')

    <div class="container">
        <form method="post" action="{{url('books')}}">
            <div class="form-group row">
                {{csrf_field()}}
                <label for="lgFormGroupInput" class="col-sm-2 col-form-label col-form-label-lg">Title</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control form-control-lg" id="lgFormGroupInput" placeholder="title" name="title">
                </div>
            </div>
            <div class="form-group row">
                {{csrf_field()}}
                <label for="lgFormGroupInput" class="col-sm-2 col-form-label col-form-label-lg">Author</label>
                <div class="col-sm-10">
                    <select id="author" name="author">
                        <option value="Z">Select an author</option>
                        @foreach ($authors as $author)
                        <option value="{{$author['id']}}">{{$author['firstname']}} {{$author['lastname']}}</option>
                        @endforeach
                    </select>
                </div>
            </div>
                    <input type="submit" value="Create" />
        </form>
    </div>

    <div class="container">
        <table class="table table-striped">
            <thead>
            <tr>
                <th>ID</th>
                <th>Title</th>
                <th>Author</th>
            </tr>
            </thead>
            <tbody>
            @foreach($books as $book)
                <tr>
                    <td>{{$book['id']}}</td>
                    <td>{{$book->$author['firstname']}} {{$book->$author['lastname']}}</td>
                    <td><a href="{{action('BooksController@edit', $book['id'])}}" class="btn btn-warning">Edit</a></td>
                    <td>
                        <form action="{{action('BooksController@destroy', $book['id'])}}" method="post">
                            {{csrf_field()}}
                            <input name="_method" type="hidden" value="DELETE">
                            <button class="btn btn-danger" type="submit">Delete</button>
                        </form>
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>
    </div>
 @endsection

所以,我不知道为什么我会在我的控制器的存储中得到这个异常......提前谢谢!

【问题讨论】:

  • 因为您没有输入名称firstname
  • 它在我的作者模型中?
  • 它在您的表单上。

标签: php laravel crud


【解决方案1】:

这是因为您的表单中没有任何输入字段。表单必须包含名称为firstname 的输入文件。

您可以通过转储您的 $inputs 字段来检查这一点。例如dd($inputs)。我确信在你的控制器中这个变量不包含任何名为firstname 的索引,因为你的表单没有任何名为firstname 的输入文件。

【讨论】:

  • 是的,我的作者实际上是这本书的选择字段,因为我在上面发布了索引模板,那么我该如何存储它们呢?
  • @mastaofthepasta 我认为你不明白@u_mulder 和@Imran 都在告诉你什么。您的表单上没有名称为 firstname 的输入,但您的代码假定您有。
  • 您不想从表单中输入名字吗?这里代码的问题与Laravel无关,是HTML和PHP的问题@mastaofthepasta
【解决方案2】:

这为我解决了问题。

composer update

【讨论】:

    【解决方案3】:

    在某些情况下,当您的 POST 来自使用您的 PHP API 的人时,最好的方法是我通常解决这个问题的方法是使用 isset coalesce。

    例如

    $name = isset($_GET['name']) ? $_GET['name'] : '';
    

    或在最新的 PHP 版本中(也是 laravel)

    $name = $_GET['name'] ?? '';
    

    享受吧!

    【讨论】:

      猜你喜欢
      • 2021-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多