Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

116
Vistas
How to optimize multiple value query from database in laravel

I am new to laravel and using simple command to query from database. I am trying to get multiple value from database with same id.

$id = 'P01';
//Get value from database
$fruitname = DB::table('fruits')->where('id', $id)->value('fruitname');
$fruitcolour = DB::table('fruits')->where('id', $id)->value('fruitcolour');
$fruitshape = DB::table('fruits')->where('id', $id)->value('fruitshape');
$updatedat = DB::table('fruits')->where('id', $id)->value('updated_at');

$data = array(
        'fruitname' => $fruitname,
        'fruitcolour' => $fruitcolour,
        'fruitshape' => $fruitshape,
        'updated_at' => $updatedat   
        );

As result, $data can store the value. But i found out that it takes time to complete the process. Is there any ways i can optimize the process?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

I'm not sure why you're making it more hard than it is, but you can just simply use eloquent in this case:

Provided you already have created the necessary model and adding it in your controller:

use App\Fruits;

Just simply use it in the find method:

public function someMethodName()
{
    $id = 'P01';
    $fruit = Fruits::find($id);

    echo $fruit->fruitname; // and others
}
over 4 years ago · Santiago Trujillo Denunciar

0


$fruit = DB::table('fruits')->where('id', $id)->first();

$data = array(
        'fruitname' => $fruit->fruitname,
        'fruitcolour' => $fruit->fruitcolour,
        'fruitshape' => $fruit->fruitshape,
        'updated_at' => $fruit->updatedat   
);

Or if you have a view, you could just do

$fruit = DB::table('fruits')->where('id', $id)->first();
return view('yourview', compact('fruit'));

And in the view, you can access each of the values like so:

{{ $fruit->fruitname }} 
{{ $fruit->fruitcolor }} 
over 4 years ago · Santiago Trujillo Denunciar

0

In your controller add function

public function function_name(Fruit $fruit)
{
 return view('your_view_name', compact('fruit'));
}

In web file set your route as

Route::get('/fruit/{fruit}','YourControllername@methodName')->name('show');

At the time of setting route <a href="{{route('show', ['fruit' => $id])}}">Show</a>

in View file get data as

{{ $fruit->fruitname }} 
{{ $fruit->fruitcolor }}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda