Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Livewire component offset

I'm trying to get some technicians data trough a laravel controller called inside Livewirecomponent. In some cases, controller returns null cause there is no technicians asociated.

Here is my livewire component rendering a view:

class TechniciansAssociated extends Component
{
    public function render()
    {
        $technicians = (new HomeController())->getTechnicians();

        return view('livewire.dashboard.technicians-associated', [
            'technicians' => $technicians,
        ]);
    }
}

Here function called from home controller:

public function getTechnicians()
{
    $customer_ids = json_decode(User::find(Auth::id())->userCustomers->customer_id,true);

    $technicians_data = DB::connection('mysql')
        -> select('SELECT distinct users.id, users.name, users.surname, users.lastname, users.email, users.cargo, users.photo
           FROM users
           INNER JOIN contractsproductsmulti AS cpm ON users.id = cpm.userId
           INNER JOIN contractsproducts AS cp ON cp.id = cpm.contractproduct_id
           INNER JOIN contracts AS cont ON cont.id = cp.idcontract
           WHERE users.isActive = 1 AND cont.idCustomer IN (?)', $customer_ids
        );

    if (count($technicians_data) > 0){
        return $technicians_data;
    }

    return null;
}

Finally when trying to get main view where render livewire view I've got the error

@section('content')

<section id="dash-cust-info">
    <div class="row p-t-30 p-b-30">
        <div class="col-md-6">
            <h4>Customer info</h4>
        </div>
        <div class="col-md-3">
            <h4>Comm_1</h4>
            <div class="card no-border no-margin">
                @livewire('dashboard.commercials-associated')
            </div>
        </div>
        <div class="col-md-3">
            <h4>Comm_2</h4>
            <div class="card no-border no-margin">
                @livewire('dashboard.technicians-associated')
            </div>
        </div>
    </div>
....

The error:

Undefined offset: 1 (View: /......../resources/views/home.blade.php)
like image 995
Makros Avatar asked Nov 25 '25 22:11

Makros


2 Answers

I guess this answer provide good explanation for this kind of issue in livewire. Leaving it here, So that anyone having this kind of issue in future will get help. You are welcome

like image 130
fahim152 Avatar answered Nov 28 '25 12:11

fahim152


Ok, LiveWire component needs a HTML element before get results on it's own view. E.g:

<div> //missed on my code
    @foreach($users as $user)
        {{$user->id}}
    @endforeach
</div>
like image 36
Makros Avatar answered Nov 28 '25 12:11

Makros



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!