Livewire binding model radio button error - laravel

i'm trying to build a polls system using livewire but i got some errors
here is my blade
#foreach ($questions as $index => $question)
<div class="row">
<div class="box-header with-border">
<i class="fa fa-question-circle text-black fs-20 me-10"></i>
<h4 class="box-title">{{ $question->title }}</h4>
</div>
<div class="box-body">
<div class="demo-radio-button">
#foreach ($answers as $key => $answer)
<input wire:model="question{{ $question->id }}" type="radio"
id="answer{{ $question->id }}{{ $key }}"
class="radio-col-primary" value="{{ $key }}">
<label
for="answer{{ $question->id }}{{ $key }}">{{ $answer }}</label>
#endforeach
#error('question')
<div class="text-danger text-bold">{{$message}}</div>
#enderror
</div>
</div>
</div>
#endforeach
my Livewire class
public $question = [];
public function render() {
$category = PollCategory::findOrFail($this->category->id);
$subCategories = PollSubCategory::where('poll_category_id', $category->id)->get();
$answers = PollAnswer::all();
return view('livewire.polls', compact('category', 'subCategories', 'answers'));
}
The Error is
Property [$question41] not found on component: [polls]
Any help please ?

you did it wrong way,
solution:
<input wire:model="question.{{ $question->id }}"
$question{{ $question->id }} equal $question41
$question.{{ $question->id }} equal to $question[41]
that's why u receive error Property [$question41] not found on component

Related

Attempt to read property "social_media_channel_name" on string laravel

