Pregunta

I'm trying to get a record where id = n but when I use "->first()" to get the first record found Laravel give me the first record in my entire table.

In this screenshot the id being searched for is "28" and the rest is a var_dump from what Laravel returns when using find and first together.

https://i.stack.imgur.com/iJNeA.png

The code below is a small snippet from an entire method.

public function update($id)
{
    $success = false;
    $error_code = 400;
    $object = null;
    $updated_at = null;

    if (is_numeric($id) && $id > 0) {
        echo $id;
        $category_type = category_type::find($id)->first();
        return var_dump($category_type);

Please not that without calling first the method works as expected.

¿Fue útil?

Solución

That's the intended behaviour. If you look at the source code, you'll find this method:

public function first($columns = array('*'))
{
    return $this->take(1)->get($columns)->first();
}

In other words, Laravel does the first() call automatically for you when you do find().

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top