2

私のアプリには、イベント モデルとユーザー モデルがあります。イベント モデルのため、名前空間に配置する必要があります。次のように名前空間を作成しました。

イベント

<?php namespace App\Models;

class Event extends \Eloquent {
   public function user()
   {
     return $this->belongsTo('User');
   }

ユーザー

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
use App\Models\Event;

class User extends Eloquent implements UserInterface, RemindableInterface {
   public function events()
   {
     return $this->hasMany('Event');
   }

User と Event の関係は単純な OneToMany です。したがって、私のEventControllerでは、 POST メソッドを使用して新しいイベント リソースを作成します。

$e = new Event(array('keys'=>'values')); //without user_id filled
$user->events()->save($e);

同時にエラーが発生しました。

Call to undefined method Illuminate\Support\Facades\Event::newQuery()

私が間違っていなければ、名前空間エラーだと思います。しかし、名前空間は既に正しく宣言されていると思います。しかし、私は同様の質問にアクセスして、別の方法で関係を築こうとしましたが、うまくいきました.個人的には満足していません.なぜこれが起こったのですか?
上記の関係を次のように変更します

public function events()
    {
        return $this->hasMany('\App\Models\Event');
    }
4

1 に答える 1