Laravel : Add text area if the user checked a specific choice (radio) - laravel

I want to display textarea or text input if the user select a specific choice in form
I tried to use if but even the choice (radio) doesn't display
view :
#elseif($question->question_type === 'checkbox')
#foreach($question->option_name as $key=>$value)
#if($value === 'else')
<input type="checkbox" id="{{ $value }}" name="{{ $question->id }}[answer]" value="{{ $value}}"/>
<input type="text" name="{{ $question->id }}[answer]" placeholder="Votre commentaire"/>
<label for="{{$value}}">{{ $value }}</label>
#else
<p style="margin:0px; padding:0px;">
<input type="checkbox" id="{{ $value }}" name="{{ $question->id }}[answer]" value="{{ $value}}"/>
<label for="{{$value}}">{{ $value }}</label>
</p>
#endif
#endforeach
The question's choice is array ,the table :
The else choice doesn't display in the page

Related

Laravel array value get to show one by one

My database
My blade view
But I want to have this
My controller
$profiles= Profile::where('id', $id)->get();
return view('artists.profile_edit',compact('profiles'));
My blade template
#foreach ($profiles as $key => $profile)
<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>
<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>
#endforeach
Here I am trying to get array data and show in blade one by one.
First of all, I think your database needs to be redesigned, because currently it's possible to have the array of names and the array of links a different size, which doesn't make sense.
But using this design, you'll need your "for" loop to loop by number instead by "foreach". Get the minimum array length of names and links, and then loop through those numbers.
Update 1.0
I thinks that these columns from json or longtext MySQL type So, you need to use json_decode method to convert this string into array like so
#foreach (json_decode($profiles['social_media_channel_name']) as $profile)
{{-- `$loop` is a variable that comes with `#for`, `#foreach`, and `#forelse` --}}
<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="{{ json_decode($profiles['social_media_channel_name'])[$loop->index] }}" placeholder="Social Media Channel Name">
</div>
<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="{{ json_decode($profiles['social_media_channel_link'])[$loop->index] }}" placeholder="Social Media Channel Link">
</div>
#endforeach
Well, according to your last comment, this means that $profiles['social_media_channel_name'] from type array So, your code must be like so
#foreach ($profiles['social_media_channel_name'] as $profile)
{{-- `$loop` is a variable that comes with `#for`, `#foreach`, and `#forelse` --}}
<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="{{ json_decode($profiles['social_media_channel_name'])[$loop->index] }}" placeholder="Social Media Channel Name">
</div>
<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="{{ json_decode($profiles['social_media_channel_link'])[$loop->index] }}" placeholder="Social Media Channel Link">
</div>
#endforeach
You can read more alse about The Loop Variable

How to get select menu session data in another page laravel 8

I can successfully get input session data on another page but I can't get select menu selected data in another page.
This is my first form
<form action="{{ route('admin.create.step.one.post') }}" method="POST">
#csrf
<label for=""> Student Name </label>
<input type="text" name="student_name" value="{{ $report->student_name ?? '' }}">
<br>
<label for=""> Email </label>
<input type="text" name="student_email" value="{{ $report->student_email ?? '' }}">
<br>
<label for=""> Phone number </label>
<input type="text" name="student_phone" value="{{ $report->student_phone ?? '' }}">
<label for=""> Group </label>
<select name="group" id="">
<option value="1" {{ $report->group ?? '' }}> Science </option>
<option value="2" {{ $report->group ?? '' }}> Arts </option>
</select>
<button type="submit"> Preview </button>
</form>
I want to get session data in this page.
I can't get select menu session data.
<form action="">
#csrf
<table>
<td> {{ $report->student_name }} </td>
<td> {{ $report->student_email }} </td>
<td> {{ $report->student_phone }} </td>
<td> {{ $report->group }} </td>
</table>
</form>
As far as I understood from your question you want to select the item within your select input.
You are just missing this line:
{{ $report->group == 1 ? 'selected' :'' }}
<form action="{{route('admin.create.step.one.post')}}" method="POST">
#csrf
<label for="student_name">Student Name</label>
<input type="text" name="student_name" id="student_name" value="{{ $report->student_name}}">
<br>
<label for="student_email">Email</label>
<input type="text" name="student_email" id="student_email" value="{{ $report->student_email}}">
<br>
<label for="student_phone">Phone number</label>
<input type="text" name="student_phone" id="student_phone" value="{{ $report->student_phone}}">
<br>
<label for="group">Group</label>
<select name="group" id="group">
<option value="1" {{ $report->group == 1 ? 'selected' :'' }}>Science</option>
<option value="2" {{ $report->group == 2 ? 'selected' :'' }}>Arts</option>
</select>
<button type="submit">Preview</button>
</form>
Now, if you need are storing in the session() something and not getting it please update your question with your Controller and I will update my answer to help you further.
I see that you want to get the selected item when you submitted and an error appeared so if you want to get the old value
you can do so using old() method in your blade.
{{ old('group') == 1 ? 'selected' :'' }}
<form action="{{route('admin.create.step.one.post')}}" method="POST">
#csrf
<label for="student_name">Student Name</label>
<input type="text" name="student_name" id="student_name" value="{{ $report->student_name}}">
<br>
<label for="student_email">Email</label>
<input type="text" name="student_email" id="student_email" value="{{ $report->student_email}}">
<br>
<label for="student_phone">Phone number</label>
<input type="text" name="student_phone" id="student_phone" value="{{ $report->student_phone}}">
<br>
<label for="group">Group</label>
<select name="group" id="group">
<option value="1" {{ old('group') == 1 ? 'selected' :'' }}>Science</option>
<option value="2" {{ old('group') == 2 ? 'selected' :'' }}>Arts</option>
</select>
<button type="submit">Preview</button>
</form>

