【问题标题】:Using Eloquent to create a new conversation使用 Eloquent 创建新对话
【发布时间】:2013-06-22 11:01:13
【问题描述】:

我正在使用Laravel 框架为我的应用程序编写一个对话系统。如果我在数据库中对对话进行了硬编码,我目前可以在应用程序中进行对话。

现在,我不太清楚如何在Laravel 中实际创建一个新的Conversation。创建新的Conversation 时,我需要向conversations 表和用户以及conversation_id 添加一个新条目到conversation_user

我该怎么做?以下是我目前拥有的。

我有 3 个数据库表

对话:

id |

conversation_user:

id | conversation_id | user_id

消息:

id | message | user_id | conversation_id | created_at

我有 2 个模型

对话

class Conversation extends Eloquent {
    public $table = 'conversations';

    public $includes = array('User', 'Message');

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

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

留言

class Message extends Eloquent {
    public $table = 'messages';

    public $includes = array('User', 'Conversation');

    protected $fillable = array('message', 'user_id', 'conversation_id');

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

    public function conversation() {
        return $this->belongsTo('Conversation');
    }
}

【问题讨论】:

    标签: laravel laravel-4 eloquent


    【解决方案1】:

    这成功了

    $conversation = Conversation::create(array());
    $conversation->user()->attach(Sentry::getUser()->id);
    $conversation->user()->attach($id);
    $conversation->save();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 2013-12-17
      • 2012-04-03
      • 2018-07-18
      • 1970-01-01
      相关资源
      最近更新 更多