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
Related
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
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
I´ve got this exception in my blade template. I made a relation between my two models (RegisteredCourses y User) and I can see it works in the rest of Blade´s template, except form.blade.php
Trying to get property 'user' of non-object (View: C:\laragon\www\hr-english\resources\views\registeredCourse\form.blade.php) (View: C:\laragon\www\hr-english\resources\views\registeredCourse\form.blade.php)
My idea is to show in my blade template the name of the user, but I need in the value of input the correspondant user_id.
I don´t what is the correct approach to this problem.
<div class="form-group {{ $errors->has('course_id') ? 'has-error' : '' }}">
<label for="course_id" class="col-md-2 control-label">Course</label>
<div class="col-md-10">
<select class="form-control" id="course_id" name="course_id">
<option value="" style="display: none;" {{ old('course_id', optional($registeredCourse)->course_id ?: '') == '' ? 'selected' : '' }} disabled selected>Select course</option>
#foreach ($courses as $key => $course)
<option value="{{ $key }}" {{ old('course_id', optional($registeredCourse)->course_id) == $key ? 'selected' : '' }}>
{{ $course }}
</option>
#endforeach
</select>
{!! $errors->first('course_id', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('user_id') ? 'has-error' : '' }}">
<label for="status_course" class="col-md-2 control-label">Name</label>
<div class="col-md-10">
<input class="form-control" name="user_id" type="text" id="user_id" value="{{ old('user_id', optional($registeredCourse->user->name)) }}" minlength="1" placeholder="Enter name here..."> <!--Problwm here-->
{!! $errors->first('name', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('status_course') ? 'has-error' : '' }}">
<label for="status_course" class="col-md-2 control-label">Status Course</label>
<div class="col-md-10">
<input class="form-control" name="status_course" type="text" id="status_course" value="{{ old('status_course', optional($registeredCourse)->status_course) }}" minlength="1" placeholder="Enter status course here...">
{!! $errors->first('status_course', '<p class="help-block">:message</p>') !!}
</div>
</div>
Use the null coalescing operator: $registeredCourse->user->name ?? null instead of optional($registeredCourse->user->name) in your blade
UPS: Here's a demo showing how this works depending on whether $registeredCourse->user is set or not.
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
I'm working on a Marionette app that has certain repeating UI patterns in Nunjucks, for example:
<div class="custom-input-group">
<input name="username" type="text" id="username" required value="{{ username }}">
<span class="highlight"></span>
<span class="bar"></span>
<label>Username</label>
</div>
<div class="custom-input-group">
<input name="first_name" type="text" id="first_name" required value="{{ first_name }}">
<span class="highlight"></span>
<span class="bar"></span>
<label>First Name</label>
</div>
<div class="custom-input-group">
<input name="last_name" type="text" id="last_name" required value="{{ last_name }}">
<span class="highlight"></span>
<span class="bar"></span>
<label>Last Name</label>
</div>
I'd like to abstract that into either a macro or a partial template, like so:
{% macro field(name, label='', type='text') %}
<div class="custom-input-group">
<input name="{{ name }}" type="{{ type }}" id="{{ name }}" required value={{ name }}>
<span class="highlight"></span>
<span class="bar"></span>
<label>{{ label }}</label>
</div>
{% endmacro %}
So that I can use it like so:
{{ field('username', 'Username') }}
{{ field('first_name', 'First Name') }}
{{ field('last_name', 'Last Name') }}
However, this generates value="username" instead of value="{{ username }}". How can I produce the latter?
Ah I got it. Instead of trying to output another template, I have to pass in the final value to the macro itself, like this:
{% macro field(value, name, label='', type='text') %}
<div class="custom-input-group">
<input name="{{ name }}" type="{{ type }}" id="{{ name }}" required value={{ value }}>
<span class="highlight"></span>
<span class="bar"></span>
<label>{{ label }}</label>
</div>
{% endmacro %}
And then use the macro like this:
{{ field(username, 'username, 'Username') }}
{{ field(first_name, 'first_name', 'First Name') }}
{{ field(last_name, 'last_name', 'Last Name') }}
This is still a little less DRY than I'd like, but heaps better than before.