问题描述
一旦客户在产品页面内单击按订单订购",我将尝试按价格订购我的产品.我不断收到下一个错误:未定义的索引:标题(视图:C:\ xampp \ htdocs \ eshop \ resources \ views \ content \ item.blade.php).我的"item.blade.php"页面在单独的页面中显示了较大尺寸的产品.我认为问题出在我的控制器Item函数或我的模型getItem函数内部,我可能是错的...如果您能帮助我,将不胜感激.
I am trying to order my products by price once the client click order by inside the product page. I keep getting the next error: Undefined index: title (View: C:\xampp\htdocs\eshop\resources\views\content\item.blade.php) .My 'item.blade.php' is the page that shows the larger size of the product in separate page. I think the issue is inside my controller Item function or im my model getItem function , I might be wrong... would appreciate if you could help me out .Thanks
我的路线:
Route::get('shop/{category_url}/sorting-{sort?}', 'ShopController@products');
我对content.products的看法:
My view in content.products:
@if($products)
<br><br>
<a href=" {{ url('shop/'.$category['url'].'/sorting-asc')}}" style="color:black"> High to low</a> |
<a href=" {{ url('shop/'.$category['url'].'/sorting-desc')}}" style="color:black">Low to high</a>
我的item.blade.php:
My item.blade.php:
@extends ('master')
@section('content')
<div class="row">
<div class="col-md-12 text-center">
@if('item')
<h1>{{ $item['title']}}</h1>
<p><img width="500" src="{{ asset ('images/' . $item['image'])}}" </p>
<p>{!! $item['article'] !!}</p>
<p><b>Price on site:</b>{{ $item['price']}}$</p>
<p>
@if(Cart::get($item['id']))
<input disabled="disabled" type="button" value="In Cart!" class="btn btn-success">
@else
<input data-id="{{ $item['id']}}" type="button" value="+ Add to cart" class="btn btn-success add-to-cart">
@endif
<a href="{{ url('shop/checkout') }}" class="btn btn-primary">Checkout</a>
</p>
@else
<p class="text-center" style="font-size: 18px">No product details ...</p>
@endif
</p>
@endsection
我的控制器:
public function products(Request $request, $category_url, $sort= 'ASC'){
Product::getProducts($category_url, self:: $data);
$catsort = Categorie::where('url', '=', $category_url)->first();
$products = Product::where('categorie_id', $catsort->id)->orderBy('price', $sort)->get();
return view('content.products', self::$data ,['products' => $products, 'sort' => $sort]);
}
public function item($category_url, $product_url){
Product::getItem($product_url, self::$data);
return view('content.item', self::$data);
}
我的模特:
static public function getProducts($category_url, &$data){
$data['products']=$data['category']=[];
if ($category=Categorie::where('url','=', $category_url)->first()){
$category= $category->toArray();
$data['category']=$category;
$data['title']=$data['title']. ' | ' . $category['title'];
if ($products=Categorie::find( $category['id'])->products){
$data['products']= $products->toArray();
}
}
}
static public function getItem($product_url, &$data) {
$data['item'] = [];
if ($product = Product::where('url', '=', $product_url)->first()) {
$product = $product->toArray();
$data['item'] = $product;
$data['title'] .= '|' . $product['title'];
}
}
推荐答案
因此,基于聊天,我将尝试提供答案.
So based on the chat, I'm going to try and provide an answer.
首先,我们需要了解如何构造数据库.
To start, we need to understand how you want to structure your database.
我可以肯定地看到您拥有产品和产品;分类.我仍然不清楚您要使用$ items实现什么.
I can see for sure you have Products & Categories. I'm still not clear what you're trying to achieve with the $items.
因此,从此开始,您将有两个表.让我们仔细考虑一下,以确定他们之间的关系.首先,请问一个问题,一个产品可以有多少个类别(1个或大于1个)?大多数人会组织类别,因此产品可以具有多个类别,因此在Laravel中,这被称为HasMany关系.相反的是一个类别可以有多少种产品?".在这种情况下,答案还是大于1.因此,这实际上是BelongsToMany关系,而不是HasMany关系.一个类别可以有很多产品,而一个产品可以有很多类别.
So, starting with this, you have two tables. Lets think this through to determine what relationship they have. To start, ask the question, how many categories can a product have (1 or more than 1)? Most people structure categories so products can have more than one, so in Laravel, this is known as a HasMany relationship. The inverse of this is "how many products can a category have?". The answer, once again in this case, is more than 1. Therefore, instead of a HasMany relationship, this is actually a BelongsToMany relationship. A category can have many products, and a product can have many categories.
接下来,您需要创建数据透视表.该表将存储产品的ID和具有关联关系的类别的ID.您可能会创建一些看起来像这样的迁移.每个人都应使用"up"方法将其放入自己的迁移文件中.
Next, you need to create a pivot table. This is a table that will store the ID of the product, and the ID of the category that have a relationship. You might create some migrations that look like this. Each of them should go in it's own migration file in the "up" method.
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('slug', 100)->index();
$table->integer('price');
$table->timestamps();
});
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('slug', 100)->index();
$table->timestamps();
});
Schema::create('category_product', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->index();
$table->integer('product_id')->index();
$table->timestamps();
});
接下来,我们需要设置模型以接受关系.因此,在您的Product.php类中,添加以下方法.
Next we need to setup the models to accept the relationship. So, in your Product.php class, add the following method.
public function categories(){
return $this->belongsToMany(Category::class);
}
在Category.php类中与此相反.
And the inverse of this in the Category.php class.
public function products(){
return $this->belongsToMany(Product::class);
}
现在,您应该可以像这样访问您的关系
Now, you should be able to access your relationships like so
$category = Category::first();
$products = $category->products;
好的,现在让它开始工作.您的路线看起来不错,尽管我不会按照您的方式添加排序.您可以将其作为GET数据发送,例如 http://example.com/shop/test-category?sort = ASC
Okay, now lets get this working. Your route looks fine, though I wouldn't add the sort the way you are. You can send this in as GET data like so http://example.com/shop/test-category?sort=ASC
Route::get('shop/{category_url}', 'ShopController@products');
好的,现在是您的控制器.
Okay, so now your controller.
public function products(Request $request, $category_url){
$category = Category::with(['products' => function($q){
$q->orderBy('price', request()->get('sort')??"ASC");
}])->whereSlug($category_url)->first(); // Eager load in products
return view('content.products', ['category' => $category]);
}
最后,现在您可以在刀片中使用对象语法而不是数组语法.
Lastly, within your blade you can now use object syntax instead of array syntax.
@section('content')
Category Name: {{$category->name}} <br />
@foreach($category->products as $product)
Product Name: {{$product->name}} <br />
Product Price: {{$product->price}} <br />
Product Name: {{$product->name}} <br />
@endforeach
@endsection
由于看不到更多,我无法更加具体,因为我不清楚您如何尝试处理$ items.您应该以与设置产品和类别相同的方式进行设置.考虑一下这种关系,然后确定是"HasMany","HasOne"还是"BelongsToMany".
Without seeing more, I can't be more specific as I'm not clear on how you're trying to handle your $items. You should set it up in the same way we setup products and categories though. Think about the relationship and determine if it's a "HasMany", "HasOne", or "BelongsToMany".
此外,您应尝试遵循默认的命名约定.您的数据库表应为小写和复数形式.因此,表名称应完全是产品"和类别".您的模型应该是单数. 产品"和类别".无需将其命名为"Categorie".然后,对于所有数据透视表,您都希望使用以下"category_product"将其命名.因此,您要处理的两个表的单数和字母顺序.最后,在数据库中,您需要确保数据透视表的列名为"product_id"& "category_id",因此数据库名称的单数形式+"_id".
Also, there are default naming conventions you should try and follow. Your database tables should be lowercase and plural. So the table names should be exactly "products" and "categories". Your models should be singular. "Product" & "Category". No need to name it "Categorie". Then, for all pivot tables, you want to name them like the following "category_product". So singular and alphabetical order with the two tables you are pivoting on. Lastly, in your database, you'll want to make sure your pivot table columns are named "product_id" & "category_id", so the singular version of the database name + "_id".
我希望这足以使您前进.
I hope that's enough to get you going.
这篇关于Laravel Orderby的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!