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);
Jul 6, 2022
9
Level 1
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?
Level 1
1 like
Please or to participate in this conversation.