eternalchaos520
4 months ago

htmlspecialchars() expects parameter 1 to be string, array given

Posted 4 months ago by eternalchaos520

Hi Community,

when I tried to cast sc_cp, sc_material, sc_grade to array in model.

Store method is work perfectly by storing the data into database and the field does store as array, however when I tried to passing to view in show method, it returns the error as below.

htmlspecialchars() expects parameter 1 to be string, array given.

Another question is that how do I get the old value in edit method and update accordingly in the dynamic input box.

Please note that there is only 1 model involved.

Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Sc extends Model
{
    protected $fillable = [
        'sc_customer',
        'sc_model',
        'sc_no',
        'quantity',
        'sc_name',
        'refer_sc_no',
        'mo_no',
        'sc_cp',
        'sc_material',
        'sc_grade',
    ];

    protected $casts = [
        'sc_cp' => 'array',
        'sc_material' => 'array',
        'sc_grade' => 'array',
    ];
}

Controller

<?php

namespace App\Http\Controllers;

use App\Sc;
use Illuminate\Http\Request;

class ScController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $scs = Sc::all()->toArray();

        return view('sc.index', compact('scs'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('sc.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = array

        $sc = new Sc([
            'sc_customer' => $request->get('sc_customer'),
            'sc_model' => $request->get('sc_model'),
            'sc_no' => $request->get('sc_no'),            
            'quantity' => $request->get('quantity'),
            'sc_name' => $request->get('sc_name'),
            'refer_sc_no' => $request->get('refer_sc_no'),
            'mo_no' => $request->get('mo_no'),
            'sc_cp' => $request->get('sc_p'),
            'sc_material' => $request->get('sc_material'),
            'sc_grade' => $request->get('sc_grade'),
        ]);

        $sc->save();

        return redirect('sc');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $sc = sc::find($id);

        return view('sc.show', compact('sc', 'id'));
    }

}

create.blade.php

@extends('layouts.app')

@section('styles')
    <style>
        #print-btn {
            display: none;
        }
    </style>
@endsection

@section('content')
    <div class="container py-5">
        <div class="row">
            <div class="col-md-12">
                <div class="row">
                    <div class="col-md-10 offset-md-1">
                        <span class="anchor" id="formUserEdit"></span>
                        <!-- MyTech Part Description Form -->
                        <div class="card card-outline-secondary">
                            <div class="card-body">
                                <form class="form" role="form" autocomplete="off" method="post" action="{{ url('sc') }}">
                                    {{ csrf_field() }}
                                    <div class="form-group row">
                                        <label class="col-lg-3 col-form-label form-control-label" id="sc_customer_label">Customer:</label>
                                        <div class="col-lg-9">
                                            <input class="form-control" id="sc_customer_input" type="text" name="sc_customer" value="" required>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-lg-3 col-form-label form-control-label" id="sc_model_label">Model:</label>
                                        <div class="col-lg-9">
                                            <input class="form-control" id="sc_model_input" type="text" name="sc_model" value="" required>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-lg-3 col-form-label form-control-label" id="sc_no_label">Part No:</label>
                                        <div class="col-lg-9">
                                            <input class="form-control" id="sc_no_input" type="text" name="sc_no" value="" required>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-lg-3 col-form-label form-control-label" id="quantity_label">Quantity:</label>
                                        <div class="col-lg-9">
                                            <input class="form-control" id="quantity_input" type="text" name="quantity" value="" required>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-lg-3 col-form-label form-control-label" id="sc_name_label">Part Name:</label>
                                        <div class="col-lg-9">
                                            <input class="form-control" id="sc_name_input" type="text" name="sc_name" value="" required>
                                        </div>
                                    </div>
                                    <div class="form-group row">
                                        <label class="col-lg-3 col-form-label form-control-label" id="refer_sc_no_label">Refer. No:</label>
                                        <div class="col-lg-9">
                                            <input class="form-control" id="refer_sc_no_input" type="text" name="refer_sc_no" value="" required>
                                        </div>
                                    </div>                                    
                                    <div class="form-group row">
                                        <label class="col-lg-3 col-form-label form-control-label" id="mo_no_label">Manufacturing No:</label>
                                        <div class="col-lg-9">
                                            <input class="form-control" id="mo_no_input" type="text" name="mo_no" value="" required>
                                        </div>
                                    </div>
                                    <div class="row">
                                        <table class="table table-bordered">
                                            <thead>
                                                <tr>
                                                    <td class="align-middle">CP:</td>
                                                    <td class="align-middle">Material:</td>
                                                    <td class="align-middle">Grade:</td>
                                                    <td class="align-middle">
                                                        <a href="#" class="btn btn-primary addRow">Add</a>
                                                    </td>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <td>
                                                    <input type="text" name="sc_cp[]" class="form-control">
                                                </td>
                                                <td>
                                                    <input type="text" name="sc_material[]" class="form-control">               
                                                </td>
                                                <td>
                                                    <input type="text" name="sc_grade[]" class="form-control">
                                                </td>
                                                <td class="align-middle">
                                                    <a href="#" class="btn btn-danger removeRow">Remove</a>
                                                </td>                                 
                                            </tbody>
                                        </table>
                                    </div>                                
                                    <div class="form-group row">
                                        <label class="col-lg-3 col-form-label form-control-label"></label>
                                        <div class="col-lg-9">
                                            <input type="reset" class="btn btn-danger" value="Reset">
                                            <input type="submit" class="btn btn-info" value="Save Changes">
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <br><br><br><br>
            </div>
        </div>
        <hr>
    </div>
    <!--/container-->
@endsection

@section('scripts')
    <script>
        $('.addRow').on('click', function(){
            addRow();
        });
        function addRow() {
            var tr = 
            '<tr>' + 
                '<td>' + 
                '<input type="text" name="sc_cp[]" class="form-control">' +
                '</td>' + 
                '<td>' + 
                '<input type="text" name="sc_material[]" class="form-control">' +
                '</td>' +
                '<td>' + 
                '<input type="text" name="sc_grade[]" class="form-control">' +
                '</td>' +
                '<td>' + 
                '<a href="#" class="btn btn-danger removeRow">Remove</a>'
                '</td>' +
            '</tr>';
            $('tbody').append(tr);
        }
        $('tbody').on('click', '.removeRow', function(){
            $(this).parent().parent().remove();
        });        
    </script>
@endsection

show.blade.php

<div class="container">
    <div class="row">
        <div class="col-sm">
            <table>
                <tr>
                    <td>CHILD PART</td>
                    <td colspan="2">MATERIAL</td>
                    <td>GRADE</td>                    
                </tr>
                <tr>
                    <td>{{$sc['sc_cp']}}</td>
                    <td colspan="2">{{$sc['sc_material']}}</td>
                    <td>{{$sc['sc_grade']}}</td>                    
                </tr>
            </table>
        </div>
    </div>
</div>

Please sign in or create an account to participate in this conversation.