Created
June 16, 2019 05:52
-
-
Save bogordesaincom/6bfaf12783764788fcd7a6283724d7bc to your computer and use it in GitHub Desktop.
Crud
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use App\DataTables\CompanyDataTable; | |
use App\Http\Requests; | |
use App\Http\Requests\CreateCompanyRequest; | |
use App\Http\Requests\UpdateCompanyRequest; | |
use App\Repositories\CompanyRepository; | |
use Flash; | |
use App\Http\Controllers\AppBaseController; | |
use Response; | |
use App\Models\City; | |
use App\Models\Province; | |
use App\Models\Company; | |
use DB; | |
class CompanyController extends AppBaseController | |
{ | |
private $companyRepository; | |
public function __construct(CompanyRepository $companyRepo) | |
{ | |
$this->companyRepository = $companyRepo; | |
} | |
/** | |
* Display a listing of the Company. | |
* | |
* @param CompanyDataTable $companyDataTable | |
* @return Response | |
*/ | |
public function index(CompanyDataTable $companyDataTable) | |
{ | |
$province = Province::get(); | |
$city = City::with('province')->get(); | |
// dd($city); | |
return $companyDataTable->render('companies.index', compact('province', 'city')); | |
} | |
public function getCity($id) { | |
$city = DB::table('cities')->where('province_id',$id)->pluck('name','id'); | |
return json_encode($city); | |
} | |
public function create() | |
{ | |
$province = Province::get(); | |
$city = City::with('province')->get(); | |
return view('companies.create', compact('province', 'city')); | |
} | |
/** | |
* Store a newly created Company in storage. | |
* | |
* @param CreateCompanyRequest $request | |
* | |
* @return Response | |
*/ | |
public function store(CreateCompanyRequest $request) | |
{ | |
// $input = $request->all(); | |
// $company = new Company([ | |
// 'name' => $request->get('name'), | |
// 'province_id'=> $request->get('province_id'), | |
// 'cities_id'=> $request->get('cities_id') | |
// ]); | |
// $company->save(); | |
$company = new Company; | |
$company->name = $request->get('name'); | |
$province = Province::get(); | |
$city = City::get(); | |
$company->province()->associate($province); | |
$company->city()->associate($city); | |
$company->save(); | |
Flash::success('Company saved successfully.'); | |
return redirect(route('companies.index')); | |
} | |
/** | |
* Display the specified Company. | |
* | |
* @param int $id | |
* | |
* @return Response | |
*/ | |
public function show($id) | |
{ | |
$company = $this->companyRepository->find($id); | |
if (empty($company)) { | |
Flash::error('Company not found'); | |
return redirect(route('companies.index')); | |
} | |
return view('companies.show')->with('company', $company); | |
} | |
/** | |
* Show the form for editing the specified Company. | |
* | |
* @param int $id | |
* | |
* @return Response | |
*/ | |
public function edit($id) | |
{ | |
$company = $this->companyRepository->find($id); | |
if (empty($company)) { | |
Flash::error('Company not found'); | |
return redirect(route('companies.index')); | |
} | |
return view('companies.edit')->with('company', $company); | |
} | |
/** | |
* Update the specified Company in storage. | |
* | |
* @param int $id | |
* @param UpdateCompanyRequest $request | |
* | |
* @return Response | |
*/ | |
public function update($id, UpdateCompanyRequest $request) | |
{ | |
$company = $this->companyRepository->find($id); | |
if (empty($company)) { | |
Flash::error('Company not found'); | |
return redirect(route('companies.index')); | |
} | |
$company = $this->companyRepository->update($request->all(), $id); | |
Flash::success('Company updated successfully.'); | |
return redirect(route('companies.index')); | |
} | |
/** | |
* Remove the specified Company from storage. | |
* | |
* @param int $id | |
* | |
* @return Response | |
*/ | |
public function destroy($id) | |
{ | |
$company = $this->companyRepository->find($id); | |
if (empty($company)) { | |
Flash::error('Company not found'); | |
return redirect(route('companies.index')); | |
} | |
$this->companyRepository->delete($id); | |
Flash::success('Company deleted successfully.'); | |
return redirect(route('companies.index')); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Province Id Field --> | |
<div class="form-group col-sm-6"> | |
{!! Form::label('province_id', 'Province Id:') !!} | |
{!! Form::select('province_id[]', \App\Models\Province::pluck('name', 'id'), null, ['class' => 'form-control', 'id' => 'province_id']) !!} | |
</div> | |
<!-- Cities Id Field --> | |
<div class="form-group col-sm-6"> | |
{!! Form::label('cities_id', 'Cities Id:') !!} | |
{!! Form::select('cities_id', \App\Models\City::pluck('name'), null, ['class' => 'form-control', 'id' => 'cities_id']) !!} | |
</div> | |
<!-- Name Field --> | |
<div class="form-group col-sm-6"> | |
{!! Form::label('name', 'Name:') !!} | |
{!! Form::text('name', null, ['class' => 'form-control']) !!} | |
</div> | |
<!-- Submit Field --> | |
<div class="form-group col-sm-12"> | |
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!} | |
<a href="{!! route('companies.index') !!}" class="btn btn-default">Cancel</a> | |
</div> | |
@section('scripts') | |
<script> | |
$('#cities_id').select2({ | |
theme: 'bootstrap', | |
}); | |
$(document).ready(function() { | |
$('#province_id').select2({ | |
theme: 'bootstrap', | |
}).on('change', function(){ | |
var provinceId = $(this).val(); | |
if(provinceId) { | |
$.ajax({ | |
url: '/okcity/'+provinceId, | |
type:'GET', | |
dataType:'json', | |
beforeSend: function(){ | |
$('#loader').css("visibility", "visible"); | |
}, | |
success:function(data) { | |
$('#cities_id').empty(); | |
$.each(data, function(key, value){ | |
$('#cities_id').append('<option value="'+ key +'">' + value + '</option>'); | |
}); | |
}, | |
complete: function(){ | |
$('#loader').css("visibility", "hidden"); | |
} | |
}); | |
} else { | |
$('#cities_id').empty(); | |
} | |
}); | |
}); | |
</script> | |
@endsection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment