php - eloquent model with empty primary key value

896

I'm using Slim 3 with Eloquent as ORM. I have a product table that contains only 3 fields, productCode, name and price. I am having problems to obtain an attribute of my products.

I have created a model class for products, it is the following:

class Product extends Model
{
    protected $primaryKey = 'productCode';

    public $timestamps = false;

    protected $fillable = [
        'name',
        'price'
    ];
}

Esta la estoy utilizando desde mi controlador para enviarlos como Json, de la siguiente forma:

class ProductsController extends Controller
{

    public function getAll($request, $response)
    {
        $products = Product::all();

        return $response->withJson($products, 200);
    }
}

The problem is that it returns the following:

[{"productCode":0,"name":"producto 1","price":15000},{"productCode":0,"name":"producto 2","price":20000},{"productCode":0,"name":"producto 3","price":5999}]

all the productCode variables are changed by vacuum, so json returns 0.

when reviewing the variable with print_r(), it does contain the productCode correctly, but I have not been able to obtain those values, it always returns empty.

    Store\Models\Product Object
(
    [primaryKey:protected] => productCode
    [timestamps] => 
    [fillable:protected] => Array
        (
            [0] => name
            [1] => price
        )

    [connection:protected] => default
    [table:protected] => 
    [keyType:protected] => int
    [incrementing] => 1
    [with:protected] => Array
        (
        )

    [withCount:protected] => Array
        (
        )

    [perPage:protected] => 15
    [exists] => 1
    [wasRecentlyCreated] => 
    [attributes:protected] => Array
        (
            [productCode] => cod0001
            [name] => producto 1
            [price] => 15000
        )

    [original:protected] => Array
        (
            [productCode] => cod0001
            [name] => producto 1
            [price] => 15000
        )

    [changes:protected] => Array
        (
        )

    [casts:protected] => Array
        (
        )

    [dates:protected] => Array
        (
        )

    [dateFormat:protected] => 
    [appends:protected] => Array
        (
        )

    [dispatchesEvents:protected] => Array
        (
        )

    [observables:protected] => Array
        (
        )

    [relations:protected] => Array
        (
        )

    [touches:protected] => Array
        (
        )

    [hidden:protected] => Array
        (
        )

    [visible:protected] => Array
        (
        )

    [guarded:protected] => Array
        (
            [0] => *
        )

)

As you can see, if the variable contains the correct productCode, the problem is that they remain empty when I try to obtain them.

479

Answer

Solution:

You have to tell Eloquent that the primary key isn't auto-incrementing:

class Product extends Model
{
    public $incrementing = false;
}

People are also looking for solutions to the problem: php - how to allow to enter special characters in text field in laravel

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.