Problem passing data from one component to another in livewire - laravel

Good day, I am passing information from one component to another from a SELECT, when I do not load the information from the json file it works, but when I extract from the json I have the following error.
ErrorException
Trying to get property 'numero_prestamo_original' of non-object (View: C:\laragon\www\prestamos-cth\resources\views\livewire\prestamos\prestamos.blade.php)
The select field:
<option value="2022006799">2022006799</option>
#foreach ($prestamos as $prestamo => $valor)
<option value="{{ $valor->numero_prestamo_original }}">{{ $valor->numero_prestamo_original }}
</option>
#endforeach
Do you have any idea what is happening. thank you very much.
I have seen on the internet to make the change from -> to [ ] but it doesn't work either.

Thank you all for your help, the solution I found for that problem was to do the search with the required fields in the array and only print what is needed. Thanks for your help.

Related

Why does this Blade component not receive a parameter?

I am using Laravel 9 and trying to understand the components.
I have this 'anonymous' component (extract) :
<option value="{{ $phase->id }}" #if(isset($phase_id) && $phase_id===$phase->id) selected #endif>
{{ $phase->name }}
</option>
The component is called like that :
<x-select-phase id="phase_id" name="phase_id" :phase_id="$event->phase_id"
I am expecting that the var $phase_id exists inside the component. It is not the case, and do not understand why. I tried a lot of things without success. What can I try next?
https://laravel.com/docs/9.x/blade#casing
Component constructor arguments should be specified using camelCase, while kebab-case should be used when referencing the argument names in your HTML attributes.
This means:
:phase-id="$event->phase_id"
should generate a variable:
$phaseId

Laravel 8 multi value select filtering

I'm trying to build Laravel project that will have a multi-select dropdown with list of categories. Selecting category should reload the page and apply filter.
"scopeFilter" in my model is actually doing filtering, but here i just need to find a way to properly form URL. The problem is that this code i have:
<form id="cats-form" action="#" method="GET">
<select multiple class="chosen-select" name="test[]">
#foreach($categories->get() as $cat) //this loop goes through existing categories in the system
#php
//if category is part of URL, pre-select it in the select:
$arr = request()->all();
$selected = '';
if(array_key_exists('test', $arr)) {
$testArr = $arr['test'];
$selected = in_array($cat->id, explode(',', $testArr)) ? 'selected' : '';
}
#endphp
<option {{ $selected }} value="{{ $cat->id }}">{{ $cat->name }}</option>
#endforeach
</select>
</form>
<script>
$(".chosen-select").chosen({ })
$('.chosen-select').on('change', function(evt, params) {
$('#cats-form').submit();
});
</script>
Is actually giving me this URL:
http://localhost:11089/?test%5B%5D=19&test%5B%5D=5
While i actually need this:
http://localhost:11089/?test=19,5
I guess it's a trivial problem to solve for someone with better knowledge of Laravel, can you tell me pls what i'm doing wrong here?
This is rather how the language or framework or library reads an URL query string. Assuming that your prefered way of URL string is a valid format (as I usually see the format ?arr[]=val1&arr[]=val2 and never see the second format that you preferred), both query string formats should be acceptable to PHP.
As Yarin stated that the best way PHP reads an array is the first method, you don't have to worry too much because the decoded URL query string is exactly in the first format.

Laravel - Trying to get property text' of non-object

