【问题标题】:Laravel Eloquent one-to-many relationshipLaravel Eloquent 一对多关系
【发布时间】:2013-02-09 19:39:38
【问题描述】:

我在让 Laravel 的 Eloquent ORM 返回关系数据时遇到了真正的问题。

我的迁移(数据库架构):

Schema::table('users', function($table)
{
    $table->engine = 'InnoDB';
    $table->create();

    $table->increments('id')->unsigned();
    $table->string('firstname');
    $table->string('surname');
    $table->string('email')->unique();
    $table->string('password')->unique();
    $table->string('phone')->nullable();
    $table->text('about')->nullable();
    $table->timestamps();
});

Schema::table('files', function($table)
{
    $table->engine = 'InnoDB';
    $table->create();

    $table->increments('id')->unsigned();
    $table->string('filename');
    $table->string('title');
    $table->text('description')->nullable();
    $table->string('keywords')->nullable();
    $table->integer('category_id')->unsigned();
    $table->integer('file_type_id')->unsigned();
    $table->integer('user_id')->unsigned();
    $table->timestamps();
});

型号

文件.php

<?php

class File extends Eloquent
{

    public static $timestamps = true;

    public function user()
    {
        return $this->belongs_to('User');
    }

}

用户.php

<?php

class User extends Eloquent
{
    public static $timestamps = true;

    public function files()
    {
        return $this->has_many('File');
    }
}

routes.php

// everything else in my routes.php is as it was when downloaded
Route::get('users', function()
{
    echo '<pre>';

    // this works
    $users = User::all();
    print_r($users);

    // this doesn't work
    print_r($users->files);

    // this doesn't work
    $files = User::find(1)->files;
    print_r($files);
});

错误:

未处理的异常

消息:

试图获取非对象的属性 地点:

C:\wamp\www\l3_mlib\application\routes.php 在第 51 行 堆栈跟踪:

#0 C:\wamp\www\l3_mlib\laravel\laravel.php(42): Laravel\Error::native(8, '尝试获取 p...', 'C:\wamp\www\ l3_...', 51) #1 C:\wamp\www\l3_mlib\application\routes.php(51): Laravel{closure}(8, '尝试获取 p...', 'C:\wamp\www\l3_...' , 51, 数组) #2 [内部函数]: {closure}() #3 C:\wamp\www\l3_mlib\laravel\routing\route.php(163): call_user_func_array(Object(Closure), Array) #4 C:\wamp\www\l3_mlib\laravel\routing\route.php(124): Laravel\Routing\Route->response() #5 C:\wamp\www\l3_mlib\laravel\laravel.php(167): Laravel\Routing\Route->call() #6 C:\wamp\www\l3_mlib\public\index.php(34): require('C:\wamp\www\l3_...') #7 {主}

我做错了什么?

【问题讨论】:

  • 原来你不能有一个名为 File 的模型,因为它与内部 Laravel 模块冲突。我将File 模型和表格重命名为Upload,现在一切正常。
  • 我昨天用 'Event' 作为模型名称做了这个,没有意识到它是保留的 :)
  • 在任何地方都有 Laravel 的保留字列表吗?
  • Hmm.. 我还没有找到,但是查看 API 文档中的命名空间和类列表是一个好的开始:laravel.com/api - 您还需要了解名称您正在使用的任何捆绑包和库。

标签: laravel one-to-many relationship eloquent laravel-3


【解决方案1】:

我认为问题出在你的迁移架构上,你没有设置外键,所以 laravel 在使用时无法获取对象数据

 $files = User::find(1)->files;

这就是为什么它返回错误:尝试获取非对象位置的属性

将此添加到您的文件迁移中,看看它是否有效

 $table->integer('user_id')->unsigned()->unique('user_id', 'files_user_id');
 $table->foreign('user_id')->references('id')->on('users');

你可以使用命名空间来避免类名冲突

【讨论】:

    【解决方案2】:

    我对一个名为 Task 的模型也有同样的问题,它也是一个 Laravel 类。

    在 application/config/application.php 中有一些删除命名空间的类别名。你可以注释掉文件别名,这样就不会和你的冲突了。

    如果你需要在任何时候使用 Laravel File 类,你可以通过它的命名空间名称来引用它,例如

    Laravel\\File::get('path/to/file');

    希望这会有所帮助,

    【讨论】:

    • 对于 SEO:我遇到了同样的问题,当我使用名为 Response 的类时,给了我Call to undefined method Illuminate\Support\Facades\Response::all()
    猜你喜欢
    • 1970-01-01
    • 2021-04-21
    • 2019-06-13
    • 2017-04-20
    • 2019-03-26
    • 1970-01-01
    • 2013-05-27
    • 2013-05-15
    • 2016-11-03
    相关资源
    最近更新 更多