mysql - 此集合实例上不存在属性 [日期]

标签 mysql laravel-5.4

我试图通过 Eloquent 关系在我的 Blade View 中传递日期,但出现上述错误。我一直无法找到与此案例类似的问题。这是我到目前为止所做的,我有以下模型

产品

class Product extends Model
{
protected $guarded = ['id'];

public function prices()
{
    return $this->hasMany(Price::class, 'product_id');
}
}

价格

class Price extends Model
{
protected $guarded = ['id'];

public function product()
{
    return $this->belongsTo(Product::class, 'product_id');
}
}

我像这样在我的 Controller 中传递数据

public function index()
{
$products = Product::with('prices')->orderBy('id')->get();

$prices = Price::all()->groupBy('date');

return view('home', compact('products', 'prices'));
}

这是我的 View 代码

@foreach($prices as $price)
<div class="panel panel-default">
    <div class="panel-heading">
        {{ $price->date }}
    </div>

    <table class="table table-striped">
        <thead>
            <tr>
                <th>Product</th>
                <th>Price</th>
                <th>Difference</th>
                <th>Percentage</th>
            </tr>
        </thead>
        <tbody>
            <tr>
            @foreach($price->product as $product)    
                <td>{{ $product->name }}</td>
            @endforeach
                <td>{{ $price->cost }}</td>
                <td>0</td>
                <td>0</td>
            </tr>
        </tbody>
    </table>
</div>
@endforeach

我错过了什么以及如何让我的代码工作?

最佳答案

例如将日期属性名称更新为 date_product。 让我知道它是否有效:)

关于mysql - 此集合实例上不存在属性 [日期],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45616448/

相关文章:

sql - 由于 ORDER BY 未使用索引导致 SQL 查询缓慢

laravel - 使用 Laravel 5.4 模拟邮件(黄昏)

javascript - 从 Laravel 设置 cookie 并从 javascript 获取 null

php - 如何为路由组中的所有请求设置 header

php - Laravel 5.4 迁移 :generate fails with Way\Generators\Filesystem\FileNotFound error

mysql - 使用行合并进行聚合

php - 我需要安装php5-mysql吗

php - Symfony2 表单集合外键

mysql - 关于优化多个联接的方法的建议。使用数据库触发器使用另一个表来编译数据是不好的做法吗?

php - Laravel Socialite - 在重定向之前保存 URL