0

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:

ErrorException (E_NOTICE)
"Undefined index: firstname"

So heres my code: my Authors.php

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

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

my 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):

 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:

@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!

mastaofthepasta
  • 91
  • 1
  • 2
  • 9

3 Answers3

2

This is because you don't have any input field in your form. The form must contain an input filed with 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.

Imran
  • 4,582
  • 2
  • 18
  • 37
  • yeah, my authors are select field for the book actually as I posted the index template above, so how can I store them? – mastaofthepasta Sep 17 '17 at 13:27
  • @mastaofthepasta I don't think you are understanding what both @u_mulder and @Imran are telling you. You do not have an input with the name `firstname` on your form, but your code assumes you do. – Don't Panic Sep 17 '17 at 16:01
  • Don't you want the first name to be input from the form? The problem of the code here has no relation with Laravel, the problem is with HTML and PHP @mastaofthepasta – Imran Sep 17 '17 at 16:40
0

This fixed it for me.

composer update
MT_Shikomba
  • 129
  • 1
  • 6
0

In some cases when your POST is coming from someone using your PHP APIs the best way and how I usually solve this is by using an isset coalesce.

For example

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

or in latest PHP versions (also laravel)

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

Enjoy!

Oliver M Grech
  • 3,071
  • 1
  • 21
  • 36