0

I am going through a Laravel tutorial and stuck at a "Call to undefined function" error. So far I have 20 Tests with 28 Assertions and only this test fails. I can't find my typo. Tell me please what source code do I have to add additionaly. I am new to Laravel.

λ vendor\bin\phpunit --filter a_user_can_filter_threads_according_to_a_channel
PHPUnit 5.7.21 by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 409 ms, Memory: 14.00MB

There was 1 error:

1) Tests\Feature\ReadThreadsTest::a_user_can_filter_threads_according_to_a_channel
Symfony\Component\Debug\Exception\FatalThrowableError: Call to undefined function App\Http\Controllers\get()

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

There must be a typo, but I can't find it.

ReadThreadsTest.php

/** @test */
public function a_user_can_filter_threads_according_to_a_channel()
{
    $channel = create('App\Channel');
    $threadInChannel = create('App\Thread', ['channel_id' => $channel->id]);
    $threadNotInChannel = create('App\Thread');

    $this->get('/threads/' . $channel->slug)
        ->assertSee($threadInChannel->title)
        ->assertDontSee($threadNotInChannel->title);
}

web.php

Route::get('threads/{channel}', 'ThreadsController@index');

Channel.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Channel extends Model
{
    public function getRouteKeyName()
    {
        return 'slug';
    }

    public function threads()
    {
        return $this->hasMany(Thread::class);
    }

}

ThreadController.php

public function index(Channel $channel)
{

    if ($channel->exists)
    {
        $threads = $channel->threads()->latest()-get();
    } else {
        $threads = Thread::latest()->get();
    }

    return view('threads.index', compact('threads'));
} 

Thread.php

<?php


namespace App;

use Illuminate\Database\Eloquent\Model;


class Thread extends Model
{

    protected $guarded = [];

    public function path()
    {
        return "/threads/{$this->channel->slug}/{$this->id}";
    }

    public function replies()
    {
        return $this->hasMany(Reply::class);
    }

    public function creator()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function channel()
    {
        return $this->belongsTo(Channel::class);
    }

    public function addReply($reply)
    {
        $this->replies()->create($reply);
    }
}
3
  • Can you show your full ReadThreadsTest class ? Commented Aug 24, 2017 at 14:47
  • $this->get('/threads/' . $channel->slug) isn't working; if you're trying to call a GET request on that URL, you'll have to use an HTTP client like Guzzle Commented Aug 24, 2017 at 14:59
  • I found my typo finally. This token ">" was missing: ->latest()-get() Thanks anyway! Commented Aug 24, 2017 at 15:14

1 Answer 1

1

I found finaly my typo after few hours searching ...

this:

    $threads = $channel->threads()->latest()-get();

must be:

    $threads = $channel->threads()->latest()->get();

Now everything works.

Sign up to request clarification or add additional context in comments.

2 Comments

If the solution is to correct typo, just delete the question.
I am getting this message: "Deletion of answered questions can result in your account being blocked from asking. Are you sure you wish to delete?" I didn't know if it was just a typo or this example from a tutorial didn't work with my laravel version. Laravel didn't show me an syntax-error or another error to help me locate this typo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.