my controller
$id= Auth::guard('artist')->user()->id;
$profiles= Profile::where('id', $id)->get();
$profiless= Profile::findorFail($id);
return view('artists.profile_edit',compact('profiles','profiless'));
my model
protected $casts = [
'social_media_channel_name' =>'array',
'social_media_channel_link' =>'array',
];
my blade
#foreach($profiless as $key => $profiles)
<div class="col-md-6">
<input type="text" class="form-control mb-3 {{ $errors->has('social_media_channel_name') ? 'is-invalid' : '' }}" name="social_media_channel_name[]" value="{{$profiles->social_media_channel_name}}" placeholder="Social Media Channel Name">
</div>
#if($errors->has('social_media_channel_name'))
<div class="invalid-feedback">
{{ $errors->first('social_media_channel_name') }}
</div>
#endif
<div class="col-md-6">
<input type="text" class="form-control mb-3 {{ $errors->has('social_media_channel_link') ? 'is-invalid' : '' }}" name="social_media_channel_link[]" value="{{$profiles->social_media_channel_link}}" placeholder="Social Media Channel Link">
</div>
#if($errors->has('social_media_channel_link'))
<div class="invalid-feedback">
{{ $errors->first('social_media_channel_link') }}
</div>
#endif
#endforeach
here Attempt to read property "social_media_channel_name" on string laravel show error.how to solve
here Attempt to read property "social_media_channel_name" on string laravel show error.how to solve
There's a lot wrong with your code...
$profiles= Profile::where('id', $id)->get();
You're querying based on id, which should return a single row, but you're calling ->get() which returns a Collection... That's pointless.
$profiless= Profile::findorFail($id);
This is more correct, but that variable name is really bad.
#foreach($profiless as $key => $profiles)
You're looping over a single Model instance, which would make $key and $profiles both strings; you don't need this foreach at all. Additionally, you're overwriting the $profiles variable you're passing from the Controller via compact('profiles','profiless'));
Let's fix your code.
$id = auth()->guard('artist')->user()->id;
$profile = Profile::where('user_id', $id)->first();
// I assume `Profile` has a `user_id` column, you'd want to reference this instead of `id`
return view('artists.profile_edit', compact('profile');
profile_edit.blade.php:
<div class="col-md-6">
<input type="text" class="form-control mb-3 {{ $errors->has('social_media_channel_name') ? 'is-invalid' : '' }}" name="social_media_channel_name[]" value="{{ $profile->social_media_channel_name }}" placeholder="Social Media Channel Name">
</div>
#if($errors->has('social_media_channel_name'))
<div class="invalid-feedback">
{{ $errors->first('social_media_channel_name') }}
</div>
#endif
<div class="col-md-6">
<input type="text" class="form-control mb-3 {{ $errors->has('social_media_channel_link') ? 'is-invalid' : '' }}" name="social_media_channel_link[]" value="{{ $profile->social_media_channel_link }}" placeholder="Social Media Channel Link">
</div>
#if($errors->has('social_media_channel_link'))
<div class="invalid-feedback">
{{ $errors->first('social_media_channel_link') }}
</div>
#endif
Edit
If a User can have multiple Profiles, then you can do:
$id = auth()->guard('artist')->user()->id;
$profiles = Profile::where('user_id', $id)->get();
return view('artists.profile_edit', compact('profiles');
And include the loop in your view:
#foreach($profiles as $profile)
// The rest of the code would be the same
#endforeach

Laravel save related data in database

I want to save question_id in answer table using post form
I tried to use foreach or functions but i always got null data
Controller
public function store(Request $request, Survey $survey)
{
$request->validate([
'answer' => 'required',
]);
$survey->option_name = unserialize($survey->option_name);
$answers = new Answer([
'answer' => $request->get('answer'),
'commentaire' => $request->get('commentaire'),
'user_id' => auth()->id(),
'last_ip' => request()->ip(),
'question_id' => $survey->questions,
'survey_id' => $survey->id,
]);
$answers->save();
return redirect()->action('SurveyController#view_survey_answers', [$survey->id]);
}
answers table
question table's row :
id
survey_id
title
timestamp
I got always null data or i tried using where but i got errors like id index doesn't exists...
View :
{!! Form::open(array('action'=>array('AnswerController#store', $survey->id))) !!}
#forelse ($survey->questions as $key=>$question)
<p class="flow-text">Question {{ $key+1 }} - {{ $question->title }}</p>
#if($question->question_type === 'text')
<div class="form-group">
<div class="input-field col s12">
<input id="answer" type="text" name="answer">
<label for="answer">Answer</label>
</div>
</div>
#elseif($question->question_type === 'textarea')
<div class="form-group">
<div class="input-field col s12">
<textarea id="textarea1" class="materialize-textarea" name="{{ $question->id }}[answer]"></textarea>
<label for="textarea1">Textarea</label>
</div>
</div>
#elseif($question->question_type === 'radio')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
#if($value === 'else')
<div class="form-group" style="margin-left: 20px;">
<input name="answer" class="custom-control-input" type="radio" id="{{ $value }}" value="{{$value}}"/>
<label class="custom-control-label" for="{{ $value }}">{{ $value }}</label>
<div id="textboxes" style="display: none">
<br>
<textarea class="form-control" name="commentaire" id="exampleFormControlTextarea1" rows="3" placeholder="Write a large text here ..."></textarea>
</div>
</div>
#else
<p style="margin:0px; padding:0px;">
<div class="form-group" style="margin-left: 20px;">
<input name="answer" class="custom-control-input" type="radio" id="{{ $value }}" value="{{ $value}}"/>
<label class="custom-control-label" for="{{ $value }}">{{ $value }}</label>
</div>
</p>
#endif
#endforeach
#elseif($question->question_type === 'checkbox')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
<div class="form-group">
<input type="checkbox" id="{{ $value }}" name="answer" value="{{$value}}"/>
<label for="{{$value}}">{{ $value }}</label>
</div>
</p>
#endforeach
#endif
<div class="divider" style="margin:10px 10px;"></div>
#empty
<span class='flow-text center-align'>Nothing to show</span>
#endempty
<div class="form-group">
{{ Form::submit('Submit Survey', array('class'=>'btn btn-success mt-4')) }}
</div>
{!! Form::close() !!}

ReflectionException (-1) Class App\Http\Controllers\AnswersController does not exist

I want to save data in a database using the form
I tried to use a form with input text, radios ... and controller to save data in a database with post method
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use App\Survey;
use App\Answer;
use App\Http\Requests;
class Answerscontroller extends Controller
{
public function store(Request $request, Survey $survey)
{
$request->validate([
'answer'=>'required'
]);
$answers = new Answer([
'answer' => $request->get('answer'),
'commentaire' => $request->get('commentaire'),
'user_id' => auth()->id(),
'last_ip' => request()->ip(),
'survey_id' => $survey->id
]);
$answers->save();
return redirect('/survey')->with('success', 'Stock has been added');
}
}
View:
{!! Form::open(array('action'=>array('AnswersController#store', $survey->id))) !!}
#forelse ($survey->questions as $key=>$question)
<p class="flow-text">Question {{ $key+1 }} - {{ $question->title }}</p>
#if($question->question_type === 'text')
<div class="form-group">
<div class="input-field col s12">
<input id="answer" type="text" name="{{ $question->id }}[answer]">
<label for="answer">Answer</label>
</div>
</div>
#elseif($question->question_type === 'textarea')
<div class="form-group">
<div class="input-field col s12">
<textarea id="textarea1" class="materialize-textarea" name="{{ $question->id }}[answer]"></textarea>
<label for="textarea1">Textarea</label>
</div>
</div>
#elseif($question->question_type === 'radio')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
#if($value === 'else')
<div class="form-group" style="margin-left: 20px;">
<input name="answer" class="custom-control-input" type="radio" id="{{ $value }}" value="{{$value}}"/>
<label class="custom-control-label" for="{{ $value }}">{{ $value }}</label>
<div id="textboxes" style="display: none">
<br>
<textarea class="form-control" name="commentaire" id="exampleFormControlTextarea1" rows="3" placeholder="Write a large text here ..."></textarea>
</div>
</div>
#else
<p style="margin:0px; padding:0px;">
<div class="form-group" style="margin-left: 20px;">
<input name="answer" class="custom-control-input" type="radio" id="{{ $value }}" value="{{ $value}}"/>
<label class="custom-control-label" for="{{ $value }}">{{ $value }}</label>
</div>
</p>
#endif
#endforeach
#elseif($question->question_type === 'checkbox')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
<div class="form-group">
<input type="checkbox" id="{{ $value }}" name="answer" value="{{$value}}"/>
<label for="{{$value}}">{{ $value }}</label>
</div>
</p>
#endforeach
#endif
<div class="divider" style="margin:10px 10px;"></div>
#empty
<span class='flow-text center-align'>Nothing to show</span>
#endempty
<div class="form-group">
{{ Form::submit('Submit Survey', array('class'=>'btn btn-success mt-4')) }}
</div>
{!! Form::close() !!}
model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Answer extends Model
{
protected $fillable = ['answer','commentaire','user_id','survey_id','last_ip'];
protected $table = 'Answer';
public function survey() {
return $this->belongsTo(\App\Survey::class);
}
public function question() {
return $this->belongsTo(\App\Question::class);
}
public function user() {
return $this->belongsTo('App\User');
}
}
Error:
ReflectionException (-1) Class App\Http\Controllers\AnswersController
does not exist
Please could you help me to fix that
ps: in the router, I put post method and controller
The problem is about naming. Your controller is Answerscontroller but the Laravel Looks fo AnswersController with capital C. So, check your controller name that should be AnswersController.php and the class name (inside the file AnswersController.php) that sould be AnswersController.

Save options radios or checkbox

I want to save radio from form in database but I get "on" in the database, I don't know why.
I tried to use HTML form with labels, radios and checkbox but I see in the database saving "on".
Controller :
public function store(Request $request, Survey $survey)
{
// remove the token
$arr = $request->except('_token');
foreach ($arr as $key => $value) {
$newAnswer = new Answer();
if (! is_array( $value )) {
$newValue = $value['answer'];
} else {
$newValue = json_encode($value['answer']);
}
$newAnswer->answer = $newValue;
$newAnswer->question_id = $key;
$newAnswer->user_id = Auth::id();
$newAnswer->survey_id = $survey->id;
$newAnswer->save();
$answerArray[] = $newAnswer;
};
return redirect()->action('SurveyController#view_survey_answers', [$survey->id]);
}
view :
{!! Form::open(array('action'=>array('AnswerController#store', $survey->id))) !!}
#forelse ($survey->questions as $key=>$question)
<p class="flow-text">Question {{ $key+1 }} - {{ $question->title }}</p>
#if($question->question_type === 'text')
<div class="input-field col s12">
<input id="answer" type="text" name="{{ $question->id }}[answer]">
<label for="answer">Answer</label>
</div>
#elseif($question->question_type === 'textarea')
<div class="input-field col s12">
<textarea id="textarea1" class="materialize-textarea" name="{{ $question->id }}[answer]"></textarea>
<label for="textarea1">Textarea</label>
</div>
#elseif($question->question_type === 'radio')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
<input name="{{ $question->id }}[answer]" type="radio" id="{{ $key }}" />
<label for="{{ $key }}">{{ $value }}</label>
</p>
#endforeach
#elseif($question->question_type === 'checkbox')
#foreach($question->option_name as $key=>$value)
<p style="margin:0px; padding:0px;">
<input type="checkbox" id="something{{ $key }}" name="{{ $question->id }}[answer]" />
<label for="something{{$key}}">{{ $value }}</label>
</p>
#endforeach
#endif
<div class="divider" style="margin:10px 10px;"></div>
#empty
<span class='flow-text center-align'>Nothing to show</span>
#endforelse
{{ Form::submit('Submit Survey', array('class'=>'btn waves-effect waves-light')) }}
{!! Form::close() !!}
You forgot to give a value attribute to your checkbox and radio inputs, for example something like this:
<input name="{{ $question->id }}[answer]" type="radio" id="{{ $key }}" value="{{ $key}}"/>
<input type="checkbox" id="something{{ $key }}" name="{{ $question->id }}[answer]" value="{{ $key}}"/>

Multiple images update Laravel deleted

Hi there if anyone can help me, I have the code for updating images and it works fine when I upload another image, and I do not touch the others for example the first image I change and I upload an image the three others I do not touch and I click submit edit button and when I go back to the view of edit I can only see the one photo that I changed the three others are gone..
this is the blade:
<form class="form-horizontal" method="POST"
action="{{ action('website\questions\MatchLinesWithPhotosController#updateQuestion', ['id' => $question->id]) }}"
enctype="multipart/form-data">
{{ csrf_field() }}
#include('website.questions.general-update-header')
<div class="row">
<div class="col-lg-5">
<label style="padding-left: 10px">
<h4 class="box-title">#lang('general.options_for_answers'):</h4>
<em class="text-caption" id="caption-question-type"> *#lang('general.chose_correct_multiple_choice_photo')</em>
</label>
</div>
</div>
<div class="optionsFormMatchlinePhotos">
#for($i=0; $i<=(count($answers->where('deleted', 0))/2)-1; $i++)
<div class="col-md-6" id="option{{ $i }}">
<div class="row">
<div class="col-md-6 col-md-offset-2">
<?php $option = $answers->where('deleted', 0)->where('order', $i)->where('is_key', 1)->first(); ?>
<div class="info-box answer-with-photo-option">
<img src="{{url('/')}}/images/answers/{{ $option->id }}/medium/{{ $option->image_path }}" class="answer-image-create" id="image{{ $i }}">
</div> #if($option)<input onchange="readURL(this, {{ $i }});" type="file" name="image{{ $i }}" id="<?php echo 'answer_file'.$i;?>" style="
margin-top: -19%;
padding-bottom: 9%;
" />
#endif
</div>
</div>
<div class="row">
<div class="form-group {{ $errors->has('options.'.$i) ? ' has-error': '' }}">
<div class="col-md-12">
<div class="col-md-3" style="width: 20%;">
{{-- <input type="text" name="options[{{ $i }}]" value="{{ $i }}" style="display :none" /> --}}
</div>
<input type="text" name="matchanswer[{{ $i }}]" value="{{ $i }}" style="display :none" />
<?php $match = $answers->where('deleted', 0)->where('order', $i)->where('is_key', 0)->first(); ?>
<div class="col-md-5">
#if($match)
<input type="text" name="match{{ $i }}" value="{{$match->text}}"
class="form-control" placeholder="#lang('general.match') {{ $i + 1}}">
#else
<input type="text" name="match{{ $i }}" value=""
class="form-control" placeholder="#lang('general.match') {{ $i + 1}}">
#endif
#if($errors->has('match.'.$i))
<div class="col-md-12"></div>
<span class="help-block">
<strong>{{ $errors->first('match.'.$i) }}</strong>
</span>
#endif
</div>
<div class="col-md-4">
<button type="button" value="{{ $i }}" class="btn btn-flat btn-default btn-sm" id="delete_option" title="#lang('buttons.remove_option')">
<i class="fa fa-trash-o" aria-hidden="true"></i>
</button>
</div>
</div>
</div>
</div>
</div>
#endfor
</div>
and here is the edit controller:
public function editQuestionForm($id){
$question = Question::findOrFail($id);
$answers = MatchLineAnswer::where('question_id', $id)->get();
return view('website.questions.partials.matchlineswithphotos.edit-matchlineswithphotos',compact('question', 'answers'));
return session('message');
}
public function updateQuestion(MatchLinesWithPhotosFormRequest $request, $id){
$QuestionController = new QuestionController();
$QuestionController->UpdateQuestion($request, $id);
MatchLineAnswer::where('question_id', $id)->update(['deleted' => 1]);
$to_delete_answers = MatchLineAnswer::select('id')->where('question_id', $id)->get();
MatchLineAnswer::where('question_id', $id)->update(['deleted' => 1]);
$imgController = new ImagesController();
$element = 'answers';
foreach($to_delete_answers as $deleted_answer){
$imgController->deleteImage($element, $deleted_answer->id);
}
foreach ($request['matchanswer'] as $key => $option){
$answer_create = MatchLineAnswer::create([
'question_id' => $id,
'order' => $option,
'is_key' => 1
]);
$img = $request->file('image'. $option);
$create_answer_id = $answer_create->id;
$imgController->uploadAnswerImage($img, $element, $create_answer_id);
$answer_update = MatchLineAnswer::where('id', $create_answer_id)->update(['image_path'=> $imgController->getUniqueNameAttribute()]);
MatchLineAnswer::create([
'question_id' => $id,
'order' => $option,
'text' => strip_tags($request['match'.$option]),
'is_key' => 0
]);
}
$test_id = Question::where('id', $id)->first();
session()->flash('message', 'Question successfully updated');
return redirect()->action('website\tests\CompleteTestController#showAllTestInfo', ['test_id' => $test_id->test_id]);
}

Resources