some progress
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Book;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class ReadlistController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the user's readlist.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$user = $request->user();
|
||||
$books = $user->readlistBooks()->with(['category', 'authors', 'summary'])->orderByPivot('created_at', 'desc')->paginate(12);
|
||||
|
||||
return view('readlist.index', [
|
||||
'books' => $books,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a book to the readlist.
|
||||
*/
|
||||
public function store(Request $request, Book $book)
|
||||
{
|
||||
$user = $request->user();
|
||||
$user->readlistBooks()->syncWithoutDetaching([$book->id]);
|
||||
|
||||
if ($request->wantsJson()) {
|
||||
return response()->json(['status' => 'added']);
|
||||
}
|
||||
|
||||
return back()->with('success', 'Book added to your readlist.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a book from the readlist.
|
||||
*/
|
||||
public function destroy(Request $request, Book $book)
|
||||
{
|
||||
$user = $request->user();
|
||||
$user->readlistBooks()->detach($book->id);
|
||||
|
||||
if ($request->wantsJson()) {
|
||||
return response()->json(['status' => 'removed']);
|
||||
}
|
||||
|
||||
return back()->with('success', 'Book removed from your readlist.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user