2

In my app I have Event and User models.Because of Event model I have to put it into namespace .so I created namespace as of following.

Event

<?php namespace App\Models;

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

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');
   }

The relations between User and Event are simple OneToMany. So in my EventController I use POST method to create new event resource .

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

At the same time i got an error .

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

If I am not wrong i guess it is namespace error.But namespaces are already declared correctly I guess. But I try visiting similar questions and used alternative way in relationship and then it worked fine.Personally I don't find it satisfied.Any idea why this is occurred ?
Change the above relation to

public function events()
    {
        return $this->hasMany('\App\Models\Event');
    }
Tixeon
  • 930
  • 1
  • 12
  • 27

1 Answers1

0

If you put Event inside a namespace you need to use it with it's fully qualified classname.

You can either import it into your controller:

use App\Models\Event;

Or specify the namespace inline:

new App\Models\Event();

Also, I would recommend that if you create a namespace for models that you put all of them in there. (User is missing a namespace in your code)

lukasgeiter
  • 147,337
  • 26
  • 332
  • 270