Yudhistira
Yudhistira

Reputation: 43

Laravel 5.8 - in_array give me error : 'htmlspecialchars() expects parameter 1 to be string, array given'

I have a function in Controller like this :

public function convert($id) {
        $project        = ProjectMaster::findOrFail($id);
        $items          = ProjectItem::all()->where('id_project_master', $id);
        $deliveryOrder  = ProjectDeliveryOrder::where('id_project', $id)->first();
        $itemsDO        = ProjectItemDeliveryOrder::all()->where('id_deliveryorder', $deliveryOrder->id)->pluck('id_item')->toArray();

        return view('delivery-order-form-add', compact('project', 'value', 'items', 'itemsDO'));
    }

$items give me result :

{
"5": {
"id": 6,
"id_project_master": 6,
"name": "Item 1",
"qty": 2,
"cost": "1,000,000",
"totalcost": "2,000,000",
"rate": "2,000,000",
"totalrate": "4,000,000",
"created_at": "2020-01-24 03:23:25",
"updated_at": "2020-01-24 03:23:25"
},
   "6": {
       "id": 7,
       "id_project_master": 6,
       "name": "Item 2",
       "qty": 2,
       "cost": "2,500,000",
       "totalcost": "5,000,000",
       "rate": "4,000,000",
       "totalrate": "8,000,000",
       "created_at": "2020-01-24 03:23:25",
       "updated_at": "2020-01-24 03:23:25"
    }
}

and $itemsDO give me result :

[
6
]

Then I have a loop where in every loop, do validate if exist in_array($this, $array) in blade view like :

@foreach ($items as $item)
<tr>
    <td class="text-right"><input type="checkbox" class="form-check-input" name="id_item[]" value="{{ $item->id }}" @if(in_array($item->id, $itemsDO)) disabled @endif></td>
</tr>
@endforeach

This validate give me an error htmlspecialchars() expects parameter 1 to be string, array given. I write wrong parameter or in_array not working on blade Laravel?

Upvotes: 0

Views: 523

Answers (3)

akbar
akbar

Reputation: 773

Your error is in if statement.

You should say if condition is true echo disable.In the way that you write disable is an simple string but you should introduce it as html code.

So in brief,you should call disable in echo function

Upvotes: 0

Yudhistira
Yudhistira

Reputation: 43

Solved with this code :

<td class="text-right"><input type="checkbox" class="form-check-input" name="id_item[]" value="{{ $item->id }}" @if(in_array($items[$i]->id, $itemsDO)) disabled @endif></td>

$i is loop increment

Upvotes: 0

Anggara
Anggara

Reputation: 637

Blade expects @if and @endif to be on individual line. You can use ternary IF instead:

@foreach ($items as $item)
<tr>
    <td class="text-right"><input type="checkbox" class="form-check-input" name="id_item[]" value="{{ $item->id }}" {{ (in_array($item->id, $itemsDO)) ?  "disabled" : "" }}></td>
</tr>
@endforeach

Upvotes: 1

Related Questions