add the following route in app/Http/route.php
Route::get('service/glist/{limit}',['uses'=>'Web\WebserviceController@getGallaryAsJson', 'as'=>'service/glist/limit']); Route::get('service/gedit/{id}',['uses'=>'Web\WebserviceController@editGallaryAsJson', 'as'=>'service/gedit/id']); Route::put('service/gupdate',['uses'=>'Web\WebserviceController@postUpdateGallary', 'as'=>'service/gupdate']); Route::post('service/gupdatebyajax',['uses'=>'Web\WebserviceController@postUpdateGallaryByAjax', 'as'=>'service/gupdatebyajax']); Route::post('service/ginsert',['uses'=>'Web\WebserviceController@inImage', 'as'=>'service/ginsert']); Route::post('service/ginsertbyajax',['uses'=>'Web\WebserviceController@inImageByAjax', 'as'=>'service/ginsertbyajax']); Route::delete('service/delete/{id}',['uses'=>'Web\WebserviceController@destroy', 'as'=>'service/delete/id']);
add a controller to operate CRUD operation as App/Http/Controllers/Web/WebserviceController.php
<?php namespace App\Http\Controllers\Web;
use Auth;
use Illuminate\Contracts\Auth\Guard;
use App\Http\Controllers\Controller;
use App\Models\Web\Web_Content;
use Illuminate\Http\Request;
use App\User;
use App;
use URL;
use App\Models\Web\WebImageGallery;
use App\Models\Web\Web_Section;
use App\Models\Web\Web_Category;
use Carbon\Carbon;
use DB;
use Illuminate\Support\Facades\Input;
use App\Services\Web\WebServices;
use Illuminate\Support\Facades\File;
class WebserviceController extends Controller {
public function getGallaryAsJson(Request $request){
$data = array();
$location = URL::to('/').'/public/images/upload/gallery/';
$limit = $request->route()->parameter('limit')?:'';
$userIns = WebImageGallery::orderBy('id','desc')
->limit($limit)
->get();
foreach($userIns as $userI){
$basePath = null;
if($userI->web_section_id && $userI->web_category_id){
$basePath = $location.$userI->web_sections->section_name.'/'.$userI->web_categorys->category_name.'/';
}else{
$basePath = $location.$userI->web_sections->section_name.'/';
}
$data[] = array(
'id'=>$userI->id,
'title'=>$userI->title,
'image'=>$basePath.$userI->thumb_path,
);
}
if($userIns) {
header('Content-type: application/json');
return json_encode(array('posts'=>$data));
}else{
return json_encode(array('posts'=>"no data found"));
}
}
public function editGallaryAsJson(Request $request,$id){
$userIns = WebImageGallery::find($id);
if($userIns) {
header('Content-type: application/json');
return json_encode($userIns);
}else{
return json_encode(array('posts'=>"no data found"));
}
}
public function postUpdateGallary(Request $request){
$id = $request->input('id');
$insert =null;
$galleryission = null;
$location = null;
$fileObject = null;
if($request->input('file_name')){
$fileObject = new \Symfony\Component\HttpFoundation\File\UploadedFile($request->input('fup_ukmFile'),$request->input('file_name'),$request->input('type'),$request->input('size'),null,TRUE);
}
//$fileObjectTT = new \Symfony\Component\HttpFoundation\File\UploadedFile('G:/image/one_and_all/shari/s1 - Copy - Copy.jpeg',$request->input('file_name'),$request->input('type'),$request->input('size'),null,TRUE);
try{
if ($request->input('id')) {
$location = base_path()."/public/images/upload/gallery/";
if($fileObject){
if($request->input('web_section_id') && $request->input('web_category_id')){
$location = $location.DB::select("select * from web_sections where id='".$request->input('web_section_id')."'")[0]->section_name."/";
$location = $location.DB::select("select * from web_categorys where id='".$request->input('web_category_id')."'")[0]->category_name."/";
if(!File::exists($location)) {
File::makeDirectory($location, 0775, true);
}
}else{
$location = $location.DB::select("select * from web_sections where id='".$request->input('web_section_id')."'")[0]->section_name."/";
if(!File::exists($location)) {
File::makeDirectory($location, 0775, true);
}
}
}
$galleryission = WebImageGallery::find($id);
$galleryission->title = $request->input('fup_title');
if($fileObject){
$galleryission->thumb_path = WebServices::fileUploadThumb($fileObject,true,$location,100,70);
$galleryission->medium_path = WebServices::fileUploadMedium($fileObject,true,$location,300,210);
$galleryission->large_path = WebServices::fileUpload($fileObject,true,$location);
$galleryission->web_section_id= $request->input('web_section_id');
$galleryission->web_category_id= ($request->input('web_category_id'))? $request->input('web_category_id'):null;
}
$galleryission->active = $request->input('fup_active');
$galleryission->update_date = Carbon::now();
$insert = $galleryission->update();
} // end if
if($insert){
return 'true';
}
}catch(Exception $e){
return 'false';
}
}
public function postUpdateGallaryByAjax(Request $request){
$id = $request->input('id');
$insert =null;
$galleryission = null;
$location = null;
$fileObject = null;
$fup_ukmFile = @$_FILES['file'];
if($fup_ukmFile['name']){
$fileObject = new \Symfony\Component\HttpFoundation\File\UploadedFile($fup_ukmFile['tmp_name'],$fup_ukmFile['name'],$fup_ukmFile['type'],$fup_ukmFile['size'],null,TRUE);
}
try{
if ($request->input('id')) {
$location = base_path()."/public/images/upload/gallery/";
if($fileObject){
if($request->input('web_section_id') && $request->input('web_category_id')){
$location = $location.DB::select("select * from web_sections where id='".$request->input('web_section_id')."'")[0]->section_name."/";
$location = $location.DB::select("select * from web_categorys where id='".$request->input('web_category_id')."'")[0]->category_name."/";
if(!File::exists($location)) {
File::makeDirectory($location, 0775, true);
}
}else{
$location = $location.DB::select("select * from web_sections where id='".$request->input('web_section_id')."'")[0]->section_name."/";
if(!File::exists($location)) {
File::makeDirectory($location, 0775, true);
}
}
}
$galleryission = WebImageGallery::find($id);
$galleryission->title = $request->input('fup_title');
if($fileObject){
$galleryission->thumb_path = WebServices::fileUploadThumb($fileObject,true,$location,100,70);
$galleryission->medium_path = WebServices::fileUploadMedium($fileObject,true,$location,300,210);
$galleryission->large_path = WebServices::fileUpload($fileObject,true,$location);
$galleryission->web_section_id= $request->input('web_section_id');
$galleryission->web_category_id= ($request->input('web_category_id'))? $request->input('web_category_id'):null;
}
$galleryission->active = $request->input('fup_active');
$galleryission->update_date = Carbon::now();
$insert = $galleryission->update();
} // end if
if($insert){
return 'true';
}
}catch(Exception $e){
return 'false';
}
}
public function inImage(Request $request){
$insert =null;
$galleryission = null;
$location = null;
$fileObject = new \Symfony\Component\HttpFoundation\File\UploadedFile($request->input('fup_ukmFile'),$request->input('file_name'),$request->input('type'),$request->input('size'),null,TRUE);
//$fileObjectTT = new \Symfony\Component\HttpFoundation\File\UploadedFile('G:/image/one_and_all/shari/s1 - Copy - Copy.jpeg',$request->input('file_name'),$request->input('type'),$request->input('size'),null,TRUE);
try{
if ($request->input('fup_title') && $fileObject) {
$location = base_path()."/public/images/upload/gallery/";
if($request->input('web_section_id') && $request->input('web_category_id')){
$location = $location.DB::select("select * from web_sections where id='".$request->input('web_section_id')."'")[0]->section_name."/";
$location = $location.DB::select("select * from web_categorys where id='".$request->input('web_category_id')."'")[0]->category_name."/";
if(!File::exists($location)) {
File::makeDirectory($location, 0775, true);
}
}else{
$location = @$location.DB::table('web_sections')->select('section_name')->where("web_section_id",$request->input('web_section_id'))->first()->section_name."/";
if(!File::exists($location)) {
File::makeDirectory($location, 0775, true);
}
}
$galleryission = new WebImageGallery();
$galleryission->title = $request->input('fup_title');
$galleryission->thumb_path = WebServices::fileUploadThumb($fileObject,true,$location,100,70);
$galleryission->medium_path = WebServices::fileUploadMedium($fileObject,true,$location,300,210);
$galleryission->large_path = WebServices::fileUpload($fileObject,true,$location);
$galleryission->web_section_id= $request->input('web_section_id');
$galleryission->web_category_id= ($request->input('web_category_id'))? $request->input('web_category_id'):null;
$galleryission->active = $request->input('fup_active');
$galleryission->create_date = Carbon::now();
//$galleryission->create_by = Auth::user()->id ;
$insert = $galleryission->save();
} // end if
if($insert){
return 'true';
}
}catch(Exception $e){
return 'false';
}
}
public function inImageByAjax(Request $request){
$insert =null;
$galleryission = null;
$location = null;
$fup_ukmFile = @$_FILES['file'];
if($fup_ukmFile['name']){
$fileObject = new \Symfony\Component\HttpFoundation\File\UploadedFile($fup_ukmFile['tmp_name'],$fup_ukmFile['name'],$fup_ukmFile['type'],$fup_ukmFile['size'],null,TRUE);
}
try{
if ($fup_ukmFile['name'] && $fileObject) {
$location = base_path()."/public/images/upload/gallery/";
if($request->input('web_section_id') && $request->input('web_category_id')){
$location = $location.DB::select("select * from web_sections where id='".$request->input('web_section_id')."'")[0]->section_name."/";
$location = $location.DB::select("select * from web_categorys where id='".$request->input('web_category_id')."'")[0]->category_name."/";
if(!File::exists($location)) {
File::makeDirectory($location, 0775, true);
}
}else{
$location = @$location.DB::table('web_sections')->select('section_name')->where("web_section_id",$request->input('web_section_id'))->first()->section_name."/";
if(!File::exists($location)) {
File::makeDirectory($location, 0775, true);
}
}
$galleryission = new WebImageGallery();
$galleryission->title = $request->input('fup_title');
$galleryission->thumb_path = WebServices::fileUploadThumb($fileObject,true,$location,100,70);
$galleryission->medium_path = WebServices::fileUploadMedium($fileObject,true,$location,300,210);
$galleryission->large_path = WebServices::fileUpload($fileObject,true,$location);
$galleryission->web_section_id= $request->input('web_section_id');
$galleryission->web_category_id= ($request->input('web_category_id'))? $request->input('web_category_id'):null;
$galleryission->active = $request->input('fup_active');
$galleryission->create_date = Carbon::now();
$insert = $galleryission->save();
}
if($insert){
return 'true';
}
}catch(Exception $e){
return 'false';
}
}
public function destroy(Request $request,$id){
$del = null;
$location ="public/images/upload/gallery/";
$webImageGallerys = WebImageGallery::find($id);
if($webImageGallerys->web_section_id && $webImageGallerys->web_category_id){
$location = $location.DB::select("select * from web_sections where id='".$webImageGallerys->web_section_id."'")[0]->section_name."/";
$location = $location.DB::select("select * from web_categorys where id='".$webImageGallerys->web_category_id."'")[0]->category_name."/";
$thumb_file = $location.$webImageGallerys->thumb_path;
$medium_file = $location.$webImageGallerys->medium_path;
$large_file = $location.$webImageGallerys->large_path;
if(File::exists($thumb_file) || File::exists($medium_file) || File::exists($large_file)) {
File::delete($thumb_file);
File::delete($medium_file);
File::delete($large_file);
}
}else{
$location = $location.DB::select("select * from web_sections where id='".$webImageGallerys->web_section_id."'")[0]->section_name."/";
$thumb_file = $location.$webImageGallerys->thumb_path;
$medium_file = $location.$webImageGallerys->medium_path;
$large_file = $location.$webImageGallerys->large_path;
if(File::exists($thumb_file) || File::exists($medium_file) || File::exists($large_file)) {
File::delete($thumb_file);
File::delete($medium_file);
File::delete($large_file);
}
}
if($webImageGallerys->delete()){
return 'true';
}else{
return 'false';
}
}
}
create a file "http://localhost/ukmodak_content_upload/webservice/restfull/jquery_plugins.js" and add the following javascript code
jQuery(document).ready(function(){ jQuery.ajax({ url: 'http://localhost/ukm/service/glist/10', type: 'get', dataType:'json', success: function(data) { var str ='ok'; if(data){ jQuery.each(data['posts'],function(key,value){ str +='<tr>'; str +='<td>'+value.title+'</td>'; str +='<td><img style="width:100px" src='+value.image+'></td>'; str +='<td><a class="btn btn-sm btn-primary editForm" alt='+value.id+' href="#" return="false">edit</a>'; str +='<a class="btn btn-sm btn-warning deleteData" alt='+value.id+' href="#" return="false">delete</a>'; str +='<a class="btn btn-sm btn-warning addForm" alt='+value.id+' href="#" return="false">Add</a></td>'; str +='</tr>'; }); jQuery("#list").append(str); } } }); }); jQuery(document).on("click",".addForm",function(){ jQuery(".showAddForm").css("display","block"); jQuery(".showEditForm").css("display","none"); }); jQuery(document).on("click",".editForm",function(){ jQuery(".showEditForm").css("display","block"); jQuery(".showAddForm").css("display","none"); var id = jQuery(this).attr("alt"); jQuery.ajax({ url: 'http://localhost/ukm/service/gedit/'+id+'', type: 'get', dataType: 'json', success: function(data) { if(data){ jQuery.each(data,function(key,value){ if(key=='id'){ jQuery("[name='id']").val(value); } if(key=='title'){ jQuery("[name='fup_title']").val(value); } if(key=='web_section_id'){ jQuery("[name='web_section_id']").val(value); } if(key=='web_category_id'){ jQuery("[name='web_category_id']").val(value); } if(key=='active'){ jQuery("[name='active']").val(value); } }); }else{ jQuery(".editForm").css("display","none"); } } }); }); jQuery(document).on("click",".deleteData",function(){ jQuery(".showEditForm").css("display","none"); jQuery(".showAddForm").css("display","none"); var id = jQuery(this).attr("alt"); jQuery.ajax({ url: 'http://localhost/ukm/service/delete/'+id+'', type: 'delete', dataType: 'json', success: function(data) { if(data){ }else{ alert("deleion fail"); } } }); }); jQuery(document).on("click",".updateButton",function(){ //var data = jQuery('form#editForm').serialize(); var formData = new FormData(); var file_data = (jQuery("[name='fup_ukmFile']").val())? jQuery("[name='fup_ukmFile']")[0].files[0]:''; var fup_title = (jQuery("[name='fup_title']").val())? jQuery("[name='fup_title']").val():''; var id = (jQuery("[name='id']").val())? jQuery("[name='id']").val() :''; var web_section_id = (jQuery("[name='web_section_id']").val())? jQuery("[name='web_section_id']").val() :''; var web_category_id = (jQuery("[name='web_category_id']").val())? jQuery("[name='web_category_id']").val() :''; var active = (jQuery("[name='fup_active']").val())? jQuery("[name='fup_active']").val() :''; if(file_data !=''){ formData.append('file', file_data); } formData.append("fup_title", fup_title); formData.append("id", id); formData.append("web_section_id", web_section_id); formData.append("web_category_id", web_category_id); formData.append("fup_active", active); /*jQuery.ajax({ url:'http://localhost/ukm/service/gupdatebyajax', type: 'POST', dataType: 'json', async:false, processData: false, contentType: false, data:{ "file":file_data, "id":id, "fup_title":fup_title, "web_section_id":web_section_id, "web_category_id":web_category_id, "active":active }, success: function(data) { alert(data); } });*/ jQuery.ajax({ url:'http://localhost/ukm/service/gupdatebyajax', type:'POST', data:formData, async:false, processData: false, contentType: false, success: function(data) { if(! data){ alert("update fail"); }else{ jQuery(".showEditForm").css("display","none"); } } }); }); jQuery(document).on("click",".addButton",function(){ var formData = new FormData(); var file_data = (jQuery("[name='a_fup_ukmFile']")[0].files[0])? jQuery("[name='a_fup_ukmFile']")[0].files[0]:''; var fup_title = (jQuery("[name='a_fup_title']").val())? jQuery("[name='a_fup_title']").val():''; var web_section_id = (jQuery("[name='a_web_section_id']").val())? jQuery("[name='a_web_section_id']").val() :''; var web_category_id = (jQuery("[name='a_web_category_id']").val())? jQuery("[name='a_web_category_id']").val() :''; var active = (jQuery("[name='a_fup_active']").val())? jQuery("[name='a_fup_active']").val() :0; formData.append('file', file_data); formData.append("fup_title", fup_title); formData.append("web_section_id", web_section_id); formData.append("web_category_id", web_category_id); formData.append("fup_active", active); jQuery.ajax({ url:'http://localhost/ukm/service/ginsertbyajax', type:'POST', data:formData, async:false, processData: false, contentType: false, success: function(data) { if(! data){ alert("insert fail"); }else{ jQuery(".showAddForm").css("display","none"); } } }); });
create a file "http://localhost/ukmodak_content_upload/webservice/restfull/list.php" and add the following php code
<html> <title>RESTfull by Ajax</title> <head> <link href="bootstrap-3.3.7/dist/css/bootstrap.css" rel="stylesheet" type="text/css" > <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="jquery_plugins.js"></script> <script> jQuery(function(){ jQuery('#editUkm').change( function(event) { var tmppath = URL.createObjectURL(event.target.files[0]); jQuery('#editImg').fadeIn("fast").attr('src',tmppath); alert(json.stringfy(event.target.files[0])); }); }); jQuery(function(){ jQuery('#addUkm').change( function(event) { var tmppath = URL.createObjectURL(event.target.files[0]); jQuery('#addImg').fadeIn("fast").attr('src',tmppath); alert(json.stringfy(event.target.files[0])); }); }); </script> </head> <body> <div class="panel panel-success"> <div class="panel-heading">New Gallery</div> <div class="panel-body"> <table id="list" class="table"> <caption> By Client Ajax</caption> <tr> <th>Title</th> <th>Image</th> <th>action</th> </tr> </table> </div> </div> <div class="panel panel-success showEditForm" style="display:none;"> <div class="panel-heading">Update Gallery</div> <div class="panel-body"> <form id="editForm" class="form-horizontal" role="form" method="POST" action="" enctype="multipart/form-data"> <div class="form-group col-md-4"> <label>Web Section</label> <div> <select name="web_section_id" class="form-control" value=""> <option value="39">Image Gallery</option> </select> </div> </div> <div class="form-group col-md-4"> <label>Web Category</label> <div> <select name="web_category_id" class="form-control" value=""> <option value="20">Image Gallery</option> </select> </div> </div> <div class="col-md-12 col-lg-12 cls"> <div class="form-group col-md-4"> <label>Upload File</label> <div> <input type="file" name="fup_ukmFile" id="editUkm" accept=".gif,.jpg,.jpeg,.png" onchange="previewFile()"> <img src="" height="200" id="editImg" alt="Image preview..."> </div> </div> <div class="form-group col-md-4"> <label>Title</label> <div> <input type="hidden" name="id" value=""/> <input type="text" pattern="^[A-Za-z_-\\]{5,50}" class="form-control" id="fup_title" name="fup_title" value=""> </div> </div> <div class="form-group col-md-4"> <label>Is Publish</label> <div> <select name="fup_active" class="form-control" value=""> <option value="1">Publish</option> <option value="0">Unpublish</option> </select> </div> </div> </div> <div class="form-group"> <div class="col-md-12" align="center"> <input type="hidden" name="update" value="update"/> <button type="button" class="btn btn-primary updateButton"> Submit </button> </div> </div> </form> </div> </div> <div class="panel panel-success showAddForm" style="display:none;"> <div class="panel-heading">New Gallery</div> <div class="panel-body"> <form id="addForm" class="form-horizontal" role="form" method="POST" action="" enctype="multipart/form-data"> <div class="form-group col-md-4"> <label>Web Section</label> <div> <select name="a_web_section_id" class="form-control" value=""> <option value="39">Image Gallery</option> </select> </div> </div> <div class="form-group col-md-4"> <label>Web Category</label> <div> <select name="a_web_category_id" class="form-control" value=""> <option value="20">Image Gallery</option> </select> </div> </div> <div class="col-md-12 col-lg-12 cls"> <div class="form-group col-md-4"> <label>Upload File</label> <div> <input type="file" name="a_fup_ukmFile" id="addUkm" accept=".gif,.jpg,.jpeg,.png" onchange="previewFile()"> <img src="" height="200" id="addImg" alt="Image preview..."> </div> </div> <div class="form-group col-md-4"> <label>Title</label> <div> <input type="text" pattern="^[A-Za-z_-\\]{5,50}" class="form-control" id="fup_title" name="a_fup_title" value=""> </div> </div> <div class="form-group col-md-4"> <label>Is Publish</label> <div> <select name="a_fup_active" class="form-control" value=""> <option value="1">Publish</option> <option value="0">Unpublish</option> </select> </div> </div> </div> <div class="form-group"> <div class="col-md-12" align="center"> <input type="hidden" name="insert" value="insert"/> <button type="button" class="btn btn-primary addButton"> Submit </button> </div> </div> </form> </div> </div> </body> </html>
Total : 26654
Today :3
Today Visit Country :