Attempt to read property "social_media_channel_name" on string laravel - 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

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

Livewire binding model radio button error

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

How to get the last inserted row with foreach in my blade?

I'm using laravel 5.8 i have two tables askquestions and Responses. When a user post a
question passing in the front-end, i notify all the back-end agent peer mail with the link they access to the show.blade.php here a button to redirect to the Response Form but in this form i'm listing all asked question with a #foreach but i just want to show the last asked question. Need help
<form method="POST" action="{{ route("admin.respones.store") }}" enctype="multipart/form-data">
#csrf
<div class="form-group {{ $errors->has('ask_question') ? 'has-error' : '' }}">
<label class="required" for="ask_question_id"><strong>
{{ trans('La question est:') }}</strong></label>
<select class="form-control select2" name="ask_question_id" id="ask_question_id" required>
#foreach($ask_questions as $id => $ask_question)
<option value="{{ $id }}" {{ old('ask_question_id') == $id ? 'selected' : '' }}>
{{ $ask_question }}</option>
#endforeach
</select>
#if($errors->has('ask_question_id'))
<span class="help-block" role="alert">
{{ $errors->first('ask_question_id') }}
</span>
#endif
<span class="help-block">
{{ trans('') }}
</span>
</div>
public function create()
{
abort_if(Gate::denies('respone_create'), Response::HTTP_FORBIDDEN, '403 Forbidden');
$categories = Category::all()->pluck('name', 'id')->prepend(trans('Sélectionnez la thématique'), '');
$author_emails = User::all()->pluck('email', 'id')->prepend(trans('Choisissez votre email'), '');
$ask_questions = AskQuestion::all()->pluck('text_question', 'id');
return view('admin.respones.create', compact('categories', 'author_emails', 'ask_questions'));
}

Show an attribute from other model in blade (Laravel)

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.

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