I'm getting an Array to string conversion when trying to get a column from a stored procedure using Laravel 5.2

时间:2016-04-25 09:04:25

标签: php laravel-5.2

I've been struggling with this for a while now, so I thought I would post here and see if anyone has a solution as to why it's always throwing an Array to string conversion exception. Here is my code:

Model

public function getPackageReference(Request $request) {
    $package_reference = $request->input("package_reference");

    $result = DB::select("CALL GetPackageReference(?)", array($package_reference));

    return $package_reference .$result;
}

stored procedure:

DELIMITER //
CREATE PROCEDURE GetPackageReference (IN package_reference_param VARCHAR(20))
BEGIN
    SELECT LPAD(CAST((COUNT(DISTINCT package_reference) + 1) AS CHAR(3)), 3, '0') AS package_reference
    FROM package  WHERE package_reference LIKE CONCAT(package_reference_param, '%');
END //
DELIMITER ;

Stored procedure result:

CALL GetPackageReference('T2AGB')

package reference

So in my model I would like it to return the package_reference value.

Cheers,

Tim

2 个答案:

答案 0 :(得分:1)

The $result variable is an array and by typing $package_reference .$result you want to convert it to string and that's what is causing the exception. In PHP, arrays cannot be converted to strings.

答案 1 :(得分:0)

I managed to get the answer I was looking for by changing the model method from:

public function getPackageReference(Request $request) {
    $package_reference = $request->input("package_reference");

    $result = DB::select("CALL GetPackageReference(?)", array($package_reference));

    return $package_reference .$result;
}

to:

public function getPackageReference(Request $request) {
    $package_reference = $request->input("package_reference");

    $result = DB::select("CALL GetPackageReference(?)", array($package_reference));

    return $package_reference .$result[0]->package_reference;
}