@laracoft Yes.
Here is an example where it's used in the controller:
I've created a model for it.
class BooksIndexController extends Controller
{
public function __invoke()
{
return view('books.index')
->with([
'books' => BookIndexView::query()
->orderBy('author_name')
->orderBy('series')
->orderBy('part')
->orderBy('published_year')
->get()
]);
}
}
And the migration for the view, I would not want to attempt this query in Eloquent/Query Builder.
class CreateBookIndexViews extends Migration
{
public function up(): void
{
DB::statement("CREATE OR REPLACE VIEW book_index_views AS
SELECT
(SELECT GROUP_CONCAT(a.id ORDER BY a.id SEPARATOR ',')
FROM authors a, author_book ab
WHERE a.id = ab.author_id
AND ab.book_id = b.id) author_id,
(SELECT GROUP_CONCAT(concat(a.last_name, ', ', a.first_name)
ORDER BY a.last_name, a.first_name SEPARATOR ' & ')
FROM authors a, author_book ab
WHERE ab.author_id = a.id
AND ab.book_id = b.id) author_name,
b.id book_id,
b.title,
b.part,
b.published_year,
CASE s.name
WHEN 'Standalone'
THEN b.published_year
ELSE (SELECT MIN(bi.published_year)
FROM books bi
WHERE bi.series_id = b.series_id)
END series_started,
f.name format,
g.name genre,
s.name series
FROM books b,
formats f,
genres g,
series s
WHERE b.genre_id = g.id
AND b.format_id = f.id
AND b.series_id = s.id
");
}
public function down(): void
{
DB::statement('DROP INDEX IF EXISTS book_index_views');
}