public function index()
{
$subCategory = Category::where('status' , 0)->where('parent_id' , '!=' , 0)->get();
$categories = Category::where(['status' => 0 , 'parent_id' => 0])->get();
$questions = Question::join('question_options' , 'questions.id','=','question_options.question_id')->where('status' , 0)->get(['questions.*' , 'question_options.options' ]);
return view('questions')->with(['categories' => $categories , 'subCategory' => $subCategory , 'questions' => $questions]);
}
public function addQuestion(Request $request)
{
$categoryName = $request->input('category_id');
$subCategoryName = $request->input('sub_category_id');
$questionDetail = $request->input('question_detail');
$questionOptions = $request->input('options');
$questionAnswer = $request->input('answer');
$questionType = $request->input('type');
$validation = Validator::make($request->all(), [
'category_id' => 'required|max:100',
'question_detail' => 'required|max:100',
'options' => 'required|max:100',
'answer' => 'required|max:100',
'type' => 'required|max:100',
]);
if ($validation->fails()) {
return redirect()->back()->with('$error', $validation->errors()->first());
} else {
$location = 'images/question_images';
$questionImg = "";
$getImage = (new customSlim())->getImages();
if(count($getImage) > 0){
$questionImage = $getImage[0];
$questionImgSave = (new customSlim)->saveFile($questionImage['output']['data'], $questionImage['input']['name'], $location);
$questionImg = $questionImgSave['name'];
}
$question = new Question();
$question->category_id = $categoryName;
$question->question_detail = $questionDetail;
$question->image = ('images/question_images/'. $questionImg);
$question->option_id = $questionAnswer;
$question->type = $questionType;
$saveQuestion = $question->save();
if($saveQuestion){
$QuestionoptionsArray = explode(',', $questionOptions);
foreach($QuestionoptionsArray as $option){
$Questionoptions = new QuestionOption();
$Questionoptions->question_id = $question->id;
$Questionoptions->options = $option;
$Questionoptions->save();
}
}
}
if ($saveQuestion) {
return redirect()->back()->with(Session::flash('alert-success', 'Question Added Successfully!'));
} else {
return redirect()->back()->with(Session::flash('alert-danger', 'Failed to Add Question'));
}
}
The question is how i can show question options together in one line. like (six,eight,five,nine). i have two database one for question and other is for question options where i'm saving question option with explode() along question id. how can i show this?
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2P0rDLJ
via IFTTT
Aucun commentaire:
Enregistrer un commentaire