Skip to content

Latest commit

 

History

History

06-components-and-controllers

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Components and Controllers

STEP 1) Create a components folder into the views folder

image

Add following code

@props(['Movie'])
<div>
    <h2>
        <a href="/movies/{{$Movie['id']}}">
            {{$Movie['Title']}} 
        </a>
    </h2>
    <h3>
        Year: <a href="/movies?year={{$Movie['Year']}}"> {{$Movie['Year']}} </a>
    </h3>
    <h3>
        Genre: <a href="/movies?genre={{$Movie['Genre']}}"> {{$Movie['Genre']}} </a>
    </h3>
    <h3>
        Director: <a href="/movies?director={{$Movie['Director']}}"> {{$Movie['Director']}} </a>
    </h3>
    <h3>
        Producer: {{$Movie['Producer']}}
    </h3>
    <h3>
        Actors: {{$Movie['Actors']}}
    </h3>
    <p>
        {{$Movie['Description']}}
    </p>
</div>

STEP 2) Use the component on the movies view

Replace the code in 'movies.blade.php' file with the following code

<h1>{{$title}}</h1>
@foreach($movies as $Movie)
    <x-movie-card :Movie="$Movie" />
@endforeach

STEP 3) Create a Movie Controller

Run the following command

php artisan make:controller MoviesController

A new file with be created in 'app/Http/Controllers'

image

Replace all the file content with the following code

<?php

namespace App\Http\Controllers;

use App\Models\Movie;

class MoviesController extends Controller
{
    public function index()
    {
        return view('movies', [
            'title' => 'Movies',
            'movies' => Movie::all()
        ]);
    }

    public function show(Movie $movie)
    {
        return view('movie', [
        
            'Movie' => $movie
        ]);

    }
}

STEP 3) Change the Routes to use the new MoviesController

image

Replace the code in 'web.php' with the following code

<?php

use App\Http\Controllers\MoviesController;
use Illuminate\Support\Facades\Route;
use App\Models\Movie;

Route::get('/movies', [MoviesController::class, 'index']);

Route::get('/movies/{movie}', [MoviesController::class, 'show']);