I'm trying to test my model relations using tinker but they're all giving errors
This is the code I'm trying
$event = \App\Event::all();
This is the error
PHP Fatal error: Call to undefined method App\Event::all() in
D:\Sites\nightshift2015\vendor\psy\psy
I have tried without the first \ but it's giving the same error
$event = App\Event::all();
This is my Event class
<?php namespace App;
class Event {
public function exhibitors() {
return $this->hasMany('App\Exhibitor');
}
public function conference() {
return $this->hasOne('App\Conference');
}
}
If that’s your event class, then it’s not extending Laravel’s base Model class, therefore hasn’t inherited any of its methods.
Fix it by changing it to this:
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Event extends Model {
Related
Using Laravel 5.4, I am getting this error on view whenever I call my method it shows on screen
(1/1) FatalThrowableError
Class 'App\Models\Chat\User' not found
Hierarchy of my project :
Controllers
- ChatMessageController
Models
-Chat
-message.php
-User.php
and here's my controller class code :
namespace App\Http\Controllers\Chat;
use App\Http\Controllers\Controller;
use App\Models\Chat\Message;
use App\Models\User;
use Illuminate\Http\Request;
class ChatMessageController extends Controller
{
public function index()
{
$messages = message::with(['user'])->latest()->limit(100)->get();
return response()->json($messages,200);
}
}
In message.php, probably you forgot to add: use App\Models\User;.
So, it is trying to find User in the wrong space.
I am getting the error below even though my Controller class is present in my Controllers directory;
Fatal error: Class 'App\Http\Controllers\Controller' not found
Below is the content of my UserController controller (in the same directory, Controllers):
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class UserController extends Controller
{
public function SignIn(Request $request)
{
if (Auth::attempt(['usernameEmail'=>$request['usernameEmail'],'password'=>$request['password']])) {
return redirect()->route('dashboard');
}
return redirect()->back();
}
public function getDashBoard()
{
return view('dashboard');
}
}
What am I doing wrong and how can I resolve it?
You get the Fatal error: Class 'App\Http\Controllers\Controller' not found
error because you have no such a class in your project at the derived location.
Instead of
use App\Http\Controllers\Controller; // this is irrelevant and causing the error you get
you should have
use Illuminate\Http\Request; // this is necessary for your SingIn method
as in:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
// your methods here
}
because one of your SignIn method does make use of it.
It you ran composer dump-autoload and it didn't fixed your problem you placed your controller in wrong folder. It should be inside app\Http\Controllers dir.
If it won't fix your issue, look at storage\logs\laravel.log where you can find more informations about the problem, and where your problem occured. Maybe you are defining routes using wrong classname or something like that.
Why yii2 throw exception, when I try use console controller? Code:
<?php
namespace app\commands;
use yii\console\Controller;
class Hashtag extends Controller
{
public function actionIndex($search = 'test')
{
echo $search;
}
}
Controller located in: app\commands\HashtagController
When using terminal: php yii hashtag
Exception 'yii\base\UnknownClassException' with message 'Unable to find 'app\commands\HashtagController' in file: /var/www/html/yiitask/yii2/commands/HashtagController.php. Namespace missing?'
in /var/www/html/yiitask/yii2/vendor/yiisoft/yii2/BaseYii.php:291
Other controller in this folder that was created before, working well.
Your namespace is wrong set the namespace for console controller properly eg: (depend where you have you console controller directory)
namespace app\console\controllers;
then could be is missing controller
class HashtagController extends Controller
{
instead of
class Hashtag extends Controller
{
I'm trying to place a trait inside a class called Page. I also need to rename a trait function so that it doesn't clash with an existing class function. I thought I did all this successfully however I get an error that points to the wrong location?!
Call to undefined function App\Pages\Models\myTraitDefaultScope()
I've also tried: MyTrait\defaultScope($query) instead of trying to rename the conflicting function. But I then get the following error:
Call to undefined function App\MyTrait\defaultScope()
Below is the trait and class contained in separate files.
<?php
namespace App;
use Illuminate\Support\Facades\Auth;
trait MyTrait{
public function defaultScope($query){
return $query->where('active', '1')
}
}
.
<?php namespace Modules\Pages\Models;
use Illuminate\Database\Eloquent\Model;
use App\MyTrait;
class Page extends Model {
use MyTrait{
MyTrait::defaultScope as myTraitDefaultScope;
}
public function defaultScope($query){
return myTraitDefaultScope($query);
}
}
I'm not all that awesome at this so please don't shoot if I've got something badly wrong :)
When you 'use' a trait in your class, the class inherits all the methods and properties of the trait, like if it was extending an abstract class or an interface
So, this method of MyTrait:
public function defaultScope($query){
return $query->where('active', '1')
}
will be inherited by your Page class
As you have aliased this method as: myTraitDefaultScope, to call the method you should call it in the same way you would call every other method of the Page class:
public function defaultScope($query){
//call the method of the class
return $this->myTraitDefaultScope($query);
}
As you're using trait. So it points to the current or parent class. Thus, calling any method should be like $this->method($params); syntax.
The manual of PHPUnit shows, that I can use class-constants in annotations for #expectedExceptionCode, see PHPUnit #expectedExceptionCode
I try to use it within my name spaced model which extends from Eloquent.
When I run my tests I got an: "PHP Fatal error: Class 'Eloquent' not found"
Running the App is all fine, so it depends on the Unit-Tests, isn't it?
Any ideas?
namespace Foo\Models;
use \Eloquent;
class Bar extends Eloquent {
const ERRORCODE = 150;
...
}
class BarTest extends TestCase {
/**
* #expectedExceptionCode Foo\Models\Bar::ERRORCODE
*/
public function testFoobar()
{
$name = 'foobar';
Bar::findBarOrFail($name);
}
}
For clarification:
PHP Fatal error: Class 'Eloquent' not found in PathToProject/app/models/Bar.php
Update
After #j-boschieros comment I got the above code working! Thanks mate!
However, when I provoke the exception in a controller test, the fatal error still occurs.
Even if I use the namespaces or not.
use \Eleoquent;
use Foo\Models\Bar;
class TestController extends TestCase {
/**
* #expectedExceptionCode Foo\Models\Bar::ERRORCODE
*/
public function testStoreActionWithInvalidDatatyp ()
{
$this->call('POST', '/routeToException');
}
Update 2
Got my Unit-Tests working when I extend \Illuminate\Database\Eloquent\Model instead of Eloquent.
namespace Foo\Models;
use \Illuminate\Database\Eloquent\Model;
class Bar extends Model {
const ERRORCODE = 150;
...
}
This differs from Laravel Doc. Is it still okay?
The "class" \Eloquent is actually an alias which is registered at run-time by the Laravel framework. You're making PHP parse the model class file before these aliases have been set up, causing it to fail. The correct approach to this is just to avoid the global namespace aliases and use the real class names instead (Illuminate\Database\Eloquent\Model instead of Eloquent in your case). You can find a list of class aliases in app/config/app.php.