Support the ongoing development of Laravel.io →
Requests Input Eloquent
Last updated 2 years ago.
0
Solution

Let me see if I'm getting this right. The final SQL statement you wanna get is something like this, right?

SELECT VendorZIP FROM Payment WHERE countries IN ('E92000001', 'L93000001', 'M83000003') AND counties IN ('95', 'E10000002', 'E10000002') AND gors in ('A', 'B', 'D')

Then the code would be something like this:

$query = Payment::query();
foreach ($input as $column => $values) {
    $query->whereIn($column, $values);
}
$zip = $query->first()->VendorZIP;

Where Payment should be an Eloquent model.

Last updated 9 years ago.
0

Ah, you made it look so simple :D That is what I am after but I made a slight mistake on my original post. The model is actually Extract which has a scope method because the table name is dynamic. So the controller code would look something like;

query = Extract::fromTable($lookup->dataset);
		
		foreach ($input as $column => $values) {
		    $query->whereIn($column, $values);
		}

	$zip = $query->first()->zip();

which doesn't work at the moment. How do I get this right using scope?

Scope method;

public function scopeFromTable($query, $tableName) 
{
	return $query->from($tableName);
}
0

Doesn't work how? What error does it return?

It looks almost right to me, except the last string should be (given that the field name you want is actually zip):

$zip = $query->first()->zip;

Notice no parentheses after ->zip.

Last updated 9 years ago.
0

Ah, yes. Taking off the parentheses works fine. And returning all the zips I have to do;

$zip[] = $query->select('zip')->get();
0

Nope, $query->get() returns you a Collection of full rows (with all columns). You can then iterate this Collection, something like:

$rows = $query->get();
$zips = [];
foreach ($rows as $row) {
    $zips[] = $row->zip;
}

But there's a handy function if you want just an array of values from a single column:

$zips = $query->lists('zip');
0

If you really want to get the hangs of Laravel's way of doing things, watch Laravel 5 Fundamentals video series. It's long (almost 6 hours), but if you are gonna work with Laravel, it's a worthy investment of your time.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

mikelovely mikelovely Joined 26 Mar 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.