Skip to content

Instantly share code, notes, and snippets.

@bogordesaincom
Created January 24, 2020 12:06
Show Gist options
  • Save bogordesaincom/692fe96df07b018bf957ecb80e4c6fc9 to your computer and use it in GitHub Desktop.
Save bogordesaincom/692fe96df07b018bf957ecb80e4c6fc9 to your computer and use it in GitHub Desktop.
Gist Survey
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateAnswerTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('answer', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id');
$table->integer('question_id');
$table->integer('survey_id');
$table->string('answer');
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('answer');
}
}
<?php
namespace App\Http\Controllers;
use App\DataTables\AnswerDataTable;
use App\Http\Requests;
use App\Http\Requests\CreateAnswerRequest;
use App\Http\Requests\UpdateAnswerRequest;
use App\Repositories\AnswerRepository;
use Flash;
use App\Http\Controllers\AppBaseController;
use Response;
class AnswerController extends AppBaseController
{
/** @var AnswerRepository */
private $answerRepository;
public function __construct(AnswerRepository $answerRepo)
{
$this->answerRepository = $answerRepo;
}
/**
* Display a listing of the Answer.
*
* @param AnswerDataTable $answerDataTable
* @return Response
*/
public function index(AnswerDataTable $answerDataTable)
{
return $answerDataTable->render('answers.index');
}
/**
* Show the form for creating a new Answer.
*
* @return Response
*/
public function create()
{
return view('answers.create');
}
/**
* Store a newly created Answer in storage.
*
* @param CreateAnswerRequest $request
*
* @return Response
*/
public function store(CreateAnswerRequest $request)
{
$input = $request->all();
$answer = $this->answerRepository->create($input);
Flash::success('Answer saved successfully.');
return redirect(route('answers.index'));
}
/**
* Display the specified Answer.
*
* @param int $id
*
* @return Response
*/
public function show($id)
{
$answer = $this->answerRepository->find($id);
if (empty($answer)) {
Flash::error('Answer not found');
return redirect(route('answers.index'));
}
return view('answers.show')->with('answer', $answer);
}
/**
* Show the form for editing the specified Answer.
*
* @param int $id
*
* @return Response
*/
public function edit($id)
{
$answer = $this->answerRepository->find($id);
if (empty($answer)) {
Flash::error('Answer not found');
return redirect(route('answers.index'));
}
return view('answers.edit')->with('answer', $answer);
}
/**
* Update the specified Answer in storage.
*
* @param int $id
* @param UpdateAnswerRequest $request
*
* @return Response
*/
public function update($id, UpdateAnswerRequest $request)
{
$answer = $this->answerRepository->find($id);
if (empty($answer)) {
Flash::error('Answer not found');
return redirect(route('answers.index'));
}
$answer = $this->answerRepository->update($request->all(), $id);
Flash::success('Answer updated successfully.');
return redirect(route('answers.index'));
}
/**
* Remove the specified Answer from storage.
*
* @param int $id
*
* @return Response
*/
public function destroy($id)
{
$answer = $this->answerRepository->find($id);
if (empty($answer)) {
Flash::error('Answer not found');
return redirect(route('answers.index'));
}
$this->answerRepository->delete($id);
Flash::success('Answer deleted successfully.');
return redirect(route('answers.index'));
}
}
<?php
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\User;
class Answer extends Model
{
use SoftDeletes;
public $table = 'answer';
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
protected $dates = ['deleted_at'];
public $fillable = [
'user_id',
'question_id',
'survey_id',
'answer'
];
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
'user_id' => 'integer',
'question_id' => 'integer',
'survey_id' => 'integer',
'answer' => 'string'
];
/**
* Validation rules
*
* @var array
*/
public static $rules = [
'user_id' => 'required',
'question_id' => 'required',
'survey_id' => 'required',
'answer' => 'required'
];
public function survey() {
return $this->belongsTo(Survey::class);
}
public function question() {
return $this->belongsTo(Question::class);
}
public function user() {
return $this->belongsTo(User::class);
}
}
<?php
namespace App\Http\Controllers\Front;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Survey;
use App\Models\Question;
use App\Models\Answer;
use DB;
class FrontController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$survey = Survey::find(1);
$surveyId = Survey::pluck('id');
$question = Question::where('survey_id', '=', $survey->id)->get();
$get_question = Question::get()->pluck('id');
// $answer = Answer::with('question')->where('question_id', $question->id)->get();
$answer = Answer::where('question_id', '=', $get_question)->get();
// $get_question = Question::pluck('id')->get();
// $answer = DB::table('answer')->where(function ($query) {
// $get_question = Question::get()->pluck('id');
// $query->where('question_id', '=', $get_question);
// })->get();
return view('home.index', compact('survey', 'question', 'answer', 'surveyId', 'get_question'));
}
/**
* 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)
{
//
}
/**
* 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.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use App\Enums\QuestionType;
class CreateQuestionTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('question', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('survey_id')->references('id')->on('survey');
$table->unsignedInteger('user_id')->references('id')->on('users');
$table->string('title');
$table->enum('question_type', ['Single' => 'single', 'Multiple' => 'multiple'])->default(QuestionType::Multiple);
$table->string('option_name')->nullable();
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('question');
}
}
<?php
namespace App\Http\Controllers;
use App\DataTables\QuestionDataTable;
use App\Http\Requests;
use App\Http\Requests\CreateQuestionRequest;
use App\Http\Requests\UpdateQuestionRequest;
use App\Repositories\QuestionRepository;
use Flash;
use App\Http\Controllers\AppBaseController;
use Response;
use App\Enums\QuestionType;
class QuestionController extends AppBaseController
{
/** @var QuestionRepository */
private $questionRepository;
public function __construct(QuestionRepository $questionRepo)
{
$this->questionRepository = $questionRepo;
}
/**
* Display a listing of the Question.
*
* @param QuestionDataTable $questionDataTable
* @return Response
*/
public function index(QuestionDataTable $questionDataTable)
{
return $questionDataTable->render('questions.index');
}
/**
* Show the form for creating a new Question.
*
* @return Response
*/
public function create()
{
$question_type = QuestionType::labels();
return view('questions.create', compact('question_type'));
}
/**
* Store a newly created Question in storage.
*
* @param CreateQuestionRequest $request
*
* @return Response
*/
public function store(CreateQuestionRequest $request)
{
$input = $request->all();
$question = $this->questionRepository->create($input);
Flash::success('Question saved successfully.');
return redirect(route('questions.index'));
}
/**
* Display the specified Question.
*
* @param int $id
*
* @return Response
*/
public function show($id)
{
$question = $this->questionRepository->find($id);
if (empty($question)) {
Flash::error('Question not found');
return redirect(route('questions.index'));
}
return view('questions.show')->with('question', $question);
}
/**
* Show the form for editing the specified Question.
*
* @param int $id
*
* @return Response
*/
public function edit($id)
{
$question = $this->questionRepository->find($id);
$question_type = QuestionType::labels();
if (empty($question)) {
Flash::error('Question not found');
return redirect(route('questions.index'));
}
return view('questions.edit', compact('question_type'))->with('question', $question);
}
/**
* Update the specified Question in storage.
*
* @param int $id
* @param UpdateQuestionRequest $request
*
* @return Response
*/
public function update($id, UpdateQuestionRequest $request)
{
$question = $this->questionRepository->find($id);
if (empty($question)) {
Flash::error('Question not found');
return redirect(route('questions.index'));
}
$question = $this->questionRepository->update($request->all(), $id);
Flash::success('Question updated successfully.');
return redirect(route('questions.index'));
}
/**
* Remove the specified Question from storage.
*
* @param int $id
*
* @return Response
*/
public function destroy($id)
{
$question = $this->questionRepository->find($id);
if (empty($question)) {
Flash::error('Question not found');
return redirect(route('questions.index'));
}
$this->questionRepository->delete($id);
Flash::success('Question deleted successfully.');
return redirect(route('questions.index'));
}
}
<?php
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Enums\QuestionType;
use MadWeb\Enum\EnumCastable;
class Question extends Model
{
use EnumCastable;
use SoftDeletes;
public $table = 'question';
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
protected $dates = ['deleted_at'];
public $fillable = [
'survey_id',
'user_id',
'title',
'question_type',
'option_name'
];
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
'survey_id' => 'integer',
'user_id' => 'integer',
'title' => 'string',
'question_type' => QuestionType::class,
'option_name' => 'string'
];
/**
* Validation rules
*
* @var array
*/
public static $rules = [
'survey_id' => 'required',
'user_id' => 'required',
'title' => 'required',
'question_type' => 'required'
];
public function survey() {
return $this->belongsTo('App\Models\Survey', 'survey_id');
}
public function user() {
return $this->belongsTo(User::class);
}
public function answers() {
return $this->hasMany(Answer::class);
}
}
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateSurveyTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('survey', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('description');
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('survey');
}
}
<?php
namespace App\Http\Controllers;
use App\DataTables\SurveyDataTable;
use App\Http\Requests;
use App\Http\Requests\CreateSurveyRequest;
use App\Http\Requests\UpdateSurveyRequest;
use App\Repositories\SurveyRepository;
use Flash;
use App\Http\Controllers\AppBaseController;
use Response;
class SurveyController extends AppBaseController
{
/** @var SurveyRepository */
private $surveyRepository;
public function __construct(SurveyRepository $surveyRepo)
{
$this->surveyRepository = $surveyRepo;
}
/**
* Display a listing of the Survey.
*
* @param SurveyDataTable $surveyDataTable
* @return Response
*/
public function index(SurveyDataTable $surveyDataTable)
{
return $surveyDataTable->render('surveys.index');
}
/**
* Show the form for creating a new Survey.
*
* @return Response
*/
public function create()
{
return view('surveys.create');
}
/**
* Store a newly created Survey in storage.
*
* @param CreateSurveyRequest $request
*
* @return Response
*/
public function store(CreateSurveyRequest $request)
{
$input = $request->all();
$survey = $this->surveyRepository->create($input);
Flash::success('Survey saved successfully.');
return redirect(route('surveys.index'));
}
/**
* Display the specified Survey.
*
* @param int $id
*
* @return Response
*/
public function show($id)
{
$survey = $this->surveyRepository->find($id);
if (empty($survey)) {
Flash::error('Survey not found');
return redirect(route('surveys.index'));
}
return view('surveys.show')->with('survey', $survey);
}
/**
* Show the form for editing the specified Survey.
*
* @param int $id
*
* @return Response
*/
public function edit($id)
{
$survey = $this->surveyRepository->find($id);
if (empty($survey)) {
Flash::error('Survey not found');
return redirect(route('surveys.index'));
}
return view('surveys.edit')->with('survey', $survey);
}
/**
* Update the specified Survey in storage.
*
* @param int $id
* @param UpdateSurveyRequest $request
*
* @return Response
*/
public function update($id, UpdateSurveyRequest $request)
{
$survey = $this->surveyRepository->find($id);
if (empty($survey)) {
Flash::error('Survey not found');
return redirect(route('surveys.index'));
}
$survey = $this->surveyRepository->update($request->all(), $id);
Flash::success('Survey updated successfully.');
return redirect(route('surveys.index'));
}
/**
* Remove the specified Survey from storage.
*
* @param int $id
*
* @return Response
*/
public function destroy($id)
{
$survey = $this->surveyRepository->find($id);
if (empty($survey)) {
Flash::error('Survey not found');
return redirect(route('surveys.index'));
}
$this->surveyRepository->delete($id);
Flash::success('Survey deleted successfully.');
return redirect(route('surveys.index'));
}
}
<?php
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Survey extends Model
{
use SoftDeletes;
public $table = 'survey';
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';
protected $dates = ['deleted_at'];
public $fillable = [
'name',
'description'
];
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
'name' => 'string',
'description' => 'string'
];
/**
* Validation rules
*
* @var array
*/
public static $rules = [
'name' => 'required',
'description' => 'required'
];
public function answers() {
return $this->hasMany(Answer::class);
}
public function question() {
return $this->hasMany(Question::class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment