I'm working on adding media uploads to my custom CMS. To do this, I have tried multiple forms of the same upload function. When I click upload, the error says 'image' is a required field even with a file inside the field. Below, I have my model, view, controller, and migration to provide insight on how I am trying to upload media.
Media.php (Model)
class Media extends Model
{
use HasFactory;
protected $fillable = ['name', 'path', 'alt', 'user_id'];
}
MediaController.php (Controller)
class MediaController extends Controller
{
public function index()
{
$uploads = Media::get();
$users = User::get();
return view('admin.media.index', compact('uploads', 'users'));
}
public function create()
{
return view('admin.media.upload');
}
public function store(Request $request)
{
$this->validate($request, [
'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048'
]);
$name = $request->file('image')->getClientOriginalName();
$path = $request->file('image')
->store('public/img/media/' . date("Y/M"));
$upload = new Media;
$upload->user_id = Auth::id();
$upload->name = $name;
$upload->path = $path;
$upload->alt = $request->alt;
$upload->save();
return redirect()->route('admin.media.index')
->with('success', 'Media uploaded successfully');
}
}
upload.blade.php (View)
#extends('layouts.admin.app')
#section('title', 'Upload Media')
#section('content')
<section class="my-4">
<h3>{{ __('Media')}} | {{ __('Upload New Media') }}</h3>
<a class="btn btn-warning" href="{{ route('admin.media.index') }}"> {{ __('Go Back To Media')}}</a>
</section>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<div class="flex">
<form action="{{ route('admin.media.store') }}" enctype="multipart/form-data" method="POST" class="w-full">
#csrf
<div class="flex">
<div class="w-full md:w-3/4">
<x-general.form.group class="">
<input type="file" name="file" class="form-control" id="image">
#error('image')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</x-general.form.group>
<button type="submit" class="btn btn-dark w-full" value="{{ __('Upload Media')}}"/>
</div>
<div class="w-full md:w-1/4">
<x-general.form.group class="">
<img src="" id="image_preview" alt="Upload Preview" class=""/>
<x-admin.general.forms.label for="alt" label="{{ __('Alt Text')}}" class="small"/>
<input type="text" name="alt" id="alt" class="" value=""/>
#error('alt')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</x-general.form.group>
</div>
</div>
</form>
</div>
#endsection
I also have screenshots to show the process:
The error appears because there is no <input> element with name="image. Your file upload field has name="file". Change
<input type="file" name="file" class="form-control" id="image">
to
<input type="file" name="image" class="form-control" id="image">
Related
I am trying to allow users to create and add category tags to a post when creating that post in the form.
I would further want those tags to appear in the profile view and function as filter buttons to show posts according to the tag names they possess.
However, in my attempt to achieve this overall result, I am stuck because everytime I submit a post with tags, the tags array keeps showing up empty in the view.
My post table is:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('caption');
$table->string('url');
$table->string('image');
$table->text('tags');
$table->timestamps();
$table->index('user_id');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
My Post Model is:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Post extends Model
{
use \Conner\Tagging\Taggable;
use HasFactory;
protected $fillable = ['caption','url','image', 'tags'];
public function user()
{
return $this->belongsTo(User::class);
}
protected $dates = [
'created_at',
'updated_at',
];
}
My create and store methods in PostsController are:
public function create()
{
return view('posts.create');
}
public function store(Request $request)
{
$data = request()->validate([
'caption' => 'required',
'url' => 'required',
'image' => ['required', 'image'],
'tags' => 'required',
]);
$tags = explode(", ", $request->tags);
$imagePath = request('image')->store('uploads', 'public');
auth()->user()->posts()->create([
'caption' => $data['caption'],
'url' => $data['url'],
'image' => $imagePath,
'tags' => $data['tags'],
]);
return redirect('/users/' . auth()->user()->id);
}
My form to create post is:
<form action="/posts" enctype="multipart/form-data" method="post">
#csrf
<div class="form-group">
<label for="caption" class="create_caption_label">Post Caption</label>
<div class="create_caption_div">
<input id="caption"
type="text"
class="form-control #error('caption') is-invalid #enderror"
name="caption"
value="{{ old('caption') ?? '' }}"
autocomplete="caption" autofocus>
#error('caption')
<div class="invalid-feedback-div">
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
</div>
#enderror
</div>
</div>
<div class="form-group">
<label for="tags" class="create_tags_label">Tags</label>
<div class="create_tags_div">
<input id="tags"
type="text"
data-role="tagsinput"
class="form-control #error('tags') is-invalid #enderror"
name="tags"
value="{{ old('tags') ?? '' }}"
autocomplete="tags" autofocus>
#error('tags')
<div class="invalid-feedback-div">
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
</div>
#enderror
</div>
</div>
<div class="form-group">
<label for="url" class="edit_title_label">URL</label>
<div class="edit_url_div">
<input id="url"
type="text"
class="form-control #error('url') is-invalid #enderror"
name="url"
value="{{ '' }}"
autocomplete="url" autofocus>
#error('url')
<div class="invalid-feedback-div">
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
</div>
#enderror
</div>
</div>
<div class="create_post_image_div">
<label for="image" class="create_image_label">Post Image</label>
<input type="file" class="form-control-file" id="image" name="image">
#error('image')
<div class="invalid-feedback-div">
<strong>{{ $message }}</strong>
</div>
#enderror
<div class="create_post_btn_div">
<button class="create_post_btn">Save Post</button>
</div>
</div>
</form>
Finally, my view is: (This is where the tags array shows up empty after submitting a post)
#foreach( $user->posts as $post )
<div class="carousel_posts_container">
<div class="post_date_and_edit_div">
<div class="posted_date_div">
<p class="posted_date">posted: {{ $post->created_at->diffForHumans() }}</p>
</div>
<div class="post_edit_div">
<form action="/posts/{{$post->id}}/edit">
<input class="post_edit_btn" type="submit" value="• • •">
</form>
</div>
</div>
<div class="post_counter_div">
<p class="post_counter">1 like</p>
</div>
<div class="post_counter_div">
<p class="post_counter">1 comment</p>
</div>
<div class="carousel_post_img_div">
<img src="/storage/{{ $post->image }}" class="carousel_img_placeholder">
</div>
<div class="like_comment_view_container">
<div class="view_btn_div">
<form action="{{$post->url}}">
<input class="like_comment_view_btns" type="submit" value="( View Post )">
</form>
</div>
<div class="like_btn_div">
<button type="button" class="like_comment_view_btns">( Like )</button>
</div>
<div class="comment_btn_div">
<button type="button" class="like_comment_view_btns">( Comment )</button>
</div>
</div>
<div class="carousel_caption_container">
<div class="carousel_caption_div">
<p class="carousel_caption_username">{{$user->username}} - {{$post->caption}}</p>
<p class="carousel_caption">{{$post->caption}}</p>
</div>
<div class="post-tags mb-4">
<strong>Tags : </strong>
#foreach($post->tags as $tag)
<span class="badge badge-info">{{$tag->name}}</span>
#endforeach
</div>
</div>
</div>
#endforeach
How can I resolve this issue?
And furthermore, how can I allow the tags to function as filter buttons to show posts according to the tag names they possess?
You can save tags as json array in db. Then cast to array in model, so it will be automatically array when you will retrieve. When saving you will pass tags as array then it will automatically convert to json string.
protected $casts = [
'tags' => 'array',
];
I am creating a very simple survey web app and in the answering of a created survey the controller is not functioning in the controller instead of dumping the array data and its information it instead redirects straight back to the form, and doesn't dump anything, there are no errors when executing the code it just redirects back to the form. I have tried remigrating, and rewriting the form in the view that fixed some things but still not the main issue
Survey Controller:
public function store(Questionnaire $questionnaire, $slug){
$data = request()->validate([
'responses.*.answerid' => 'required',
'responses.*.question_id' => 'required',
'survey.name' => 'required',
'survey.email' =>'required | email',
]);
dd($data);
}
Blade.php View:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<h1>{{ $questionnaire->title }}</h1>
<form action = "/surveys/{{ $questionnaire->id }}-{{Str::slug($questionnaire->title)}}" method = "post">
#csrf
#foreach($questionnaire->questions as $key => $question)
<div class="card mt-4">
<div class="card-header"><strong> {{ $key + 1 }} </strong>{{ $question->question }}</div>
<div class = "card-body">
#error('responses.' . $key . '.answerid')
<small class = "text-danger">{{ $message }}</small>
#enderror
<ul class = "list-group">
#foreach($question->answers as $answer)
<label for="answer{{$answer->id}}">
<li class="list-group-item">
<input type = "hidden" name = 'responses[{{$key}}][questionid]' value = '{{$question->id}}'>
<input type = "radio" name = 'responses[{{$key}}][answerid]' id = 'answer{{ $answer->id }}' class = "mr-2" value= "{{$answer->id}}" {{ old('responses.' . $key . '.answer_id') == $answer->id ? 'checked' : '' }}>
{{ $answer->answer }}
</li>
</label>
#endforeach
</ul>
</div>
</div>
#endforeach
<div class="card mt-4">
<div class="card-header">Your Information</div>
<div class="card-body">
<div class="form-group">
<label for="name">Your Name</label>
<input name = "survey[name]" type="text" class="form-control" id="name" aria-describedby="nameHelp" placeholder="Enter Name">
<small id="nameHelp" class="form-text text-muted">Please Enter Your Name</small>
#error('name')
<small class = "text-danger">{{ $message }}</small>
#enderror
</div>
</div>
<div class="card-body">
<div class="form-group">
<label for="email">Your Email</label>
<input name = "survey[email]" type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter Email">
<small id="emailHelp" class="form-text text-muted">Please enter your Email</small>
#error('email')
<small class = "text-danger">{{ $message }}</small>
#enderror
</div>
</div>
<div>
<button class = "btn btn-dark" type="submit">Complete Survey</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
#endsection
Routes file:
Route::get('/surveys/{questionnaire}-{slug}', 'SurveyController#show');
Route::post('/surveys/{questionnaire}-{slug}', 'SurveyController#store');
Questionnaire Model:
protected $guarded = [];
public function surveys(){
return $this->hasMany(Survey::class);
}
Survey Model:
protected $guarded = [];
public function questionnaire(){
return $this->belongsTo(Questionnaire::class);
}
public function responses(){
return $this->hasMany(SurveyResponse::class);
}
SurveyResponses Model:
protected $guarded = [];
public function survey(){
return $this->belongsTo(Survey::class);
}
Survey Migration:
Schema::create('surveys', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('questionnaire_id');
$table->string('name');
$table->string('email');
$table->timestamps();
});
Survey Responses Migration:
Schema::create('survey_responses', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('survey_id');
$table->unsignedBigInteger('questionid');
$table->unsignedBigInteger('answerid');
$table->timestamps();
});
This is the form in the blade.php file:
<form action = "/surveys/{{ $questionnaire->id }}-{{Str::slug($questionnaire->title)}}" method = "post">
#csrf
#foreach($questionnaire->questions as $key => $question)
<div class="card mt-4">
<div class="card-header"><strong> {{ $key + 1 }} </strong>{{ $question->question }}</div>
<div class = "card-body">
#error('responses.' . $key . '.answerid')
<small class = "text-danger">{{ $message }}</small>
#enderror
<ul class = "list-group">
#foreach($question->answers as $answer)
<label for="answer{{$answer->id}}">
<li class="list-group-item">
<input type = "hidden" name = 'responses[{{$key}}][questionid]' value = '{{ $question->id }}'>
<input type = "radio" name = 'responses[{{$key}}][answerid]' id = 'answer{{ $answer->id }}' class = "mr-2" value= "{{$answer->id}}" {{ old('responses.' . $key . '.answer_id') == $answer->id ? 'checked' : '' }}>
{{ $answer->answer }}
</li>
</label>
#endforeach
</ul>
</div>
</div>
#endforeach
<div class="card mt-4">
<div class="card-header">Your Information</div>
<div class="card-body">
<div class="form-group">
<label for="name">Your Name</label>
<input name = "survey[name]" type="text" class="form-control" id="name" aria-describedby="nameHelp" placeholder="Enter Name">
<small id="nameHelp" class="form-text text-muted">Please Enter Your Name</small>
#error('name')
<small class = "text-danger">{{ $message }}</small>
#enderror
</div>
</div>
<div class="card-body">
<div class="form-group">
<label for="email">Your Email</label>
<input name = "survey[email]" type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter Email">
<small id="emailHelp" class="form-text text-muted">Please enter your Email</small>
#error('email')
<small class = "text-danger">{{ $message }}</small>
#enderror
</div>
</div>
<div>
<button class = "btn btn-dark" type="submit">Complete Survey</button>
</div>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
</div>
</div>
</form>
This is the input where the question id should be passed through:
<input type = "hidden" name = 'responses[{{$key}}][questionid]' value = '{{ $question->id }}'>
Your dd($data); in your controller not run if your request validate fails because when validation fails it redirect to your view with errors. So you have two way to see error:
1- use below code in your controller:
use Illuminate\Support\Facades\Validator; // Important to use Validator Facade in this case
public function store(Questionnaire $questionnaire, $slug){
$validator = Validator::make($request->all(), [
'responses.*.answerid' => 'required',
'responses.*.question_id' => 'required',
'survey.name' => 'required',
'survey.email' =>'required|email',
]);
if ($validator->fails()) {
//dd($validator);
return redirect(route('route-name'))
->withErrors($validator)
->withInput();
}
}
2- use below code in your view:
<!-- /resources/views/view-name.blade.php -->
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
I've saved in MYSQL database and in my laravel storage two images created by a form of my webpage. I just wanna recall all images saved in the storage and show all them in my webpage. I've used this in my createcar.blade.php page:
<img src="{{Storage::url(img)}}" alt="img">
but it doesn't work
This is the createcar.blade.php part form page:
<div class="container">
<div class="row py-3" style="justify-content:center">
<div class="col-md-4">
<div class="form_main">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<h4 class="heading"><strong>Compile </strong> form! <span>
</span></h4>
<div class="form">
<form action="{{route('submit')}}" method="post" id="contactFrm" name="contactFrm" enctype="multipart/form-data">
#csrf
<input type="text" required="" placeholder="Name Car" value="{{old('name')}}" name="name" class="txt">
<input type="file" name="img" placeholder="Image"
value="{{old('img')}}">
<textarea placeholder="Features Vehicle"
name="message" value="{{old('message')}}" type="text" class="txt_3"> </textarea>
<input type="submit" value="Add" name="submit" class="txt2">
</form>
<img src="{{Storage::url(img)}}" alt="img"> //this is the code i've used to recall images from storage
</div>
</div>
</div>
</div>
</div>
This is the file CarsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Requests\CarsRequest;
class CarsController extends Controller
{
public function submit(CarsRequest $req){
$nome = $req->input('name');
$messaggio = $req->input('message');
$img = $req->file('img')->store('public/img');
$dati = compact('nome', 'messaggio', 'img');
DB::table('cars')->insert(
[
'name' => $nome,
'message' => $messaggio,
'img' => $img,
]);
return redirect(route('carthankyou'));
}
public function carthankyou() {
return view('contacts.carthankyou');
}
You should find images in database
public function carthankyou() {
$cars = DB::table('cars')->get();
return view('contacts.carthankyou', ['cars' => $cars]);
}
and show images like this
#foreach($cars as $car)
<img src="{{Storage::url($car->img)}}" alt="img">
#endforeach
also you can add user_id field and show only current user cars
Using Laravel 5.6 before, my photos uploaded perfectly and now that I've upgraded to 5.7, now they wont and I'm at a loss. The posts will upload, just not the photos. I have checked any rechecked the relationships and routes gut to no avail. Any help will be appreciated.
home.blade.php:
<form method="POST" action="{{ route('makePost') }}">
#csrf
<div class="form-group row">
<label for="body" class="col-md-4 col-form-label text-md-right">{{ __('Body') }}</label>
<div class="col-lg-6">
<textarea id="body" type="text" class="form-control{{ $errors->has('body') ? ' is-invalid' : '' }}" name="body" value="{{ old('body') }}" required autofocus></textarea>
#if ($errors->has('body'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('body') }}</strong>
</span>
#endif
</div>
</div>
<div class="col-md-6">
<input id="image" type="file" class="form-control{{ $errors->has('image') ? ' is-invalid' : '' }}" name="image" value="{{ old('image') }}" autofocus>
#if ($errors->has('image'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('image') }}</strong>
</span>
#endif
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Create Post') }}
</button>
</div>
</div>
</form>
PostsController.php:
public function store(Request $request)
{
$input = $request->all();
$user = Auth::user();
if($file = $request->file('photo_id')) {
$name = time() . $file->getClientOriginalName();
$file->move('images', $name);
$photo = Photo::create(['file'=>$name]);
$input['photo_id'] = $photo->id;
}
$user->post()->create($input);
return redirect('/home');
}
Photo.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Photo extends Model
{
protected $fillable = [
'file',
];
public function user() {
return $this->belongsTo('App\User');
}
public function photo() {
return $this->belongsTo('App\Photo');
}
}
Post.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'body', 'photo_id', 'user_id',
];
public function post()
{
return $this->belongsTo('App\User');
}
}
Add enctype="multipart/form-data" to <form> tag. This value is required when you are using forms that have a file upload control.
<form method="POST" action="{{ route('makePost') }}" enctype="multipart/form-data">
//
</form>
Source: HTML enctype Attribute
please help me to create a image upload system using Laravel 5.4 and also can save the filename at the database...
i can't find any related article about this and i also tried a youtube tutorial but it doesn't explain how filename transferred on the database, hope you can help mo on this
thank you...
here so far my code that i done...
$this->validate(request(), [
'article_banner' => 'required | mimes:jpeg,jpg,png | max:2000',
'article_title' => 'required|max:255',
'article_date' => 'required|date',
'article_content' => 'required',
]
);
$article_banner = $request->file('article_banner');
$article_title = $request->input('article_title');
$article_date = $request->input('article_date');
$article_content = $request->input('article_content');
return $article_banner;
}
also here's my error on validation every time i upload a docx... not image
here's the article_add.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">User Management -> Edit User</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ route('article_add.post') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('article_banner') ? ' has-error' : '' }}">
<label for="article_banner" class="col-md-4 control-label">Article Banner: </label>
<div class="col-md-6">
<input id="article_banner" type="file" class="form-control" name="article_banner" required autofocus>
<p class="help-block">Example block-level help text here.</p>
#if ($errors->has('article_banner'))
<span class="help-block">
<strong>{{ $errors->first('article_banner') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('article_title') ? ' has-error' : '' }}">
<label for="article_title" class="col-md-4 control-label">Article Title: </label>
<div class="col-md-6">
<input id="article_title" type="text" class="form-control" name="article_title" value="{{ old('article_title') }}" required autofocus>
#if ($errors->has('article_title'))
<span class="help-block">
<strong>{{ $errors->first('article_title') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('article_date') ? ' has-error' : '' }}">
<label for="article_date" class="col-md-4 control-label">Article Date: </label>
<div class="col-md-6">
<input id="article_date datepicker" type="text" class="form-control datepicker" name="article_date" value="{{ old('article_date') }}" data-provide="datepicker" required autofocus>
#if ($errors->has('article_date'))
<span class="help-block">
<strong>{{ $errors->first('article_date') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('article_content') ? ' has-error' : '' }}">
<div style="padding:10px;">
<label for="article_content">Article Date: </label>
<br />
<textarea id="content article_content" type="text" class="form-control" name="article_content" autofocus>{{ old('article_content') }}</textarea>
</div>
#if ($errors->has('article_content'))
<span class="help-block">
<strong>{{ $errors->first('article_content') }}</strong>
</span>
#endif
</div>
#if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
#endif
#if(session()->has('errors'))
<div class="alert alert-danger">
{{ session()->get('errors') }}
</div>
#endif
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Submit
</button>
<a href="{{ url('article_management') }}" class="btn btn-primary">
Back
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
make one function as
public function uploadFiles($_destination_path, $images, $new_file_name) { //code to uplaod multiple fiels to path and return paths array wit file names
$file_name = str_replace(' ', '-', $new_file_name);
$paths = array('path' => $_destination_path . '/' . basename(Storage::disk($this->diskStorage)->putFileAs($_destination_path, $images, $file_name)),
'name' => pathinfo($file_name));
return $paths;
}
And pass required argument to it like as
$image = $request->file('image');
$fileName = $image->getClientOriginalName();
$destinationPath = '/images';
$img_path[] = $this->uploadFiles($destinationPath, $image, $fileName);
You will get required data in the img_path[] array variable.
public function feedbackPost(Request $request, $id)
{
$fileName1 = "";
$fileName2 = "";
$rules = array(
'conferencename' =>'required',
'yourname' =>'required',
'email' =>'required',
'objective' =>'required',
'results' =>'required',
'recommendations' =>'required',
'key_customers' =>'required',
'actions' =>'required',
'business_opportunities' =>'required',
'other_opportunities' =>'required',
'upload_leads' =>'mimes:csv,xls,xlsx',
'upload_attendees' =>'mimes:csv,xls,xlsx',
);
$validator = Validator::make($request->all(), $rules);
if ($validator->fails())
{
return back()->with('danger', 'File format not valid');
}
else
{
if($file=$request->hasFile('upload_attendees')) {
$file=$request->file('upload_attendees');
$fileName1=$file->getClientOriginalName();
if (!file_exists('uploads/feedback/attendees/'.$id.'')) {
mkdir('uploads/feedback/attendees/'.$id.'', 0777, true);
}
$destinationPath='uploads/feedback/attendees/'.$id.'';
$file->move($destinationPath,$fileName1);
}
if($file=$request->hasFile('upload_leads')) {
$file=$request->file('upload_leads');
$fileName2=$file->getClientOriginalName();
if (!file_exists('uploads/feedback/leads/'.$id.'')) {
mkdir('uploads/feedback/leads/'.$id.'', 0777, true);
}
$destinationPath='uploads/feedback/leads/'.$id.'';
$file->move($destinationPath,$fileName2);
}
$feedback = Feedback::insert([
'user_id' => $request->user_id,
'conferenceid' => $request->conferenceid,
'conferencename' =>$request->conferencename,
'yourname' =>$request->yourname,
'email' =>$request->email,
'objective' =>$request->objective,
'results' =>$request->results,
'recommendations' =>$request->recommendations,
'key_customers' =>$request->key_customers,
'actions' =>$request->actions,
'business_opportunities' =>$request->business_opportunities,
'other_opportunities' =>$request->other_opportunities,
'upload_attendees' =>$fileName1,
'upload_leads' =>$fileName2,
]);
}
return back()->with('success', 'Thanks! Your Feedback has been Submitted!');
}
This is how I did. You may try this.