UserProductsController
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'));
}
}
products_table
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');
});
}
categories_table
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');
如何获得相同类别的所有产品?由于这是我的第一个项目,因此我将花费更多的时间。但是对我没有任何作用。有人可以引导我吗?
答案 0 :(得分:1)
假设您正确设置了关系(不是这样)
您有几种使用口才的方法:
$products = Category::findOrFail($categoryId)->products;
$products = Product::where('category_id', $categoryId)->get();
$products = Product::whereHas('category', function ($query) use ($categoryId) {
$q->where('id', $categoryId);
})->get();
列举一些。
Laravel 7.x Docs - Eloquent - Retrieving Single Models / Aggregates findOrFail
Laravel 7.x Docs - Eloquent - Relationships - Relationship Methods vs Dynamic Properties
Laravel 7.x Docs - Eloquent - Relationships - Querying the Existence of Relationships whereHas
答案 1 :(得分:1)
首先,您没有正确定义您的关系。应该是这样的:
class Product extends Model
{
public function category()
{
return $this->belongsTo('App\Category');
}
}
class Category extends Model
{
public function products()
{
return $this->hasMany('App\Product');
}
}
然后在产品迁移文件中,cat_id
应该重命名为category_id
。这样,您无需在关系上指定外键。
我假设您要列出属于特定类别的所有产品。您可以使用路由模型绑定轻松地做到这一点。在这种情况下,您的路线应类似于:
Route::get('categories/{category:id}/products', [CategoryController::class, 'products']);
然后在您的控制器中:
use App\Category;
class CategoryController extends Controller
{
public function products(Category $category)
{
$category->load('products');
return view('products')->withCategory($category);
}
}
您可以通过以下方式在刀片视图中访问产品列表:$category->products
答案 2 :(得分:0)
您需要对Category
模型进行调整,因为Category
有很多Products
。就目前而言,关系的命名并不能反映出这一点
class Category extends Model
{
public function products()
{
return $this->hasMany('App\Product');
}
}
然后您可以像这样通过Category
模型访问产品。
$categories = Category::with('products')->all();
$categories->each(function($category) {
$products = $category->products;
// Dump & Die a collection of products
dd($products);
});
注意:我渴望使用with()
方法加载该关系,这只是为了防止n+1
查询。有关急切和延迟加载的更多信息,请参见文档。
答案 3 :(得分:0)
您可以做类似的事情,
$products = product::with('categories')->get();
foreach($products as $product)
{
foreach($product->categories as $category)
{
echo $category->name;
}
}
$categories = Category::with('products')->get();
$category = Category::with('products')->find($category_id);