Laravel : Can't move file if I use custom Form Request Class
up vote
1
down vote
favorite
I'm facing some strange problem when try to move file (image) with Laravel: when I create custom Form request class and after validation is passed, I'm receiving an error, but the file is moved regularly.
Here is my controller:
(...)
class ArticleController extends Controller{
public function store(StoreArticleRequest $request){
$article = new Article;
if ($request->hasfile('image')){
$name= rand(1, 10000).$request->file('image')->getClientOriginalName() ;
$move = $request->file('image')->move(public_path().'/images/articles/', $name);
$article->image = $name;
}
$article->title = request('title');
$article->preview = request('preview');
$article->text = request('content');
$article->user_id = Auth::user()->id;
$article->source = request('source');
$article->save();
return view('admin.dashboard');
}
...and here is the Form request class:
(...)
class StoreArticleRequest extends FormRequest{
public function authorize(){
return true;
}
public function rules(){
return [
'title' => 'required|unique:articles',
'source' => 'required',
];
}
public function messages(){
return [
'title.required' => 'foo',
'title.unique' => 'bar',
'source.required' => 'baz',
];
}
}
This way, although the file is moved to the required location, I'm receiving an error:
Symfony Component HttpFoundation File Exception FileNotFoundException
The file "/tmp/php9lQ7wd" does not exist
If I use the same code, but if I change an argument of store method in ArticleController, there is no error and files are moved normally:
public function store(Request $request){
(...the rest of the code in this method is exactly the same...)
Am I doing something wrong?
Is there a way to fix this?
TIA, Ognjen
*Laravel version: 5.7
UPDATE: Solved
There was no any strange Laravel behaviour, just a lack of my concentration.
The problem was that I forgot to add the following line to store method (when use StoreArticleRequest):
$validated = $request->validated();
php laravel validation request
New contributor
add a comment |
up vote
1
down vote
favorite
I'm facing some strange problem when try to move file (image) with Laravel: when I create custom Form request class and after validation is passed, I'm receiving an error, but the file is moved regularly.
Here is my controller:
(...)
class ArticleController extends Controller{
public function store(StoreArticleRequest $request){
$article = new Article;
if ($request->hasfile('image')){
$name= rand(1, 10000).$request->file('image')->getClientOriginalName() ;
$move = $request->file('image')->move(public_path().'/images/articles/', $name);
$article->image = $name;
}
$article->title = request('title');
$article->preview = request('preview');
$article->text = request('content');
$article->user_id = Auth::user()->id;
$article->source = request('source');
$article->save();
return view('admin.dashboard');
}
...and here is the Form request class:
(...)
class StoreArticleRequest extends FormRequest{
public function authorize(){
return true;
}
public function rules(){
return [
'title' => 'required|unique:articles',
'source' => 'required',
];
}
public function messages(){
return [
'title.required' => 'foo',
'title.unique' => 'bar',
'source.required' => 'baz',
];
}
}
This way, although the file is moved to the required location, I'm receiving an error:
Symfony Component HttpFoundation File Exception FileNotFoundException
The file "/tmp/php9lQ7wd" does not exist
If I use the same code, but if I change an argument of store method in ArticleController, there is no error and files are moved normally:
public function store(Request $request){
(...the rest of the code in this method is exactly the same...)
Am I doing something wrong?
Is there a way to fix this?
TIA, Ognjen
*Laravel version: 5.7
UPDATE: Solved
There was no any strange Laravel behaviour, just a lack of my concentration.
The problem was that I forgot to add the following line to store method (when use StoreArticleRequest):
$validated = $request->validated();
php laravel validation request
New contributor
Are you maybe forgetting to import the custom request like this at the top of the class:use AppHttpRequestsStoreArticleRequest;
– nakov
Nov 16 at 21:08
I imported custom request and other necessary classes. That isn't a problem but thanks for try. Validation is passed, but I got the described error that is related to moving a file.
– Ognjen K
Nov 16 at 21:25
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm facing some strange problem when try to move file (image) with Laravel: when I create custom Form request class and after validation is passed, I'm receiving an error, but the file is moved regularly.
Here is my controller:
(...)
class ArticleController extends Controller{
public function store(StoreArticleRequest $request){
$article = new Article;
if ($request->hasfile('image')){
$name= rand(1, 10000).$request->file('image')->getClientOriginalName() ;
$move = $request->file('image')->move(public_path().'/images/articles/', $name);
$article->image = $name;
}
$article->title = request('title');
$article->preview = request('preview');
$article->text = request('content');
$article->user_id = Auth::user()->id;
$article->source = request('source');
$article->save();
return view('admin.dashboard');
}
...and here is the Form request class:
(...)
class StoreArticleRequest extends FormRequest{
public function authorize(){
return true;
}
public function rules(){
return [
'title' => 'required|unique:articles',
'source' => 'required',
];
}
public function messages(){
return [
'title.required' => 'foo',
'title.unique' => 'bar',
'source.required' => 'baz',
];
}
}
This way, although the file is moved to the required location, I'm receiving an error:
Symfony Component HttpFoundation File Exception FileNotFoundException
The file "/tmp/php9lQ7wd" does not exist
If I use the same code, but if I change an argument of store method in ArticleController, there is no error and files are moved normally:
public function store(Request $request){
(...the rest of the code in this method is exactly the same...)
Am I doing something wrong?
Is there a way to fix this?
TIA, Ognjen
*Laravel version: 5.7
UPDATE: Solved
There was no any strange Laravel behaviour, just a lack of my concentration.
The problem was that I forgot to add the following line to store method (when use StoreArticleRequest):
$validated = $request->validated();
php laravel validation request
New contributor
I'm facing some strange problem when try to move file (image) with Laravel: when I create custom Form request class and after validation is passed, I'm receiving an error, but the file is moved regularly.
Here is my controller:
(...)
class ArticleController extends Controller{
public function store(StoreArticleRequest $request){
$article = new Article;
if ($request->hasfile('image')){
$name= rand(1, 10000).$request->file('image')->getClientOriginalName() ;
$move = $request->file('image')->move(public_path().'/images/articles/', $name);
$article->image = $name;
}
$article->title = request('title');
$article->preview = request('preview');
$article->text = request('content');
$article->user_id = Auth::user()->id;
$article->source = request('source');
$article->save();
return view('admin.dashboard');
}
...and here is the Form request class:
(...)
class StoreArticleRequest extends FormRequest{
public function authorize(){
return true;
}
public function rules(){
return [
'title' => 'required|unique:articles',
'source' => 'required',
];
}
public function messages(){
return [
'title.required' => 'foo',
'title.unique' => 'bar',
'source.required' => 'baz',
];
}
}
This way, although the file is moved to the required location, I'm receiving an error:
Symfony Component HttpFoundation File Exception FileNotFoundException
The file "/tmp/php9lQ7wd" does not exist
If I use the same code, but if I change an argument of store method in ArticleController, there is no error and files are moved normally:
public function store(Request $request){
(...the rest of the code in this method is exactly the same...)
Am I doing something wrong?
Is there a way to fix this?
TIA, Ognjen
*Laravel version: 5.7
UPDATE: Solved
There was no any strange Laravel behaviour, just a lack of my concentration.
The problem was that I forgot to add the following line to store method (when use StoreArticleRequest):
$validated = $request->validated();
php laravel validation request
php laravel validation request
New contributor
New contributor
edited 2 days ago
New contributor
asked Nov 16 at 20:55
Ognjen K
62
62
New contributor
New contributor
Are you maybe forgetting to import the custom request like this at the top of the class:use AppHttpRequestsStoreArticleRequest;
– nakov
Nov 16 at 21:08
I imported custom request and other necessary classes. That isn't a problem but thanks for try. Validation is passed, but I got the described error that is related to moving a file.
– Ognjen K
Nov 16 at 21:25
add a comment |
Are you maybe forgetting to import the custom request like this at the top of the class:use AppHttpRequestsStoreArticleRequest;
– nakov
Nov 16 at 21:08
I imported custom request and other necessary classes. That isn't a problem but thanks for try. Validation is passed, but I got the described error that is related to moving a file.
– Ognjen K
Nov 16 at 21:25
Are you maybe forgetting to import the custom request like this at the top of the class:
use AppHttpRequestsStoreArticleRequest;
– nakov
Nov 16 at 21:08
Are you maybe forgetting to import the custom request like this at the top of the class:
use AppHttpRequestsStoreArticleRequest;
– nakov
Nov 16 at 21:08
I imported custom request and other necessary classes. That isn't a problem but thanks for try. Validation is passed, but I got the described error that is related to moving a file.
– Ognjen K
Nov 16 at 21:25
I imported custom request and other necessary classes. That isn't a problem but thanks for try. Validation is passed, but I got the described error that is related to moving a file.
– Ognjen K
Nov 16 at 21:25
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Ognjen K is a new contributor. Be nice, and check out our Code of Conduct.
Ognjen K is a new contributor. Be nice, and check out our Code of Conduct.
Ognjen K is a new contributor. Be nice, and check out our Code of Conduct.
Ognjen K is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345256%2flaravel-cant-move-file-if-i-use-custom-form-request-class%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you maybe forgetting to import the custom request like this at the top of the class:
use AppHttpRequestsStoreArticleRequest;
– nakov
Nov 16 at 21:08
I imported custom request and other necessary classes. That isn't a problem but thanks for try. Validation is passed, but I got the described error that is related to moving a file.
– Ognjen K
Nov 16 at 21:25