@mekol make sure you don't have any validation errors, you can test that by adding this to your template and then submitting the form:
@foreach($errors->all() as $error)
<div>{{ $error }}</div>
@endforeach
I have created a form, where I can add a new book. However, after I push the create button nothing happens, it just refreshes. Could you please help me with this problem?
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Book;
use App\Language;
use App\Genre;
use App\Author;
use App\Publisher;
class BookController extends Controller
{
public function index(Request $request)
{
$books = Book::with(['language', 'authors', 'publishers', 'genres'])->get();
return view('books/index', compact('books'));
}
public function create()
{
$languages = Language::all();
$genres = Genre::all();
$authors = Author::all();
$publishers = Publisher::all();
return view('books/create', compact('languages','genres','authors','publishers'));
}
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|max:255',
'authors' => 'array|min:1',
'authors.*' => 'exists:authors,id',
'year' => 'required|numeric|max:2500',
'publishers' => 'array|min:1',
'publishers.*' => 'exists:publishers,id',
'genres' => 'array|min:1',
'genres.*' => 'exists:genres,id',
'language_id' => 'required',
'isbn' => 'required|numeric|max:9999999999999',
'pages' => 'required|numeric',
]);
$book = Book::create($validatedData);
$book->authors()->attach(request('authors'));
$book->genres()->attach(request('genres'));
$book->publishers()->attach(request('publishers'));
return redirect('books')->with('success', 'Buch wurde erfolgreich hinzugefügt!');
}
public function show($id)
{
$book = Book::findOrFail($id);
$languages = Language::all();
$genres = Genre::all();
$authors = Author::all();
$publishers = Publisher::all();
return view('books/show', compact('book', 'languages', 'genres','authors','publishers'));
}
public function edit($id)
{
$book = Book::findOrFail($id);
$languages = Language::all();
$genres = Genre::all();
$authors = Author::all();
$publishers = Publisher::all();
return view('books/edit', compact('book', 'languages', 'genres','authors','publishers'));
}
public function update(Request $request, $id)
{
$book=Book::findorFail($id);
$validatedData = $request->validate([
'title' => 'required|max:255',
'authors' => 'exists:authors,id',
'year' => 'required|numeric|max:2500',
'publishers' => 'exists:publishers,id',
'genres' => 'exists:genres,id',
'language_id' => 'required',
'isbn' => 'required|numeric|max:9999999999999',
'pages' => 'required|numeric',
]);
$validatedData =$request->all();
$book->fill($validatedData)->save();
$book->authors()->attach(request('authors'));
$book->genres()->attach(request('genres'));
$book->publishers()->attach(request('publishers'));
return redirect('books')->with('success', 'Buch wurde erfolgreich bearbeitet!');
}
public function destroy($id)
{
$book = Book::findOrFail($id);
$book->delete();
return redirect('books')->with('success', 'Buch wurde erfolgreich entfernt!');
}
public function search(Request $request)
{
$validatedData =$request->validate([
'book_search' => 'min:3',
]);
$search = $request->get('book_search');
$books = Book::with(['language', 'authors', 'publishers', 'genres'])->where('title','like','%' .$search. '%')->get();
return view('books/index', compact('books'));
}
}
create.blade.php
@extends('layout')
@section('title')
<title>Neues Buch hinzufügen</title>
@section('stylesheets')
<script src="http://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/select2.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/select2.min.css" rel="stylesheet" />
@endsection
@section('content')
<style>
.uper {
margin-top: 40px;
}
</style>
<div class="card uper">
<div class="card-header">
Neues Buch hinzufügen
</div>
<div class="card-body">
<form method="post" action="{{ route('books.store') }}">
<div class="form-group">
@csrf
<label for="title">Titel:</label>
<input type="text" class="form-control" name="title" value="{{ old('title')}}" />
@error('title')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="authors">Author(en):</label>
<select name="authors[]" multiple class="form-control select2-multi <!-- @error('authors') is-invalid @enderror -->">
@foreach ($authors as $author)
<option value="{{ $author->id }}" {{ in_array($author->id, old('authors') ?? []) ? 'selected' : '' }}>
{{ $author->name }}
</option>
@endforeach
</select>
@error('authors')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="year">Jahr:</label>
<input type="text" class="form-control" name="year" value="{{ old('year')}}" />
@error('year')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="publishers">Verlag(e):</label>
<select name="publishers[]" multiple class="form-control select2-multi <!-- @error('publishers') is-invalid @enderror -->">
@foreach ($publishers as $publisher)
<option value="{{ $publisher->id }}" {{ in_array($publisher->id, old('publishers') ?? []) ? 'selected' : '' }}>
{{ $publisher->name }}
</option>
@endforeach
</select>
@error('publishers')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="genres">Genre(s):
@foreach($genres as $genre)
<input type="checkbox" name="genres[{{$genre->id}}]"@if(array_key_exists($genre->id, old('genres', []))) checked @endif > {{$genre->name}}
@endforeach
</label>
</select>
@error('genre_id')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="language_id">Sprache:</label>
<select name="language_id" class="form-control @error('language_id') is-invalid @enderror">
<option value="{{ old('language_id')}}">-- {{ __('Spache auswählen') }} --</option>
@foreach ($languages as $language)
@if (old('language_id') == $language->id)
<option value="{{ $language->id }}" selected>{{ $language->name }}</option>
@else
<option value="{{ $language->id }}">{{ $language->name }}</option>
@endif
@endforeach
</select>
@error('language_id')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="isbn">ISBN:</label>
<input type="text" class="form-control" name="isbn" value="{{ old('isbn')}}" />
@error('isbn')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<div class="form-group">
<label for="pages">Seitenzahl:</label>
<input type="text" class="form-control" name="pages" value="{{ old('pages')}}" />
@error('pages')
<div class="alert alert-danger">{{ $message }}</div>
@enderror
</div>
<button type="submit" class="btn btn-primary">Hinzufügen</button>
</form>
</div>
</div>
<script type="text/javascript">
$(".select2-multi").select2();
</script>
@endsection
Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Book extends Model
{
protected $fillable = ['title', 'year', 'language_id', 'isbn', 'pages', 'authors','publisher','genres'];
public function publishers(){
return $this->belongsToMany(Publisher::class);
}
public function authors(){
return $this->belongsToMany(Author::class);
}
public function genres(){
return $this->belongsToMany(Genre::class);
}
public function language(){
return $this->belongsTo(Language::class);
}
public function readers()
{
return $this
->belongsToMany(Reader::class, 'book_reader')
->using(Checkout::class)
->withPivot(['id', 'returndate', 'maxreturndate']);
}
}
Please or to participate in this conversation.