【发布时间】: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,请您更新您的帖子,为我们提供有关您的表的简单描述,以了解用户表和业务模型之间的真实关系?
-
已编辑...添加了我在用户和业务模型之间的模型关系