我正在阅读Laravel教程并坚持使用#34;调用未定义的函数"错误。到目前为止,我有20个测试,有28个断言,只有这个测试失败了。我无法找到我的拼写错误。请告诉我,我必须添加什么源代码。我是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.
一定有拼写错误,但我找不到。
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);
}
}
答案 0 :(得分:0)
我在搜索了几个小时之后发现了我的拼写错误...
这样:
$threads = $channel->threads()->latest()-get();
必须是:
$threads = $channel->threads()->latest()->get();
现在一切正常。