I use Laravel DB::select to return specific data from DB,
the problem is only one row return when use print_r($result)
$questions = DB::select('
select text
from question
where id in (?)
order by field(id,?)',
array($question_ids_list_str,$question_ids_list_str));
print_r($questions);
how to return the rest of results?
thanks,
You may try something like this:
$ids = array(1, 2, 3); // Pass the ids in whereIn clause
$questions = DB::table('question')
->whereIn('id', $ids)
->orderBy('id')
->get(array('text')); // Only get text field
Try this:
$questions = DB::select('
select text
from question
where id in (?)
order by field(id,?)',
array($question_ids_list_str,$question_ids_list_str));
foreach($questions as $question) {
echo $question;
}
Related
i want to get data using where in condition
my code is below
SELECT `cuc_UserCardsDetail`.*, `cuc_CreditCardType`.`varValidName`
FROM (`cuc_UserCardsDetail`)
JOIN `cuc_CreditCardType` ON `cuc_CreditCardType`.`intGlCode` =
`cuc_UserCardsDetail`.`fk_CardTypeGlCode`
WHERE `cuc_UserCardsDetail`.`intGlCode` IN ('10,29')
AND `chrAccountType` = 'M'
ORDER BY `dtCreateDate` desc
now I have resulted with one row actually in my table there is two-row with related id
i think problem is In('10,29') but i want like ('10','29') or (10,29) how can i do in codeigitor?
i want (10,29) or ('10','29') in where in ..
In CI query builder use
$id= array('10', '20'); # or direct assing your input params
$this->db->where_in('column_name', $id);
Read Looking for Specific Data - Codeigniter.com
Method chaining will accept array in where_in
$intGlCode = array(10, 29);
$this->db->select('`cuc_UserCardsDetail`.*, `cuc_CreditCardType`.`varValidName`');
$this->db->from('cuc_UserCardsDetail');
$this->db->join('cuc_CreditCardType','cuc_CreditCardType.intGlCode =
cuc_UserCardsDetail.fk_CardTypeGlCode');
$this->db->where('chrAccountType','M');
$this->db->where_in('cuc_UserCardsDetail.intGlCode',$intGlCode);
$this->db->order_by('dtCreateDate','DESC');
$data = $this->db->get()->result_array();
Your query look something like
you have to define column chrAccountType from which table, here i assume it from cuc_UserCardsDetail
$this->db->select('ud.*, cc.varValidName');
$this->db->where_in('ud.intGlCode', ['10','29']);
$this->db->where('ud.chrAccountType', 'M');
$this->db->order_by('dtCreateDate', 'desc');
$this->db->join('cuc_CreditCardType cc', 'cc.intGlCode = ud.fk_CardTypeGlCode');
$data = $this->db->get('cuc_UserCardsDetail ud')->result_array();
print_r($data);
where_in() function just in the case of array of data.
$ids = array();
$this->db->where_in('id', $ids);
I have a simple eloquent query and want to include another table with my results, however, the order of relationship results is incorrect.
Is it possible to order the results without using an SQLRAW statement
$groups = AttributeGroup::with('attribute')->where('page_id', $page->id)->get();
What I would like -
$groups = AttributeGroup::with('attribute')->orderBy('iteration', 'DESC')->where('page_id', $page->id)->get();
I get the error of Unknown column because this column is part of relationship table.
This will order each attribute relation of every attribute group result:
$groups = AttributeGroup::with(['attribute' => function ($query) {
$query->orderBy('iteration', 'DESC');
}])->where('page_id', $page->id)->get();
Is this what you want to achieve?
You can use closures to change the query when using with and has.
$groups = AttributeGroup::with(['attribute' => function($query){
$query->orderBy('iteration');
})->where('page_id', $page->id)->get();
Details are available on https://laravel.com/docs/5.6/eloquent-relationships#constraining-eager-loads
I have two query results.I need to merge the query result based on a condition.
$portfolio = DB::table('architects_portfolio')->get();
$img=DB::table('architects_portfolio_images')
->distinct()->get(['architects_images_id']);
and my condition to merge is
architects_portfolio.id = architects_portfolio_images.architects_images_id
I have my first table 'architects_portfolio' which contain portfolio and second table 'architects_portfolio_images' which contain images of portfolio.I need to get only one image for now.But I get all images of portfolio now by using below code.But I need only one
$architect = User::find($user_id)->architect;
$portfolio = ArchitectsPortfolio::join('architects_portfolio_images as images', 'architects_portfolio.id', '=', 'images.architects_images_id')
->where('architects_portfolio.architects_id', $architect->id)
->select('architects_portfolio.id', 'architects_portfolio.architects_id', 'architects_portfolio.name', 'architects_portfolio.description', 'images.filename','images.architects_images_id')
->distinct()->get(['images.architects_images_id']);
My tables
architects_potfolio
architects_portfolio_images
You should try this:
$rsltArchitectDetails = DB::table('architects_portfolio')
->select('architects_portfolio_images.architects_images_id')
->join('architects_portfolio_images', 'architects_portfolio.id' , '=', 'architects_portfolio_images.architects_images_id')
->distinct()
->get();
$querys = DB::table('architects_portfolio');
$querys->select('architects_portfolio_images.architects_images_id');
$querys->join('architects_portfolio','architects_portfolio.id','=','architects_portfolio_images.architects_images_id')->distinct()->get();
The best way is to use relationships, then you could just load portfolios with related images:
$portfolios = ArhitectPortfolio::with('images')->get();
To make it work, you just need to define the images relationship in the ArhitectPortfolio model:
public function images()
{
return $this->hasMany(ArchitectPortfolioImage::class, 'architects_images_id');
}
Instead of merging we can make use of following code
$data = Architect::where("user_id", $user_id)->first();
$architect = User::find($user_id)->architect;
$portfolio = ArchitectsPortfolio::where('architects_portfolio.architects_id', $architect->id)->paginate(6);
foreach ($portfolio as $r) {
$image = ArchitectsPortfolioImages::where('architects_images_id', $r->id)->first();
if ($image) {
$r->filename = $image->filename;
}
}
How can I get only the first record using multiple IDs in Laravel? In the following code, $ids has three elements. I want to fetch the first record from table using these three IDs.
$ids = [16, 18, 20];
$listing = Listing::whereIn('id', $ids)->first();
If you want to get the first object with smallest id, you can do:
$listing = Listing::whereIn('id', $ids)->oldest('id')->first();
But better to do this:
$listing = Listing::find(min($ids));
If you want to get an object with the first ID in the array, do this:
$listing = Listing::find($ids[0]);
You can use either limit or oldest function of laravel see below query
$listing = Listing::whereIn('id', $ids)->limit(1)->get(); //using limit
OR
$listing = Listing::whereIn('id', $ids)->oldest('id')->first(); //using oldest()
A possible way to do this would be to sort the id column like so:
$listing = Listing::whereIn('id', $ids)->orderBy('id')->first();
I think you should try this:
$ids=[16,18,20];
foreach($ids as $id){
$listing[] = Listing::where('id', $id)->first();
}
Hope this work for you!
I have some problems here.
I get a collection which like:
$VenderData=Vender::where('active', '=', '1')->get();
Inside the collection i have a column called 'type' and the data looks like 'A1,A2,A3,'
or
'A1,A3,'
I want to transfer those codes to real names from
another table 'vender_type'.
code name
A1 XX
A2 OO
A3 ZZ
then add a new column into the original collection $VenderData.
How can i do this?
You must split the type attribute (using array explode(string $delimiter , string $string)) , and get the name of each one from vender_type table, try this :
$VenderData = Vender::where('active', '=', '1')->get();
foreach($VenderData as $vend)
{
$vend->new_type = array();
$types = explode(",", $vend->type);
foreach($types as $type)
{
$t = vender_type::where('code', $type)->first();
if($t)
$vend->name_type[] = $t->name;
// or for example : $vend->name_type[$type] = $t->name;
}
}
I hope that will help you.
I think that you need better query than modifying each value after. This vender_type is probably some foreign key in you database so you can get that value in query builder. To find out about foreign keys and joins in laravel eloquent check this.
Basically you need something like this
$users = DB::table('table1')
->join('table2', 'table1.vender_type', '=', 'table2.vender_type')
->select('table1.code', 'table1.name', 'table2.vender_type')
->where('table1.active', '=', '1')
->get();
Just replace table1 and table2 with values of you table names in database and you're done.
Hope it helps