【问题标题】:Not Getting correct model record from query on Eloquent RelationShip Laravel 5.1未从 Eloquent RelationShip Laravel 5.1 上的查询中获取正确的模型记录
【发布时间】:2016-08-05 02:22:50
【问题描述】:

我正在开发一个 5.1 Laravel 应用程序。在该应用程序中,我在业务模型(西班牙语中名为 Negocio)和用户模型之间建立了 OnetoMany 关系。当我注册我的用户时,我会创建一个令牌并将其保存在用户表中,以便在电子邮件确认中使用该令牌....

当用户确认它的帐户时,我想使用控制器收到的令牌创建一个目录,然后在“public/negocios/”中使用与用户相关的企业名称。

所以在我的控制器中我有用户确认:

public function emailConfirm($token)
{
    try {
        //getting the bussiness that user is related to
        $negocio = App\Negocio::with(['user' => function ($query) use($token) {
            $query->where('token', 'like', $token);
        }])->firstOrFail()->folderProfile();

        //Then we activate the user account
        $user = User::whereToken($token)->firstOrFail()->confirmEmail();
        return redirect('/login')->with('mensaje', '¡Su cuenta de usuario se ha activado, ahora puede iniciar sesión en su cuenta!');

    } catch (ModelNotFoundException $e) {
        return redirect('/login')->with('mensaje', 'Ya se ha confirmado a este usuario, solo inicie sesión ¬¬');
    }
}

在我的业务模型中,我具有创建目录的功能,该目录将业务名称作为目录名称:

public function folderProfile()
{
    $name = str_replace(' ', '', $this->nombre_negocio);
    $ruta_a_public = public_path().'/negocios/';
    $ruta_a_public .= $name;
    File::makeDirectory($ruta_a_public, $mode = 0777, true, true);
}

问题是在php artisan tinker 上探测代码,Eloquent 将我所有的业务记录在数据库中,而不仅仅是用户相关的业务。

如果您能告诉我使我的“查询”按预期工作的最佳方法,我们将不胜感激

失败的部分是(在商业模式上):

$negocio = App\Negocio::with(['user' => function ($query) use($token) {
            $query->where('token', 'like', $token);
        }])->firstOrFail()->folderProfile();

我是根据我在https://laravel.com/docs/5.1/eloquent-relationships#querying-relations 中读到的内容编写的

编辑: 在我的 Negocio 模型中(西班牙语中的商业模型):

public function user()
{
  return $this->hasMany('App\User', 'negocio', 'codigo_negocio');
}

在用户模型中我有这个:

public function negocio()
{
  return $this->belongsTo('App\Negocio', 'negocio', 'codigo_negocio');
}

谢谢大家...抱歉这篇长篇文章。

【问题讨论】:

  • 您好 Elotgamu,请您更新您的帖子,为我们提供有关您的表的简单描述,以了解用户表和业务模型之间的真实关系?
  • 已编辑...添加了我在用户和业务模型之间的模型关系

标签: php laravel eloquent


【解决方案1】:

这是一个不同的逻辑,但可能有帮助:

//First search for the user
$user = App\User::where('token','like','%' . $token . '%')->first();

//Then search for the business
$negocio = App\Negocio::where('user_id',$user->id)->first();

不要忘记定义表之间的关系。

顺便说一句,你忘了这里的 %:

$query->where('token', 'like', '%' . $token . '%');

如果您正在寻找完全相同的令牌,您应该使用这个:

Something::where('token',$token)

【讨论】:

  • 是的,我也有这种方法,但问题是我的商业模式很多用户..并且用户属于一个企业。
  • 所以我无法获得业务。我尝试按照您的建议寻找确切的令牌,但我得到了一个包含所有记录业务的 Eloquent 收藏。所以问题仍然存在...感谢您的回答和帮助我... :)
  • 我认为您的模型不正确,这是文档中的一个示例:$this->hasMany('App\Something', 'foreign_key', 'local_key') 如果您使用 first() 或 find() 方法,您将无法获得多个业务。
  • 嗯,我不这么认为。好吧,我想我在文档上遵循了 laravel 的模型指南,因为 negocio 是我在用户表上的外键,它引用了 negocio PK(即 codigo_negocio)......而且我得到了与我的 negocio 模型相关的所有用户。而且我也无法达到现实的反面。所以,请告诉我我在哪里没有在模型上使用 laravel 方式...感谢@szebasztian 的所有帮助...
【解决方案2】:

进一步阅读 laravel 文档https://laravel.com/docs/5.1/eloquent-relationships 对我有用并获得预期记录的方式是:

$negocio = App\Negocio::whereHas('user', function ($query) use($token) {
            $query->where('token', $token);
        })->firstOrFail()->folderProfile();

通过这种方式,我获得了与令牌所有者相关的正确业务...... 感谢@szebasztian 为我提供了一些关于在 eloquent ORM 上构建查询的新技巧。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 2014-03-07
    • 1970-01-01
    相关资源
    最近更新 更多