kriszpchicken's avatar

Validation of ManyToMany Relationship data

I have my main table, which is books. I have created 3 ManyToMany relationships to the books table from the publisher, authors and genres table. When I'm creating a new book, and input data into the publishers, authors and genres these won't be validated.What could be the 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 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!');
    }}

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')
@foreach($errors->all() as $error)
<div>{{ $error }}</div>
@endforeach
<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 value="{{ $genre->id }}" > {{$genre->name}}
                    @endforeach
                </label>
                @error('genre.'. $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
0 likes
0 replies

Please or to participate in this conversation.