How to fill the form checkboxes with checked data from database laravel 5.8

I'm having a problem in my update Form,
I create user with multiple roles assigned using checkboxes in my form, and when I need to update a user i get all the data into my update form input fields except the checkbox fields will be empty. Is there a way that I can get the checkbox checked with the data from database ?
Here's my User Model Relationship
public function roles()
{
return $this->belongsToMany(Role::class)->withTimestamps();
}
Here's my Role Model Relationship
public function users()
{
return $this->belongsToMany(User::class)->withTimestamps();
}
Here's my Edit User Form
<form action="{{ route('users.update',['user'=>$user]) }}" method="POST">
#method('PATCH')
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" value="{{ old('name') ?? $user->name }}">
<span class="text-danger">{{ $errors->first('name') }}</span>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" name="email" class="form-control" value="{{ old('email') ?? $user->email }}">
<span class="text-danger">{{ $errors->first('email') }}</span>
</div>
<div class="form-group">
#foreach ($roles as $role)
<label for="role">{{ $role->name }}</label>
<input type="checkbox" name="role[{{$role->id}}]" value="{{ old($role->id) }}"
#if(in_array($role->id,old('role',[]) )) checked #endif >
#endforeach
<span class="text-danger">{{ $errors->first('role') }}</span>
</div>
#csrf
<button class="btn btn-primary" type="submit">Create</button>
</form>
Here's my Edit Function
public function edit(User $user)
{
$roles = Role::all();
return view('users.edit', compact('user', 'roles'));
}
I just wanted to know how can I pull data into my checkboxes from the database and mark them as checked
I guess you should check role id with the user role id:
#if(in_array($role->id, $user->roles->toArray())) checked #endif
Finally I found out the answer
<div class="form-group">
#foreach ($roles as $role)
<label for="role">{{ $role->name }}</label>
<input type="checkbox" name="role[]" value="{{ $role->id }}"
{{ $role->users->contains($user->id) ? 'checked' : '' }}
#if(in_array($role->id,old('role',[]))) checked #endif>
#endforeach
<span class="text-danger">{{ $errors->first('role') }}</span>
</div>

button disappeared after configuration laravel

hello my radiobutton disappeared after a laravel configuration they are used to modify user roles they worked for a moment and then disappeared
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Modifier <strong> {{ $user->name }}</strong></div>
<div class="card-body">
<form action="{{ route('admin.user.update', $user) }}" method="POST">
#csrf
#method('PATCH')
#foreach ($roles as $role)
<div class="form-group form-check">
#foreach ($user->roles as $userRole)
#if ($userRole->id == $role->id)
<input type="radio" class="form-check-input" name="roles[]" value="{{ $userRole }}" checked>
#else
<input type="radio" class="form-check-input" name="roles[]" value="{{ $userRole }}">
#endif
#endforeach
<label for="{{ $role->id }}" class="form-check-label">{{ $role->name }}</label>
</div>
#endforeach
<button type="submit" class="btn btn-primary">Modifier les roles</button>
</form>
You can use contains() with key and value defined:
The contains() method determines whether the collection contains a given item:
#foreach ($roles as $role)
<div class="form-group form-check">
<input type="radio" class="form-check-input" name="roles[]" value="{{ $role->id }}" {{ $user->roles->contains('id', $role->id) ? 'checked' : '' }} >
<label for="{{ $role->id }}" class="form-check-label">{{ $role->name }}</label>
</div>
#endforeach

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}}"/>

Resources