Laravel - ErrorException 未定义索引:名字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46263958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - ErrorException Undefined index: firstname
提问by mastaofthepasta
Sup y'all,I am new to Laravel and I'm trying to make crud operations example with adding,modifying,deleting authors and books.So my authors crud works alright, but when I try to create a book by getting the created author I get this error:
大家好,我是 Laravel 的新手,我正在尝试通过添加、修改、删除作者和书籍来制作 crud 操作示例。所以我的作者 crud 工作正常,但是当我尝试通过获取创建的作者来创建一本书时我收到此错误:
ErrorException (E_NOTICE)
"Undefined index: firstname"
So heres my code: my Authors.php
所以继承人我的代码:我的 Authors.php
class Authors extends Model
{
protected $table ='authors';
protected $fillable = ['firstname','lastname'];
public function book()
{
return $this->hasMany('Books','author');
}
}
my Books.php:
我的 Books.php:
class Books extends Model
{
protected $table = 'books';
protected $fillable = ['title'];
public function author()
{
return $this->belongsTo('App\Authors');
}
}
my BooksController.php(where it actually gets the exception):
我的 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');
}
my index.php:
我的 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
So, I have no idea why do I get this exception in the store of my controller...Thanks in advance!
所以,我不知道为什么我会在我的控制器的商店中得到这个异常......提前致谢!
回答by Imran
This is because you don't have any input field in your form. The form must contain an input filed with name firstname
.
这是因为您的表单中没有任何输入字段。表单必须包含一个以 name 提交的输入firstname
。
You can check this by dumping your $inputs
field. For example dd($inputs)
. I am sure in you controller this variable does't contains any index called firstname
because your form doesn't have any input filed named as firstname
.
您可以通过转储您的$inputs
字段来检查这一点。例如dd($inputs)
。我确信在你的控制器中,这个变量不包含任何被调用的索引,firstname
因为你的表单没有任何名为firstname
.