I am working on a function in my library application, where users can rent out books. Now, the renting out works fine, and I can even display the data, but after I wanted to create a function to delete the rented out books (after they were returned) I get an error message: Property [pivot] does not exist on this collection instance. I can't even figure out what my mistake was, because it worked fine a minute ago, and the rented book gets deleted in the databank, but I can't display my page without the error message. Could you please check if you might find the problem? It first happened when I tried to create the delete function.
So basically this is a pivot table, that is made up of my books and readers table. I also added the attributes id and maxreturndate.
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Book;
use App\Reader;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
class CheckedOutController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$book = Book::orderBy('title')->get();
$reader = Reader::orderBy('name')->get();
$duedate = Carbon::now()->addDays(14);
$readers = Reader::all();
return view('checkedouts/index', compact('book', 'readers'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$books = Book::doesntHave("readers")->get();
$readers = Reader::all();
return view('checkedouts/create', compact('books', 'readers'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$reader = Reader::find($request->reader_id);
$reader
->books()
->attach(
$request->book_id,
["maxreturndate" => Carbon::now()->addDays(14)]
);
return redirect('checkedouts')->with('success', 'Buch wurde erfolgreich verliehen!');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$book = DB::table('book_reader')
->where('id', $id)
->take(1)
->delete();
return redirect('checkedouts')->with('success', 'Das Buch wurde zurückgebracht!');
}
}
index.blade.php:
@extends('layout')
@section('title')
<title>Alle ausgeliehen Bücher</title>
@section('content')
<style>
.uper {
margin-top: 40px;
}
</style>
<div class="uper">
@if(session()->get('success'))
<div class="alert alert-success">
{{ session()->get('success') }}
</div><br />
@endif
<table class="table table-hover">
<thead>
<tr>
<td>ID</td>
<td>Titel</td>
<td>Verliehen an</td>
<td>Verliehen bis</td>
<td>Bearbeiten</td>
</tr>
</thead>
<tbody>
@foreach($readers as $reader)
<tr>
@foreach($reader->books as $book)
<td>{{$book->pivot->id}}</td>
<td>{{$book->title}}</td>
@endforeach
<td>{{$reader->name}}</td>
<td>{{$book->pivot->maxreturndate}}</td>
<td>
<form action="{{ route('checkedouts.destroy', $book->pivot->id)}}" method="post">
@csrf
@method('DELETE')
<button class="btn btn-danger" type="submit">Löschen</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
<div>
@endsection
Book Model:
public function readers()
{
return $this
->belongsToMany(Reader::class, 'book_reader')
->using(Checkout::class)
->withPivot(['id', 'returndate', 'maxreturndate']);
}