PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 18, 2022

[FIXED] Implement select function in Eloquent with

 March 18, 2022     eloquent, laravel, laravel-5     No comments   

Issue

I'm trying to implement a simple select method into a eloquent with. So, I have this code that works correctly.

$usersInfo = User::with(['product' => function($query) use($request) {
    $query->whereBetween('date', [$request->get('datefrom'), $request->get('dateto')]);
}])->get();

But if I put a select into the query function, the query stop working and return an empty value into the product array.

$usersInfo = User::with(['product' => function($query) use($request) {
    $query->select(['date', 'name'])
        ->whereBetween('date', [$request->get('datefrom'), $request->get('dateto')]);
}])->get();

I know that if use DB::table() is possible to do this stuff.

So, my question is. If there any possibility to put this select into the with, in Eloquent?

Thanks


Solution

You need to pass the primary key of the other model into the select to retrieve the necessary results. Without seeing the product relation function I cannot accurately determine what should be done, however it will probably be id.

$query->select('id', 'date', 'name')...

Referenced here



Answered By - mbozwood
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing