deepu07
1 year ago

PHP Fatal error: Uncaught Error: Call to a member function connection() on null in C:\xampp7\htdocs\Lara\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1116

Posted 1 year ago by deepu07

Hi all, I'm trying to test my article controller [ArticleController] with the help phpunit. I had a simple testcase and related files. But still I'm getting an error. Can anyone help me out.

#ArticleTest.php#

<?php

//namespace Tests\Unit;

use Tests\TestCase;
use App\Article;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ArticleTest extends PHPUnit_Framework_TestCase
{
    /**
     * @test
     */
    public function article_index()
    {
        Article::action('GET', '[email protected]');

         $this->assertResponseOk();
        $this->assertViewHas('articles');
    }
}

#ArticleController.php#

<?php

namespace App\Http\Controllers;

use App\Article;
use App\Http\Controllers\ArticleController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class ArticleController extends Controller
{

    public function index()
    {
        $articles = Article::latest()->paginate(3);

        return view('article.index',compact('articles'));
    }

#Article.php#

<?php
namespace App;

use App\Comment;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
   public $timestamps = TRUE;
   protected $table = 'articles';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'title',
        'body'
    ];

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }

    public function user()
    {

        return $this->belongsTo(User::class);
    }
}
  

#error#

`PHP Fatal error:  Uncaught Error: Call to a member function connection() on null in C:\xampp7\htdocs\Lara\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:1116
Stack trace:
#0 C:\xampp7\htdocs\Lara\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1082): Illuminate\Database\Eloquent\Model::resolveConnection(NULL)
#1 C:\xampp7\htdocs\Lara\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(914): Illuminate\Database\Eloquent\Model->getConnection()
#2 C:\xampp7\htdocs\Lara\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(858): Illuminate\Database\Eloquent\Model->newBaseQueryBuilder()
#3 C:\xampp7\htdocs\Lara\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(821): Illuminate\Database\Eloquent\Model->newQueryWithoutScopes()
#4 C:\xampp7\htdocs\Lara\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php(1455): Illuminate\Database\Eloquent\Model->newQuery()
#5 C:\xampp7\htdocs\Lara\vendor\laravel\framework\src\Illuminate\Database\El in C:\xampp7\htdocs\Lara\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php on line 1116`


Thanks in advance

Please sign in or create an account to participate in this conversation.