// models/Product.php
abstract class Product extends Eloquent {
protected $table = 'products';
// commom methods
}
// models/PhysicalProduct.php
class PhysicalProduct extends Product {
// because we are overriding we must specified the same function prototype for get
public function get( $arg = [] ) {
return parent::where( 'type', 'physical' )->get();
}
}
// some similar goes for the other types of products
now, in your code, just make an instance of some product type, for example, DigitalProduct
,
$product = new DigitalProduct;
$product->where( 'price', '>', 51.12 )
// because we have overriding the get method, all of our queries
// will have the correct where type value
->get();
now for loading the correct view, i recommend to you that in your routes uses the url to know what controller must be trigger for a specific product type, ie
Route::group( [ 'prefix' => 'product' ], function() {
Route::group( [ 'prefix' => 'digital' ], function() {
Route::get( '{id}', 'ProductDigitalController@get' );
Route::post( '{id}/edit', 'ProductDigitalController@edit' );
} );
// some similar goes for the other types of products
} );
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community