What is the exact error?
its showing me App\project error
"" Route::post('projects', function() { App\Project::create(request(['title', 'description'])); }); ""
@Sinnbeck Undefined type 'Illuminate\Support\Facades\App\Project'.intelephense(1009)
@Sinnbeck ''' ) Tests\Feature\ProjectsTest::test_a_user_can_create_a_project Failed asserting that a row in the table [projects] matches the attributes { "title": "Nam cum qui consequuntur iure aut.", "description": "Et vel et iusto alias similique nam. Eaque veniam incidunt quis quis. Harum atque sint eaque esse. Voluptas dicta est deserunt dolore." }.
'''
@jaspal So your projects table does not have a title column?
@Sinnbeck after fixing it shows this ..
Failed asserting that a row in the table [projects] matches the attributes {
"title": "Nam cum qui consequuntur iure aut.",
"description": "Et vel et iusto alias similique nam. Eaque veniam incidunt quis quis. Harum atque sint eaque esse. Voluptas dicta est deserunt dolore."
}```
@jaspal Show the test as well then :) Add ``` on the line before and after your code to format it.
@Sinnbeck formatted sir help me out
@jaspal I dont see the test anywhere?
- Tests\Feature\ProjectsTest::test_a_user_can_create_a_project
Failed asserting that a row in the table [projects] matches the attributes {
"title": "Culpa odio et iste.",
"description": "Asperiores libero consequatur optio praesentium fugit facilis exercitationem. Dignissimos sapiente aut dignissimos. Sunt expedita illo minus in."
}.
@jaspal Thats the error? Can you show the code for the test itself
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
class ProjectsTest extends TestCase
{
use WithFaker, RefreshDatabase;
public function test_a_user_can_create_a_project()
{
$this->withExceptionHandling();
$attributes = [
'title' => $this->faker->sentence,
'description' => $this->faker->paragraph,
];
$this->post('/projects', $attributes);
$this->assertDatabaseHas('projects',$attributes);
}
}
@jaspal Check the model Project and see if both title and description are in the $fillable array
@Sinnbeck yeah!
I have used protected $gaurded = [];
@jaspal Its spelled protected $guarded = []
@Sinnbeck spell corrected but it shows same error.
Can you check that a record is indeed inserted?
$this->assertDatabaseCount('projects', 1);
$this->assertDatabaseHas('projects',$attributes);
@Sinnbeck after trying your code .. find this issue.
- Tests\Feature\ProjectsTest::test_a_user_can_create_a_project
Failed asserting that table [projects] matches expected entries count of 1. Entries found: 0.
@jaspal Try changing this
$this->withExceptionHandling(); //before
$this->withoutExceptionHandling(); //after
@Sinnbeck 1) Tests\Feature\ProjectsTest::test_a_user_can_create_a_project
Error: Class "Illuminate\Support\Facades\App\Project" not found
@Sinnbeck Route::post('projects', function() { App\Project::create(request(['title', 'description'])); });
@jaspal Change it to
Route::post('projects', function() {
\App\Project::create(request(['title', 'description']));
});
@Sinnbeck done but shows new error.
- Tests\Feature\ProjectsTest::test_a_user_can_create_a_project
Error: Class "App\Project" not found
@jaspal And the file is app/Project.php? Or app/Models/Project.php
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Route;
// use Illuminate\Support\Facades\App\Project;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::post('projects', function() {
\App\Project::create(request(['title', 'description']));
});```
@Sinnbeck project.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
use HasFactory;
// protected $guarded = [];
protected $fillable = [
'title',
'description'
];
}
@jaspal So its in Models
Route::post('projects', function() {
\App\Models\Project::create(request(['title', 'description']));
});
@Sinnbeck No sir this is inside the web.php file
@jaspal No the Project model. It says it cannot find that file. And you gave it the wrong path (namespace). You forgot \Models\
@Sinnbeck My bad . again thanks a lot sir
@jaspal Happy to help. Does it all work now?
good grief
Please or to participate in this conversation.