This is something I've done a million times before so I'm really confused as to why I'm getting this error.
In my Controller, I fetch some results:
$modOfSubchanIDs = Auth::user()->modOf->pluck('id')->toArray();
$modmails = ModMail::whereIn('subchan_id', $modOfSubchanIDs)
->with('subchan')
->with('creator')
->with('appeal')
->orderBy('created_at', 'desc')
->paginate(10);
then in Blade, I try to output the results of the following relationship:
#foreach ($modmails as $modmail)
<div>
{{$modmail->appeal->text}}
</div>
#endforeach
Modmail Model relationship:
public function appeal()
{
return $this->belongsTo(SubchanBanAppeal::class, 'appeal_id');
}
But I get the following error:
ErrorException Trying to get property 'text' of non-object
Strange. So I try just
<div>
{{$modmail->appeal}}
</div>
which returns the following in Blade:
{"id":1,"subchan_id":8,"subchan_ban_id":1,"text":"test","denied":0,"created_by":5003,"created_at":"2021-03-26T00:07:58.000000Z","updated_at":"2021-03-26T00:07:58.000000Z"}
So now I'm really confused. Why is it returning that properly but not when I try a specific column? I also tried {{$modmail->appeal['text']}} but this returns
Trying to access array offset on value of type null
It feels like I'm missing something really basic here...
Did you do the {{ $modmail->appeal }} debug inside the loop? It is likely that most of the $modmails have an appeal, but one or two do not. If it's not the first one that is missing, your test would look right.
The optional() helper method can help in this scenario.
#foreach ($modmails as $modmail)
<div>
{{ optional($modmail->appeal)->text }}
</div>
#endforeach
https://laravel.com/docs/master/helpers#method-optional

Problem accessing data with relations in Laravel

I want to create a multiple select option based on a parent select option. Here is what I have done to achieve this.
On my livewire php file:
public function updatedSelectedMajor($major_id) {
$this->skills = \App\Models\Major::where('major_id',$major_id)->with('mySkills')->get();
}
When I run this with this statement:
#foreach($skills as $skill)
<option value="$skill->mySkills">{{$skill->mySkills}}</option>
#endforeach
I get these results.
[{"skills_id":5,"name":"Digital Marketing","points":10,"created_at":null,"updated_at":null,"pivot":{"major_id":2,"skills_id":5}}]
With this I want to get only the skills_id as the value and name as the option name, but I'm getting error Property [name] does not exist. I tried with pivot, but it doesn't work either.
and when I do this:
<option value="$skill->mySkills[0]->skills_id">{{$skill->mySkills[0]->name}}</option>
it works fine, but I need to get all of the records, not just one.
Blade provides simple directives for working with PHP's loop structures. This directive functions identically to their PHP counterpart foreach, for example:
#foreach($skills->mySkills as $skill)
<option value="{{ $skill->id }}">{{ $skill->name }}</option>
#endforeach
join table
use \App\Models\Major;
public function updatedSelectedMajor($major_id)
{
$myvalue = $this->skills::join('major','major.id','major_id')
->where('major_id',$major_id)
->select('skills.*','major.myskills as myskills');
return $myvalue;
}
in blade
#foreach($skills as $item)
<option value="$item->skills_id">{{$item->mySkills}}</option>
#endforeach

Laravel old for selectbox options

So i'm working on a standalone laravel project (still a bit new to this..). Using the same form blade file adding and editing records. I've set old to the fields for validation checks, however i can't seem to get old working correctly for dropdowns. The select options are loaded in a foreach, and ive got a check to set it to selected if the record on edit matches the dropdown option selected, however when i add the old method to it, select a new option (E.G. "broker") and force validation to throw an error it works as expected & saves whatever i've selected (option "broker"), it saves the option on submit, but then when i go to edit another record (option "assessor") it displays the same option i previously selected (option "broker")...if any of that made sense!
Here's the foreach with option select from the blade file:
<select class="form-control" name="other_party_details[party_type_id]" id="other_party_details[party_type_id]">
<option value="">--- Please Select ---</option>
#foreach($partyTypes as $partyType)
<option value="{{ $partyType->id }}" {{old('other_party_details.party_type_id', !empty($claim->otherParty->party_type_id)) == $partyType->id ? 'selected' : '' }}>
{{ $partyType->label }}
</option>
#endforeach
</select>
What am i doing wrong?
I've exhausted google as for the most it seems to show results for multi select (mine is not), or using LaravelCollective (which i'm not), so running out of places to look!
Thanks in advance
You can try below code
<option value="{{$partyType->id}}" #if(old('party_type_id') == $partyType->id || $claim->otherParty->party_type_id == $partyType->id ) selected #endif> {{ $partyType->label }}</option>

Resources