Hi Everyone !
I must submit post's informations and featured image (path) to the database via a form.
Here, my code works better but sometimes users will not be obliged to attach an image to the post.
So, when i submit informations with an empty image field, i got errors.
The question is How can i do to submit a form with an empty image field without catching errors.
Thanks for your help !
Codes :
Form in my view :
{!! csrf_field() !!}
Title
<input id="Title" type="text" class="form-control" name="Title">
Text
<textarea id="ckeditor" class="ckeditor" name="Content"></textarea>
<script type="text/javascript">
CKEDITOR.replace('ckeditor');
</script>
Categorie :
<select id="IdCat" class="form-control" name="IdCat">
<?php
foreach ($cat as $cats) {
?>
<option value="<?php echo $cats->IdCat ?>"><?php echo $cats->NomCat ?></option>
<?php
}
?>
</select>
<label for="ImagePlan" class="control-label">Featured image : </label>
<input type="file" id="ImagePlan" name="ImagePlan" class="control-label">
<button type="submit" class="btn btn-primary">
<i class="fa fa-btn alert-success"></i> Publier
</button>
</form>
Method in my controller named PostController :
public function insertPost(Request $req)
{
$post= new Post;
$post->IdCat=$req->IdCat;
$post->Title=$req->Title;
$post->User_Id=$req->User_Id;
$post->Content=$req->Content;
$image=$req->file('ImagePlan');
$filename=$image->getClientOriginalName();
Storage::disk('uploads')->put('/imagesPost/'.$filename,file_get_contents($req->file('ImagePlan')->getRealPath()));
$post->ImagePlan=$filename;
$post->save();
session()->flash('success','Operation succeed');
return redirect()->route('admin');
}
This is the error :
FatalErrorException in PostController.php line 30:
Call to a member function getClientOriginalName() on a non-object