added the upload functions for both company image and registration certificate, stored under /storage/app directory

This commit is contained in:
Aqeeb Imtiaz Harun
2018-05-15 10:36:41 +08:00
parent 2da890f36e
commit a1ebdb9767
5 changed files with 129 additions and 167 deletions
+82 -166
View File
@@ -6,134 +6,24 @@ use Illuminate\Http\Request;
use App\Company;
use Image;
use Illuminate\Support\Facades\Storage;
use Validator;
class CompanyController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
return view ('home');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
//$company = new Company;
// if($request->hasFile('avatar'))
// {
// $avatar = $request->file('avatar');
// $filename = time() . '.' . $avatar->getClientOriginalExtension();
// Image::make($avatar)->resize(300,300)->save(public_path('/uploads/company_profile/'.$filename));
// }
// else
// {
// return response()->json(['message'=>'Document not found']);
// }
$input = $request->all();
//validating uploaded files
// $this->validate(request(),[
// 'company_profile' => 'required|image|mimes:jpg,jpeg,bmp,png',
// 'reg_cerf' => 'required|mimes:jpg,jpeg,bmp,png,gif,svg,pdf'
// ]);
if($file = $request->file('company_profile')) //company profile picture
{
$company_profile = $request->file('company_profile');
$filename = $company_profile->getClientOriginalName();
//$destinationPath = config('app.fileDestinationPath').'/'.$filename;
$file = $request->file('company_profile');
$ext = $file->getClientOriginalExtension();
Storage::putFileAs(
'company_profile',/*folder name*/ $file, $filename. '.' .$ext
);
// $name = $file->getClientOriginalName();
// $img_file = $request->file('company_profile');
// Image::make($img_file)->resize(300,300)->save(public_path('\company_profile'));
// //return $img_file;
// // $file->move('uploads/company_profile/', $img_file);
// $input['company_profile'] = $name;
}
if($request->hasFile('reg_cert')) //Registration Certificate upload
{
$reg_cert = $request->file('reg_cert');
$filename = $reg_cert->getClientOriginalName();
//$destinationPath = config('app.fileDestinationPath').'/'.$filename;
$file = $request->file('reg_cert');
$ext = $file->getClientOriginalExtension();
Storage::putFileAs(
'reg_cert',/*folder name*/ $file, $filename. '.' .$ext
);
// $uploaded = Storage::put($destinationPath, file_get_contents($reg_cert->getRealPath()));
// $name = $file->getClientOriginalName();
// $img_file = $request->file('reg_cert');
// Image::make($img_file)->save(public_path('uploads\reg_cert'));
// $input['company_profile'] = $name;
// $request->file('req_cert');
// //$request->image->store('\uploads\reg_cert');
// Storage::putFile('uploads\reg_cert', $request->file('reg_cert'));
}
// else{
// return "failed";
// }
Company::create($input);
//Company::create(request()->all());
//dd(request()->all());
return redirect('/');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
@@ -146,37 +36,8 @@ class CompanyController extends Controller
$company = Company::find($id);
if (!$company)
{
return response()->json(['message'=>'Document not found'],200);
return response()->json(['message'=>'company not found'],200);
}
//updating company profile picture
if($file = $request->file('company_profile')) //company profile picture
{
$company_profile = $request->file('company_profile');
$filename = $company_profile->getClientOriginalName();
//$destinationPath = config('app.fileDestinationPath').'/'.$filename;
$file = $request->file('company_profile');
$ext = $file->getClientOriginalExtension();
Storage::putFileAs(
'company_profile',/*folder name*/ $file, $filename. '.' .$ext
);
}
//updating registration certificate
if($request->hasFile('reg_cert')) //Registration Certificate upload
{
$reg_cert = $request->file('reg_cert');
$filename = $reg_cert->getClientOriginalName();
//$destinationPath = config('app.fileDestinationPath').'/'.$filename;
$file = $request->file('reg_cert');
$ext = $file->getClientOriginalExtension();
Storage::putFileAs(
'reg_cert',/*folder name*/ $file, $filename. '.' .$ext
);
}
$company->company_name=$request->input('company_name');
$company->registration_no=$request->input('registration_no');
@@ -189,34 +50,89 @@ class CompanyController extends Controller
$company->state=$request->input('state');
$company->country=$request->input('country');
$company->contact_person=$request->input('contact_person');
$company->save();
return response()->json(['warehouse'=>$company],200);
// $product = Company::findOrFail($productId);
// $product->update($request->all());
// $product->company_name = 'imagepath';
// $product->save();
//return response()->json($product, 200);
return response()->json(['company'=>$company],200);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
public function companyProfile(Request $request, $id)
{
//
}
$company = Company::find($id);
if (!$company)
{
return response()->json(['message'=>'Document not found'],200);
}
public function uploadImage($request)
$validator = Validator::make($request->all(), [
'company_profile' => 'image|mimes:jpg,jpeg,bmp,png'
]);
if($validator->fails())
{
return "failed";
}
if($file = $request->file('company_profile')) //company profile picture
{
$company_profile = $request->file('company_profile');
$filename = $company_profile->getClientOriginalName();
//$filename = $company_profile->uniqid('img_');
$unique_name = 'comp_prof_' . md5($filename);
//$destinationPath = config('app.fileDestinationPath').'/'.$filename;
$file = $request->file('company_profile');
$ext = $file->getClientOriginalExtension();
$input['company_profile'] = $filename;
Storage::putFileAs(
'company_profile',/*folder name*/ $file, $unique_name. '.' .$ext
);
$company->company_profile = $unique_name. '.' .$ext;
// $name = $file->getClientOriginalName();
// $img_file = $request->file('company_profile');
// Image::make($img_file)->resize(300,300)->save(public_path('\company_profile'));
// //return $img_file;
// // $file->move('uploads/company_profile/', $img_file);
// $input['company_profile'] = $name;
} //end if
$company->save();
return response()->json(['company'=>$company],200);
}//end of companyProfile()
public function regCert(Request $request, $id)
{
// if($request->hasFile('avatar'))
// {
// $avatar = $request->file('avatar');
// $filename = time() . '.' . $avatar->getClientOriginalExtension();
// Image::make($avatar)->resize(300,300)->save(public_path('/uploads/company_profile/'.$filename));
// }
}
}
$company = Company::find($id); //find the company first
if (!$company)
{
return response()->json(['message'=>'Document not found'],200);
}
//validating file types
$validator = Validator::make($request->all(), [
'reg_cert' => 'mimes:jpg,jpeg,bmp,png,gif,svg,pdf'
]);
if($validator->fails()){
return "failed";
}
if($request->hasFile('reg_cert')) //Registration Certificate upload
{
$reg_cert = $request->file('reg_cert');
$filename = $reg_cert->getClientOriginalName();
$unique_name = 'reg_cert_' . md5($filename);
//$destinationPath = config('app.fileDestinationPath').'/'.$filename;
$file = $request->file('reg_cert');
$ext = $file->getClientOriginalExtension();
$input['reg_cert'] = $filename;
Storage::putFileAs(
'reg_cert',/*folder name*/ $file, $unique_name. '.' .$ext
);
$company->reg_cert = $unique_name. '.' .$ext;
}//end if
$company->save();
return response()->json(['company'=>$company],200);
}//end regCert()
}//end of class