用户产品控制器
class UserProductsController extends Controller
{
public function index()
{
$products = Product::get();
return view ('products')->with(compact('products'));
}
public function product_categories()
{
$categories = Category::all();
return view ('categories')->with(compact('categories'));
}
}
产品表
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('prod_name');
$table->string('prod_brand')->nullable();
$table->unsignedBigInteger('cat_id');
$table->string('prod_image_path')->nullable();
$table->timestamps();
$table->foreign('cat_id')
->references('id')
->on('categories')
->onDelete('cascade');
});
}
类别表
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('cat_name');
$table->string('cat_image_path')->nullable();
$table->string('cat_description')->nullable();
$table->timestamps();
});
}
产品型号
class Product extends Model
{
public function category()
{
return $this->belongsTo('App\Category','category_id');
}
}
类别型号
class Category extends Model
{
public function category()
{
return $this->hasMany('App\Product');
}
}
我的路线是
Route::get('/All_Products', 'UserProductsController@index')->name('All_Products');
Route::get('/product_categories', 'UserProductsController@product_categories')->name('categories');
如何获得同一类别的所有产品?由于这是我的第一个项目,我在这方面花费了更多的时间。但对我来说没有任何作用。有人可以指导我吗?
杨魅力
森栏
白衣非少年
回首忆惘然