小编Tho*_*son的帖子

Laravel ORM友谊关系没有重复

用雄辩的方式塑造友谊关系的最佳方式是什么?我的表架构在下面,我想定义一个关系,我可以检索所有的朋友,如下所示.

<?php

class User extends Eloquent {

public function friends() {
    return $this->belongsToMany('User', 'friendships', 'user_id', 'friend_id')->orWhere($this->id,'=', 'friend_id');
  }
}

+----+---------+-----------+----------+---------------------+---------------------+
| id | user_id | friend_id | state    | created_at          | updated_at          |
+----+---------+-----------+----------+---------------------+---------------------+
|  1 |       3 |         1 | accepted | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
|  2 |       2 |         3 | accepted | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
+----+---------+-----------+----------+---------------------+---------------------+
Run Code Online (Sandbox Code Playgroud)

当我寻找用户ID为3的朋友时,上面的关系接近工作我得到了用户1和3,但显然我想要1和2.

友谊表

user_id:请求友情
friend_id的用户ID:目标朋友
状态的用户ID :友谊是待处理,被接受还是被阻止.
created_at和updated_at

我知道有很多来自Laravel的解决方案许多自我引用表只能以一种方式工作,我可以从关系的两边检索朋友,但我必须是两行,例如,如果用户1和3是朋友,那么在一行user_id = …

php orm laravel laravel-4

8
推荐指数
2
解决办法
2378
查看次数

在Laravel 4中更改用户时区的最佳方法是什么?

目前我已将用户时区保存在其数据库行中,每次打印日期时,我都会将其转换为用户的时区.我怎么能干这样干?

我应该覆盖Eloquent返回Carbon DateTime对象的位置.如果是这样,我应该把它放在下面的特征中,所以我只需要写一次?

<?php

use Carbon\Carbon;
use Illuminate\Database\Eloquent;

trait ConvertTimeZone {
/**
 * Return a timestamp as DateTime object.
 *
 * @param  mixed  $value
 * @return DateTime
 */
protected function asDateTime($value)
{
    // If this value is an integer, we will assume it is a UNIX timestamp's value
    // and format a Carbon object from this timestamp. This allows flexibility
    // when defining your date fields as they might be UNIX timestamps here.
    if (is_numeric($value))
    {
        return Carbon::createFromTimestamp($value);
    }

    // …
Run Code Online (Sandbox Code Playgroud)

php laravel

5
推荐指数
1
解决办法
4181
查看次数

标签 统计

laravel ×2

php ×2

laravel-4 ×1

orm ×1