与Laravel 5的关系冲突

时间:2016-11-03 14:48:49

标签: laravel-5 eloquent

我希望在将记录保存到“课时”之后发送电子邮件。表。然而,我的行为非常奇怪。 在我的测试页面中,我发现错误(试图获取非对象的属性)似乎是基于我在“包裹”中的项目数量而发生的。表。如果我有两个包,MessageSent事件将触发一次发送电子邮件。之后,错误开始。 (有时错误是'调用成员函数getFullName(lessonhours_id)'出现。)如果我有4个包,我可以发送3封电子邮件等... 除了在MessageSent事件触发时插入的日志文件,记录也会插入到数据库中。似乎存在某种关系故障,因为在创建了几条记录之后,相关属性似乎是不可见的。我与Packages和Lessonhours有1对多的关系。当我使用' Tinker'时,我可以看到这种关系有效。任何见解都将不胜感激。

修补码:

$ php artisan tinker
Psy Shell v0.7.2 (PHP 5.5.9-1ubuntu4.19 — cli) by Justin Hileman
>>> $les = App\Lessonhours::first();
=> App\Lessonhours {#662
     id: 1,
     created_at: "2016-11-03 13:16:42",
     updated_at: "2016-11-03 13:16:42",
     players_id: 1,
     packages_id: 1,
     signup_date: "2016-09-06",
   }
>>> $les = App\Lessonhours::first();
=> App\Lessonhours {#664
     id: 1,
     created_at: "2016-11-03 13:16:42",
     updated_at: "2016-11-03 13:16:42",
     players_id: 1,
     packages_id: 1,
     signup_date: "2016-09-06",
   }
>>> $les->players->getFullName($les->players_id)
=> "Girltest Testfam"
>>> $les->packages->name
=> "04 Pack Pvt"
>>> 

Lessonhours模型:

  <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Lessonhours extends Model
{
    protected $fillable = array('signup_date', 'players_id', 'packages_id');

    public $table = "lessonhours";

    public function players()
    {
        return $this->belongsTo('App\Players', 'id');
    }

    public function packages()
    {
        return $this->belongsTo('App\Packages', 'id');
    }

    public function hoursused()
    {
       return $this->hasMany('App\Hoursused', 'lessonhours_id');
    }

    protected $dates = ['signup_date'];


}

包模型:

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Packages extends Model
{
    public $table = "packages";
    protected $fillable = ['name', 'cost', 'numberofhours', 'type'];

    public function lessonhours()
    {
        return $this->hasMany('App\Lessonhours', 'packages_id');
    }
}

事件MessageSent:

   use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Lessonhours;

class MessageSent extends Event
{
    use SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Lessonhours $lessonhours)
    {

        $this->lessonhours = $lessonhours; 
        $this->player = $lessonhours->players->getFullName($lessonhours-   >players_id);
        $this->email = $lessonhours->players->family->email;
        $this->package = $lessonhours->packages->name;
    }

监听器:

 public function handle(MessageSent $event)
{
    $lessonhours = $event->lessonhours;
    $player = $event->player;
    $email = $event->email;
    $package = $event->package;

    Mail::send('admin.email.lessonhoursnotification', ['name' => $lessonhours], function($message) use ($player, $email, $package){

        $message->from('test@test.com', 'Lesson Mgmt');
        $message->to($email, $player);
        $message->subject('Lesson Package Update');

    });
}

First Insert with Two Packages

Error thrown on the new record

1 个答案:

答案 0 :(得分:0)

问题出现在您的Lessonhours模型中。 belongsTo()的第二个参数应该是引用当前表中父键的列。所以它应该是:

public function players()
{
    return $this->belongsTo('App\Players', 'players_id');
}

public function packages()
{
    return $this->belongsTo('App\Packages', 'packages_id');
}

Docs