Quick Tip: Using the Laravel from() Testing Helper


March 26th, 2021

Quick Tip: Using the Laravel from() Testing Helper

When testing with Laravel, sometimes it's helpful to test that a user was redirected back to the page where they "submitted" the form from.

Normally, if you just call the following the test will fail because the session doesn't have a previous page set.


To fix this issue, Laravel provides a from() testing helper that, under the hood, sets a referer header and sets the _previous.url session variable to the url you pass in. Then, when you call redirect()->back() in your controller or somewhere else, Laravel knows where to redirect the user to.

/** @test */
public function the_user_is_redirected_back_to_the_edit_page()
$user = User::factory()->create();
$post = Post::factory()->create();
$data = ['title' => 'Paul is Eric and Eric is Paul'];
->patch('/posts/1', $data)

Filed in:

Jason Beggs

TALL stack (Tailwind CSS, Alpine.js, Laravel, and Livewire) consultant and owner of roasted.dev.