小编plu*_*iam的帖子

Laravel雄辩 - 在连接表时防止覆盖值

好的,所以我有以下两种型号:AccountRole

class Account extends Eloquent
{
  protected $table = 'account';

  /* [...] */

  public function group() {
    return $this->belongsTo('Group');
  }
}
Run Code Online (Sandbox Code Playgroud)

class Role extends Eloquent {

  protected $table = 'role';

  public function accounts() {
    return $this->hasMany('Account');
  }

}
Run Code Online (Sandbox Code Playgroud)

和数据库表:accountrole

account
-------
id
name
role_id (nullable)

role
----
id
name
Run Code Online (Sandbox Code Playgroud)

而现在的问题是:

我需要accountsrole.name列排序.但是在join(或leftJoin)之后,第二个表中的值会覆盖这些值.这是一些代码:

$response = Account::with('role')->leftJoin('group', 'group.id', '=', 'account.group_id')->get();
Run Code Online (Sandbox Code Playgroud)

之后,在eloquent集合中的值idname不正确.

此外,我需要返回为eloquent类型模型,因为我在JSON中返回响应,重要的是稍后在JS(解析JSON之后)我可以做到account.role.name.

更改表中字段的名称(例如:id - > account_id和:id …

php join laravel eloquent

13
推荐指数
2
解决办法
7988
查看次数

将root重定向到rails 4中的命名路由

我正在尝试将我的www.example-app.comroot 重定向到www.example-app.com/app/dashboard使用routes.rb.目前我正是这样做的:

root to: redirect('/app/dashboard')
Run Code Online (Sandbox Code Playgroud)

但是想使用命名路由来做,例如:

get 'app/dashboard' => 'accounts#dashboard', as: :account_dashboard
Run Code Online (Sandbox Code Playgroud)

但是当我把它放在路线中时:

root to: redirect(account_dashboard_url)
Run Code Online (Sandbox Code Playgroud)

......当然它不起作用,我怎么能这样做?

redirect routes ruby-on-rails-4

7
推荐指数
1
解决办法
5510
查看次数

标签 统计

eloquent ×1

join ×1

laravel ×1

php ×1

redirect ×1

routes ×1

ruby-on-rails-4 ×1