相关疑难解决方法(0)

Laravel model relationships and model events

I am building a notification system at the moment, and the notifications get delivered via model events. Some of the notifications are dependent on things happening with the models relationships.

Example: I have a project model that has a one:many relationship with users,

public function projectmanager() {
    return $this->belongsToMany('User', 'project_managers');
}
Run Code Online (Sandbox Code Playgroud)

I am wanting to monitor for changes on this relationship in my project model events. At the moment, I am doing this by doing this following,

$dirtyAttributes = $project->getDirty(); …
Run Code Online (Sandbox Code Playgroud)

php relational-database laravel eloquent

4
推荐指数
1
解决办法
3766
查看次数

Laravel如何更新一对一关系?

条款表:

  • term_id
  • 名称
  • ug

Term_taxonomy表:

  • term_taxonomy_id
  • term_id
  • 描述

我的学期模型:

public function TermTaxonomy(){
    return $this->hasOne('TermTaxonomy');
}
Run Code Online (Sandbox Code Playgroud)

我的TermTaxonomy模型:

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

我的类别控制器:

public function update($id){
    echo "$id"; // echo success
    echo $data['name']; // it should update name field in term table
    echo $data['slug']; // it should update slug field in term table
    echo $data['TermTaxonomy']['description']; // it should update description field in termtaxonomy table
}
Run Code Online (Sandbox Code Playgroud)

如何更新一对一关系?也许与push()

谢谢,对不起,我是新来的laravel。

php class laravel laravel-4

3
推荐指数
2
解决办法
7388
查看次数

Ardent + Laravel,自动水合物关系

我用lavarel和热情的包装.

当我想要更新一行时,我遇到了一些问题.

我有2个模型客户端和地址相关的morphone关系.

这种关系很好,当我想得到一个客户端这一行返回预期的结果:

Client::with('address')->find($id);
Run Code Online (Sandbox Code Playgroud)

但我无法理解如何使用干净的解决方案更新客户端.有人可以回答这些问题:

  1. 有了Ardent你怎么能自动修复相关模型?
  2. 当您更新一些数据时,lavarel的最佳做法是什么?使用更新方法吗?使用保存?用推?填写所有型号?使用自动水合物?

当我在我的更新方法中记录Input :: all()时,我得到了:

[2014-05-31 15:52:56] production.INFO: {"id":983,"firstName":"Susanne","lastName":"Adam","birthDate":"18\/06\/1982","inscriptionDate":"08\/09\/2013","status":3,"created_at":"2014-05-31 14:26:25","updated_at":"2014-05-31 14:26:25","email":"bernard.alix@free.fr","address":{"id":983,"address":"avenue Etienne","address2":"","ville":"Cordierboeuf","cp":"25 10","phone":"0403983157","mobile":"+33 (0)3 0","addressable_id":983,"addressable_type":"Client","created_at":"2014-05-31 14:27:58","updated_at":"2014-05-31 14:27:58"}} [] []
Run Code Online (Sandbox Code Playgroud)

如您所见,地址数据位于客户端数据中.

3.当我使用更新,保存或推送(eloquent的方法)时,雄辩不明白他应该更新地址模型然后更新相关的客户端模型.我的数据格式不是很好吗?

谢谢.

更新:

当我执行Log :: info(Input :: all())时,我在控制器中获得以下json数据:

[2014-06-01 18:10:46] production.INFO: {"id":284,"firstName":"Andr\u00e9e","lastName":"Adam","birthDate":"23\/07\/1944","inscriptionDate":"22\/11\/2013","status":2,"created_at":"2014-06-01 15:41:22","updated_at":"2014-06-01 18:06:44","email":"monique17@normand.com","address":{"id":284,"streetAddress":"93, avenue Lefort","streetAddress2":"","city":"Boulay-sur-Leger","zipCode":"14054","phone":"09 51 03 1","mobile":"+33 6 00 6","addressable_id":284,"addressable_type":"Client","created_at":"2014-06-01 15:42:50","updated_at":"2014-06-01 18:06:44"}} [] []
Run Code Online (Sandbox Code Playgroud)

随着ardent的自动水化不起作用...客户端成功自动水合但地址不成功,可能是由于它们之间的多态关系(一对一).

我尝试用这种方式填充我的模型:

$client = Client::with('address')->find($id);
$client->update(Input::except('address'));
$client->address->update(Input::only('address'));
Run Code Online (Sandbox Code Playgroud)

但是这不起作用,因为Input :: only('address')给出错误的形成数据,当我记录这个时我得到了:

Log::info(Input::except('address'));
Log::info(Input::only('address'));

//output 

[2014-06-01 18:20:34] production.INFO: {"id":284,"firstName":"Andr\u00e9e","lastName":"Adam","birthDate":"23\/07\/1944","inscriptionDate":"22\/11\/2013","status":2,"created_at":"2014-06-01 15:41:22","updated_at":"2014-06-01 18:10:46","email":"monique17@normand.com"} [] []
[2014-06-01 18:20:34] production.INFO: {"address":{"id":284,"streetAddress":"93, avenue Lefort","streetAddress2":"","city":"Boulay-sur-Leger","zipCode":"14054","phone":"09 51 …
Run Code Online (Sandbox Code Playgroud)

php laravel eloquent ardent

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

标签 统计

laravel ×3

php ×3

eloquent ×2

ardent ×1

class ×1

laravel-4 ×1

relational-database ×1