Solution and explanation
- MVC means Model, View, Controller.
- Model: responsible for data and database interaction.
class Product extends Model
{
protected $fillable = ['name', 'price', 'category_id'];
}
- View: responsible for presentation.
<h1>{{ $product->name }}</h1>
<p>Price: {{ $product->price }}</p>
- Controller: coordinates the request.
class ProductController extends Controller
{
public function show(int $id)
{
$product = Product::findOrFail($id);
return view('products.show', compact('product'));
}
}
Request → Route → Controller → Model → Database → Model → Controller → View → Response
- Important interview point: don't say "the model contains all business logic".
- In a small application it might, but in a large production application responsibilities are separated:
Controller
↓
Service
↓
Repository / Query
↓
Model