3

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)
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Makros
  • 352
  • 2
  • 21

2 Answers2

2

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

fahim152
  • 2,531
  • 1
  • 10
  • 29
1

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>
Makros
  • 352
  • 2
  • 21