Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0
// 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
	
} );
Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.