lundi 30 mars 2020

Error ErrorException: Undefined index: id in Laravel test

I am beginner in Laravel and php. I use in my project Laravel 5.8.

I have this code:

class AdTest extends TestCase
{
public function testDeleteArticle()
    {
        $this->withoutExceptionHandling();
        $user = User::find(1);
        $this->be($user);

        $ads = factory(Ad::class)->create();
        $this->post('/ads-destroy', ['id' => $ads->id]);
        $this->assertDatabaseMissing('ads', ['id' => $ads->id]
    }
}

Record was add correct. Problem is wit delete.

I have error:

1) Tests\Feature\AdTest::testDeleteArticle
ErrorException: Undefined index: id
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

In application add/edit/remove works fine. Problem is only with this test.

My model:

class Ad extends Model
{
    use ScopeActiveTrait;

    protected $quarded = ['id'];
    protected $fillable = ['company_id', 'user_id', 'title', 'content', 'provincial_id', 'enable', 'url_address'];
    public $timestamps = true;

    public function author()
    {
        return $this->belongsTo('App\Models\User', 'user_id');
    }
}

My Controller:

public function destroy(Request $request)
    {
        if ($request->isMethod('post')) {
            foreach ($_POST['id'] as $value) {
                $this->model->delete($value);
            }
            return redirect()->route('ads.index')->with('success', 'Rekordy zostały usunięte!');
        }
    }

How can I repair this error?

Please help me :)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire