Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

josue_zacaula's avatar

Model function is not passing query results to view

Hello everyone!

I'm new in this community and with Laravel.

I'm going to create an application that retrieve a lot of numeric data from a database to then manipulate it and make some math operations. I'm facing some troubles with a function and it's not passing query results to the view. Here is my code:

MODEL

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Quote extends Model
{
    use HasFactory;

    public function searchKey($key)
    {
        $unidad = DB::table('vehiculos')
            ->select(DB::raw('id, llave, marca'))
            ->where('llave', $key)
            ->get();

        return $unidad;
    }
}

CONTROLLER

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Quote;

class QuoteController extends Controller
{
    public function index()
    {
        return view('index');
    }

    public function sendForm(Request $request)
    {
        $request->validate([
            'llave' => 'required'
        ]);

        $quote = new Quote();
        $quote->searchKey($request->llave);

        return view('index', [
            'cotizaciones' => $quote
        ]);
    }
}

VIEW

@foreach ($cotizaciones as $cotizacion)
    {{ $cotizacion }}
@endforeach

If I test the model variable dd($unidad), it shows the query results as an array inside a collection, but when I test dd($cotizacion) from the view, then I get a collection with lots of data, but not the query results. What am I doing wrong?

0 likes
9 replies
ziarv's avatar
ziarv
Best Answer
Level 1

Hi your model function is returning value but when you are calling his function in controller it assigns to nothing. try this in your controller $quote = $quote->searchKey($request->llave);

1 like
josue_zacaula's avatar

@ziaarvvy

Thank you Ziaarvvy, it works now and I can access data from the view, but i can't display it. I'm trying with:

@foreach ($cotizaciones as $key => $val)
    {{ $key }} : {{ $val }}
@endforeach

and I get

htmlspecialchars(): Argument #1 ($string) must be of type string, stdClass given

I tryied too with toArray() method in controller :S

Sinnbeck's avatar

@josue_zacaula you need to select which parameter to show

@foreach ($cotizaciones as $key => $val)
    {{ $key }} : {{ $val->id }}
@endforeach 
1 like
Sinnbeck's avatar

Why have models if you don't use them?

    public function searchKey($key)
    {
        $unidad = $this
            ->select('id', 'llave' , 'marca') 
            ->where('llave', $key)
            ->get();

        return $unidad;
    }
josue_zacaula's avatar

@Sinnbeck

Hello Sinnbeck. As I said, I'm new with Laravel and I want to learn the best practices. What should I do to manage custom queries in a correct way?

Thanks for your feedback.

josue_zacaula's avatar

@Sinnbeck I've been looking for tutorials to learn another Laravel functions and not only CRUD operations, but, nothing useful yet or some examples with older versions of Laravel. In this case, do you recommend another way to manage this query?

Please or to participate in this conversation.