Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!