-3

I've done a query at Laravel and to check result content, I've used dd, but, for some reason, when I remove the dd(), it throws an exception sayint "Undefined array key 0". However, with dd DO find the key.

Code is this:

public function getFormatosArticulo(Articulo $articulo){
    $formatoRaw = Formato::where('articulo_id', '=', $articulo->id)->get();
    dd($formatoRaw[0]);
    $formato = $formatoRaw[0];
    return $formato;
}

And dd output is this:

dd output

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
Rurru
  • 9
  • 3
  • 5
    If you just want the first result, then just use `->first()` instead of `->get()` and trying to manipulate the collection. If you have a hasOne relation from Articulo to Formato, then you can just return the relation with `return $articulo->formato;` or whatever you have the relation named. – aynber Feb 01 '23 at 20:19
  • The problem of that is that query returns a collection and I need a Formato, that's why I was trying to use $formatoRaw[0] – Rurru Feb 01 '23 at 20:41
  • 3
    `->get()` returns a collection, `->first()` returns a single model. – aynber Feb 01 '23 at 20:48
  • I've used ->first(), but it returns a collection. I've tried to use ->toArray(), but it's still not working. If I wrap $formato->toArray() into the dd, it shows me $formato converted to array, but if I remove the dd(), it throws an exception saying that I've called function toArray() on null . – Rurru Feb 01 '23 at 20:57
  • Are you calling this on multiple different `Articulo`s? Maybe the first time the index 0 works, but on the next ones it's empty so it throws an error. – Knapsack Injury Feb 01 '23 at 21:13
  • 2
    *"I've used `->first()`, but it returns a Collection."* - Can you clarify that? I can't think of many cases where that would be true (nested Collections maybe? But not relevant here.) `->first()` returns a Class instance, or `null`, i.e. `Formato::where(...)->first()` will return a `Formato.php` Class instance, or `null`, neither of which is a Collection. Models share a lot of similar methods, like `Formato::where(...)->first()->toArray()` will work (unless `->first()` returns `null`, but that's a different issue), so are you maybe confusing what a Collection is vs a single Model instance? – Tim Lewis Feb 01 '23 at 21:52
  • Maybe I'm confusing them, I've started using Laravel two weeks ago. What I don't understand is that dd() shows me $formato info but when I try to do any other thing, it's null (why would dd() work with a null object?) – Rurru Feb 01 '23 at 22:40

2 Answers2

0

The reason this happens is that dd stands for “dump and die” so your first iteration goes through but you don’t check the rest because you use die(). A solution to this can be as simple as:

public function getFormatosArticulo(Articulo $articulo) {
    $formatoRaw = Formato::where('articulo_id', '=', $articulo->id)->get();
    if ($formatoRaw) {
        $formato = $formatoRaw[0];
        return $formato;
    }
}

Since you are only interested for the [0] position though a similar approach would be:

public function getFormatosArticulo(Articulo $articulo) {
    $formatoRaw = Formato::where('articulo_id', '=', $articulo->id)->first();
    if ($formatoRaw) {
        return $formatoRaw;
    }
}
pr1nc3
  • 8,108
  • 3
  • 23
  • 36
0

I guess you are calling getFormatosArticulo function for multiple times, and passed not exists id into it. The get() function will always return a empty collection even if no data matched.

Can you test your function use code below and check if id does exists or not?

public function getFormatosArticulo(Articulo $articulo){
    try {
        $formatoRaw = Formato::where('articulo_id', '=', $articulo->id)->get();
        $formato = $formatoRaw[0];
        return $formato;
    catch (Exception $e) {
        dd($articulo->id); // i guess there is no articulo_id equal this in formato table.
    }
}
Charlie
  • 287
  • 11
  • Okay, I think I've found the problem. As you well supposed, there are some Formatos that haven't any articulo associated. The dd showed me first formato data, but when I removed the dd, some formato without any article associated throwed the exception. I think another user answered that but I didn't understand, so thank you both :) – Rurru Feb 02 '23 at 15:40