如何使用 Eloquent ORM 从 Laravel 中的数据库检索数据
How do I use Eloquent ORM to retrieve data from a database in Laravel
我是MVC新手,不是很懂。我有一个名为 products
的 table 数据库
我有一个名为 Product
的模型,看起来像
<?php
class Product extends Eloquent {
// MASS ASSIGNMENT -------------------------------------------------------
// define which attributes are mass assignable (for security)
// we only want these 3 attributes able to be filled
protected $fillable = array('type', 'brand', 'image');
}
我有这样的路线
Route::get('products', function()
{
return View::make('products');
});
我的视图看起来像这样
@extends('master')
@section('content')
PRODUCTS
@foreach($products as $product)
{{ $product->type }}
@endforeach
@stop
我看到您可以像这样获取所有行 $products = Product::all();
但我不明白这是怎么回事。它进入视图了吗?它会进入模型吗?它在路线上吗?我当前的 @foreach 循环只会导致 undefined variable: products
有人可以澄清一下吗?
这应该让你现在开始:
Route::get('products', function()
{
$products = Product::all();
return View::make('products')->with('products', $products);
});
我是MVC新手,不是很懂。我有一个名为 products
我有一个名为 Product
的模型,看起来像
<?php
class Product extends Eloquent {
// MASS ASSIGNMENT -------------------------------------------------------
// define which attributes are mass assignable (for security)
// we only want these 3 attributes able to be filled
protected $fillable = array('type', 'brand', 'image');
}
我有这样的路线
Route::get('products', function()
{
return View::make('products');
});
我的视图看起来像这样
@extends('master')
@section('content')
PRODUCTS
@foreach($products as $product)
{{ $product->type }}
@endforeach
@stop
我看到您可以像这样获取所有行 $products = Product::all();
但我不明白这是怎么回事。它进入视图了吗?它会进入模型吗?它在路线上吗?我当前的 @foreach 循环只会导致 undefined variable: products
有人可以澄清一下吗?
这应该让你现在开始:
Route::get('products', function()
{
$products = Product::all();
return View::make('products')->with('products', $products);
});