我对Laravel很新.我正在想与Eloquent合作.
假设我有2个表:类别和产品.这两个表有一对多的关系.1类可以有很多产品.
我想返回一个JSON响应,该响应由一系列类别组成,其中每个类别都有一系列产品.它应该如下所示:
[
{"name":"Category 1", "products": [
{"name":"Product 1","price":"2.50"},
{"name":"Product 2","price":"2.50"}
]
},
{"name":"Category 2", "products": [
{"name":"Product 1","price":"1.95"},
{"name":"Product 2","price":"2.15"}
]
}
]
Run Code Online (Sandbox Code Playgroud)
楷模:
Category.php
<?php
class Category extends Eloquent{
protected $table = 'categories';
public function products(){
return $this->hasMany('Product');
}
}
Run Code Online (Sandbox Code Playgroud)
Product.php
<?php
class Product extends Eloquent {
protected $table = 'products';
public function categories(){
return $this->belongsTo('Category');
}
}
Run Code Online (Sandbox Code Playgroud)
数据库表:
create_categories_table:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration {
public function up()
{
Schema::create('categories', function(Blueprint $table) …Run Code Online (Sandbox Code Playgroud) 我最近使用 Laravel 4 创建了一个网站。我使用 XAMP 在 localhost 上测试我的网站,为了更容易,我在 vhosts 文件中创建了一个虚拟主机,该文件指向我的 Laravel 应用程序的公共文件夹。该网站运行良好。
现在我在 One.com 主机上租了一个网站空间。我用ftp打开了网站空间,当我进入“根”但没有任何文件夹所以我猜根位置是公共位置?我不确定。
Laravel 应用程序的正常结构是这样的:
--> app
--> bootstrap
--> public
--> vendor
--> other files
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为我认为 One.com 不使用公共 html 文件夹。我试图让它工作,但不幸的是。我将公共文件夹的内容移动到我的域的根目录,并将所有其他文件夹移动到名为 core 的文件夹中。然后我更改了一些配置文件 index.php 和 paths.php 但它仍然不起作用。
问题是我需要对我的文件夹结构进行哪些更改才能让它在这个 One.com 网络服务器上工作以及我需要调整哪些文件(.htaccess、paths.php)?我当然想保护我的私人文件夹。
提前致谢。