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();









share|improve this question









New contributor




Ognjen K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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















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();









share|improve this question









New contributor




Ognjen K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • 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













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();









share|improve this question









New contributor




Ognjen K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




Ognjen K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Ognjen K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago





















New contributor




Ognjen K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 16 at 20:55









Ognjen K

62




62




New contributor




Ognjen K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Ognjen K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Ognjen K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • 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










  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Ognjen K is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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






























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.










 

draft saved


draft discarded


















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.















 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin