Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ignasbernotas/61439f577d227b60172e598325d7a814 to your computer and use it in GitHub Desktop.
Save ignasbernotas/61439f577d227b60172e598325d7a814 to your computer and use it in GitHub Desktop.
Laravel resource scaffolding
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateJobsTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::create('jobs', function (Blueprint $table) {
$table->increments('id');
$table->integer('owner_id')->nullable();
$table->string('title', 30)->nullable();
$table->text('description');
$table->timestamp('valid_until');
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*/
public function down()
{
Schema::dropIfExists('jobs');
}
}
<?php
return [
'controller' => 'Jobs\JobController',
'resource' => 'jobs',
'model' => 'Models\Job',
'database' => [
'table' => 'jobs',
'columns' => [
'id' => 'increments',
'owner_id' => 'integer|nullable',
'title' => 'string:30|nullable',
'description' => 'text',
'valid_until' => 'timestamp',
'timestamps' => true,
'softDeletes' => true
]
],
'requests' => [
'store' => 'Jobs\CreateJobRequest',
'update' => 'Jobs\UpdateJobRequest'
],
'views' => [
'directory' => 'jobs'
]
];
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">Add a Job</div>
<div class="panel-body">
{{ Form::open(['route' => ['jobs.store']]) }}
<div class="form-group @if($errors->has('owner_id')) has-error @endif">
<label for="owner_id-field">Owner Id</label>
{{ Form::text('owner_id', null, ['class' => 'form-control', 'id' => 'owner_id-field']) }}
@if($errors->has('owner_id'))
<span class="help-block">{{ $errors->first('owner_id') }}</span>
@endif
</div>
<div class="form-group @if($errors->has('title')) has-error @endif">
<label for="title-field">Title</label>
{{ Form::text('title', null, ['class' => 'form-control', 'id' => 'title-field']) }}
@if($errors->has('title'))
<span class="help-block">{{ $errors->first('title') }}</span>
@endif
</div>
<div class="form-group @if($errors->has('description')) has-error @endif">
<label for="description-field">Description</label>
{{ Form::text('description', null, ['class' => 'form-control', 'id' => 'description-field']) }}
@if($errors->has('description'))
<span class="help-block">{{ $errors->first('description') }}</span>
@endif
</div>
<div class="form-group @if($errors->has('valid_until')) has-error @endif">
<label for="valid_until-field">Valid Until</label>
{{ Form::text('valid_until', null, ['class' => 'form-control', 'id' => 'valid_until-field']) }}
@if($errors->has('valid_until'))
<span class="help-block">{{ $errors->first('valid_until') }}</span>
@endif
</div>
<div>
<button type="submit" class="btn btn-primary">Create</button>
<a class="btn btn-link pull-right" href="{{ route('jobs.index') }}"><i class="glyphicon glyphicon-backward"></i> Back</a>
</div>
{{ Form::close() }}
</div>
</div>
</div>
</div>
</div>
@endsection
<?php
namespace App\Http\Requests\Jobs;
use Illuminate\Foundation\Http\FormRequest;
class CreateJobRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'owner_id' => '',
'title' => 'max:30',
'description' => 'required',
'valid_until' => 'required',
];
}
}
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">Edit a Job</div>
<div class="panel-body">
{{ Form::model($job, ['route' => ['jobs.update', $job->id], 'method' => 'PUT']) }}
<div class="form-group @if($errors->has('owner_id')) has-error @endif">
<label for="owner_id-field">Owner Id</label>
{{ Form::text('owner_id', null, ['class' => 'form-control', 'id' => 'owner_id-field']) }}
@if($errors->has('owner_id'))
<span class="help-block">{{ $errors->first('owner_id') }}</span>
@endif
</div>
<div class="form-group @if($errors->has('title')) has-error @endif">
<label for="title-field">Title</label>
{{ Form::text('title', null, ['class' => 'form-control', 'id' => 'title-field']) }}
@if($errors->has('title'))
<span class="help-block">{{ $errors->first('title') }}</span>
@endif
</div>
<div class="form-group @if($errors->has('description')) has-error @endif">
<label for="description-field">Description</label>
{{ Form::text('description', null, ['class' => 'form-control', 'id' => 'description-field']) }}
@if($errors->has('description'))
<span class="help-block">{{ $errors->first('description') }}</span>
@endif
</div>
<div class="form-group @if($errors->has('valid_until')) has-error @endif">
<label for="valid_until-field">Valid Until</label>
{{ Form::text('valid_until', null, ['class' => 'form-control', 'id' => 'valid_until-field']) }}
@if($errors->has('valid_until'))
<span class="help-block">{{ $errors->first('valid_until') }}</span>
@endif
</div>
<div>
<button type="submit" class="btn btn-primary">Update</button>
<a class="btn btn-link pull-right" href="{{ route('jobs.index') }}"><i class="glyphicon glyphicon-backward"></i> Back</a>
</div>
{{ Form::close() }}
</div>
</div>
</div>
</div>
</div>
@endsection
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading clearfix">
<h3 class="pull-left">Job list</h3>
<a href="{{ route('jobs.create') }}" class="btn btn-default pull-right">Add Job</a>
</div>
<div class="panel-body">
<table class="table table-condensed table-striped">
<thead>
<tr>
<th>Id</th>
<th>Owner Id</th>
<th>Title</th>
<th>Description</th>
<th>Valid Until</th>
<th>Updated At</th>
<th>Created At</th>
<th>Deleted At</th>
<th class="text-right">Options</th>
</tr>
</thead>
<tbody>
@if($jobs->count())
@foreach($jobs as $job)
<tr>
<td>{{ $job->id }}</td>
<td>{{ $job->owner_id }}</td>
<td>{{ $job->title }}</td>
<td>{{ $job->description }}</td>
<td>{{ $job->valid_until }}</td>
<td>{{ $job->updated_at }}</td>
<td>{{ $job->created_at }}</td>
<td>{{ $job->deleted_at }}</td>
<td class="text-right">
<a class="btn btn-xs btn-primary" href="{{ route('jobs.show', $job->id) }}">
View
</a>
<a class="btn btn-xs btn-warning" href="{{ route('jobs.edit', $job->id) }}">
Edit
</a>
<form action="{{ route('jobs.destroy', $job->id) }}" method="POST"
style="display: inline;"
onsubmit="if(confirm('Are you sure?')) { return true } else {return false };">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-xs btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
@else
<tr>
<td colspan="100" class="text-center">No data.</td>
</tr>
@endif
</tbody>
</table>
{!! $jobs->render() !!}
</div>
</div>
</div>
</div>
</div>
@endsection
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Job extends Model
{
use SoftDeletes;
protected $fillable = ['owner_id', 'title', 'description', 'valid_until'];
protected $dates = ['valid_until'];
}
<?php
namespace App\Http\Controllers\Jobs;
use App\Http\Controllers\Controller;
use App\Http\Requests\Jobs\CreateJobRequest;
use App\Http\Requests\Jobs\UpdateJobRequest;
use App\Models\Job;
class JobController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$columns = ['id', 'owner_id', 'title', 'description', 'valid_until', 'updated_at', 'created_at', 'deleted_at'];
$jobs = Job::select($columns)->paginate();
return view('jobs.index')->with('jobs', $jobs);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('jobs.create');
}
/**
* Store a newly created resource in storage.
*
* @param CreateJobRequest $request
*
* @return \Illuminate\Http\Response
*/
public function store(CreateJobRequest $request)
{
$job = new Job();
$job->owner_id = $request->get('owner_id');
$job->title = $request->get('title');
$job->description = $request->get('description');
$job->valid_until = $request->get('valid_until');
$job->save();
return back()->with('message', 'Job created successfully.');
}
/**
* Display the specified resource.
*
* @param Job $job
*
* @return \Illuminate\Http\Response
*/
public function show(Job $job)
{
return view('jobs.show')->with('job', $job);
}
/**
* Show the form for editing the specified resource.
*
* @param Job $job
*
* @return \Illuminate\Http\Response
*/
public function edit(Job $job)
{
return view('jobs.edit')->with('job', $job);
}
/**
* Update the specified resource in storage.
*
* @param UpdateJobRequest $request
* @param Job $job
*
* @return \Illuminate\Http\Response
*/
public function update(UpdateJobRequest $request, Job $job)
{
$job->owner_id = $request->get('owner_id');
$job->title = $request->get('title');
$job->description = $request->get('description');
$job->valid_until = $request->get('valid_until');
$job->save();
return back()->with('message', 'Job updated successfully.');
}
/**
* Remove the specified resource from storage.
*
* @param Job $job
*
* @return \Illuminate\Http\Response
*/
public function destroy(Job $job)
{
if ($job->delete()) {
return back()->with('message', 'Job removed successfully.');
}
return back()->with('message', 'Failed to remove Job');
}
}
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default">
<div class="panel-heading">Job</div>
<div class="panel-body">
<dl class="dl-horizontal">
<dt>Owner Id</dt>
<dd>{{ $job->owner_id }}</dd>
<dt>Title</dt>
<dd>{{ $job->title }}</dd>
<dt>Description</dt>
<dd>{{ $job->description }}</dd>
<dt>Valid Until</dt>
<dd>{{ $job->valid_until }}</dd>
</dl>
<div>
<a class="btn btn-link pull-right" href="{{ route('jobs.index') }}"><i class="glyphicon glyphicon-backward"></i> Back</a>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
<?php
namespace App\Http\Requests\Jobs;
use Illuminate\Foundation\Http\FormRequest;
class UpdateJobRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'owner_id' => '',
'title' => 'max:30',
'description' => 'required',
'valid_until' => 'required',
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment