I've two different database tables entries and backers.
I wanted to select the rows from entries by comparing the email address of the user->email and the email in the backers table. Then I need to take the crowd_fund_id of those matching rows to get the rows from the table entries.
I tried the following queries and didn't work:
$donations = DB::table('entries')
->join(
'backers',
function($join, $user){
$join->on(
'entries.crowd_id',
'backers.crowd_fund_id'
)
->where(
'backers.email',
$user->email
);
}
)
->get();
and
$donations = Entries::where(
'crowd_id',
Backers::where('email', $user->email)->pluck('crowd_fund_id')
)->get();
What will be the best way to get the row from entries ?
Your syntax is a little off...
$donations = DB::table('entries')
->join('backers', function($join) use ($user)
{
$join->on('entries.crowd_id', '=', 'backers.crowd_fund_id')
->where('backers.email', '=', $user->email);
})->get();
The second query gives one result because you are using pluck(). If you just want a list (array) of crowd_fund_id, use ->lists('crowd_fund_id') rather than ->pluck().
it would be simpler if you do this
Banker::join('entries','entries.crowd_id','=','bankers.crowd_fund_id')
->where('bankers.email',$user->email)->get();
Related
I have a products table that is connected through model_has_attachments with attachments table. I need to connect first attachment to each product record thought the query builder, but for some reason it just give me few records with model_has_attachments ids and rest is the null
my query builder look as:
$products = DB::table('products')->
leftJoin(DB::raw('(select `model_id`, `attachment_id` from model_has_attachments where model_has_attachments.model_id = id) as model_has_attachments'), 'model_has_attachments.model_id', 'products.id')->
leftJoin('attachments', 'model_has_attachments.attachment_id', '=', 'attachments.id')->
select('products.id', 'products.square', 'products.height', 'products.address', 'products.rooms', 'products.title', 'products.description', 'model_has_attachments.model_id as id_model', 'model_has_attachments.attachment_id')->
where([
['products.deleted_at', '=', null],
]);
I've tried to add limit = 1 in the DB::raw but it just give me the first record of the products table, not a joined table. Can you tell me why?
I also tried different approach, but it takes all the record of attachments which result duplicate products records if product has more than one attachment. I also have tried to add ->limit(1) at the end but it just ignores the method.
leftJoin('model_has_attachments', function ($join) {
$join->on('products.id', '=', 'model_has_attachments.model_id')->where('model_has_attachments.model_type', '=', Product::class);
})->
``
//try this
$products = Product::leftJoin('model_has_attachments', 'products.id', '=', 'model_has_attachments.model_id')
->leftJoin('attachments', 'attachments.id', '=', 'model_has_attachments.attachment_id')
->addSelect('products.*', 'attachments.id as attachment_id')
->where('attachments.is_active',1)
->get();
I'm printing some data in mi index.blade.php file but it returns duplicated values. This is my query:
$hist = DB::table('codigo_sisnova')
->join('llamada', 'codigo_sisnova.idPaciente', '=', 'llamada.id_paciente')
->join('medico', 'llamada.id_medico', '=', 'medico.id_medico')
->where('llamada.status_llamada', 'Finalizada')
->where(function($query){
$query->where('llamada.status_pago', '=', 'Sisnova')
->orWhere('llamada.status_pago', '=', 'RedireccionadaSisnova');
})
->distinct()
->get();
I already tried with unique() but it doesn't work too.
EDIT
The relations between tables are one to may from "codigo_sisnova" to "llamada", if I take out the join with the table "medico" the rows keep duplicating
Every row gets a duplicate
You will have multiple codigo_sisnova rows if you are joining tables that have multiple matches on that table. DISTINCT would not eliminate those since the joined data will make it non-distinct in those results. I would recommend trying to use groupBy() to eliminate the redundant rows.
You are missing the group_by statement. Assuming that you have an ID column in codigo_sisnova, besides adding it in a SELECT clause, you need to add it before your get() method :
$hist = DB::table('codigo_sisnova')
->select('codigo_sisnova.id')
->join('llamada', 'codigo_sisnova.idPaciente', '=', 'llamada.id_paciente')
->join('medico', 'llamada.id_medico', '=', 'medico.id_medico')
->where('llamada.status_llamada', 'Finalizada')
->where(function($query){
$query->where('llamada.status_pago', '=', 'Sisnova')
->orWhere('llamada.status_pago', '=', 'RedireccionadaSisnova');
})
->group_by('codigo_sisnova.id')
->get();
I am trying to get sum of amount of all users group by month.But if I add sum then query not work.It return amount log as blank array But without sum query return all logs data as monthly.
My query
$reports = $user->whereHas('groups', function ($query) use($acceptedGroup) {
$query->whereIn('groups.name',$acceptedGroup);
})->with(
array(
'amountLogs' => function($query) use($year){
$query
->select(
DB::raw('sum(amount) as total')
)
->whereYear('created_at','=', $year)
->groupBy(DB::raw("MONTH(created_at)",'user_id'))->get();
})
);
If I remove
->select(
DB::raw('sum(amount) as total')
)
Then query works
If you creating a specific select on a relationship in your query, you also need to include the foreign key to the related table (in your case, probably the users table)
->select(
'user_id',
DB::raw('sum(amount) as total')
)
This allows Eloquent to relate the records after loading from the database.
Say I have 3 tables in my database.
'my_recipe', 'my_inventory' and 'ingredient'.
The 'my_recipe' table stores a list of raw_id's based on the 'ingredient' table and the 'quantity' need for the recipe. The 'my_inventory' table stores a list of raw_id's and 'have_quantity'.
So let's take a look at what I currently have at the moment. I have the following 2 queries:
First Query:
$recipe = DB::table('my_recipe as tA')
->leftJoin('ingredient as tB', 'tA.raw_id', '=', 'tB.raw_id')
->select('tA.user as user', 'tA.raw_id as raw_id', 'tA.quantity as quantity',
'tB.ingredient_name as ingredient_name')
->where('user', '=', $user)
->where('raw_id', '=', $raw_id)
->get();
Second Query:
$inventory = DB::table('my_inventory as tA')
->leftJoin('ingredient as tB', 'tA.raw_id', '=', 'tB.raw_id')
->select('tA.user as user', 'tA.have_quantity as have_quantity',
'tB.ingredient_name as ingredient_name')
->where('user', '=', $user)
->get();
The first query returns results that look something like this:
{"user":"jack","raw_id":"853","quantity":2,"ingredient_name":"apple"},
{"user":"jack","raw_id":"853","quantity":4,"ingredient_name":"peach"}
The second query returns results that look something like this:
{"user":"jack","have_quantity":30,"ingredient_name":"apple"},
{"user":"jack","have_quantity":20,"ingredient_name":"apple"},
{"user":"jack","have_quantity":10,"ingredient_name":"apple"},
{"user":"jack","have_quantity":1,"ingredient_name":"peach"},
{"user":"jack","have_quantity":1,"ingredient_name":"peach"}
Notice in the second query results I have to get the sum of the ingredients based on the 'ingredient_name' for my ideal output.
How can I get my ideal output in a single query?
My ideal output would look something like this:
{"user":"jack","raw_id":"853","quantity":2,"ingredient_name":"apple","have_quantity":60},
{"user":"jack","raw_id":"853","quantity":4,"ingredient_name":"peach","have_quantity":2}
It's basically the results of the first query with 'have_quantity' totals from the second query.
EDIT:
my_recipe Model:
'user', 'raw_id', 'quantity'
my_inventory Model:
'user', 'raw_id', 'have_quantity'
ingredient Model:
'raw_id', 'ingredient_name'
Note: In the ingredient model there can be rows with the same 'ingredient_name' but have different 'raw_id'.
Based on our chat conversation I managed to get some extra information on the table structure and what was needed to do to get the wanted results.
For those interested the information can be found here
Anyway I ended up creating the query like this:
SELECT
my_recipe.user AS user,
my_recipe.raw_id AS raw_id,
my_recipe.quantity AS quantity,
ingredient.ingredient_name AS ingredient_name,
IFNULL(SUM(my_inventory.have_quantity),0) AS have_quantity
FROM my_recipe
LEFT JOIN ingredient USING(raw_id)
LEFT JOIN ingredient AS ingredients USING(ingredient_name)
LEFT JOIN my_inventory ON my_inventory.raw_id = ingredients.raw_id
WHERE my_recipe.recipe_id = 853
AND my_recipe.user = 'jack'
AND my_inventory.user = 'jack'
GROUP BY ingredient_name;
Now converting into the needed structure:
$inventory = DB::table('my_recipe')
->leftJoin('ingredient', 'my_recipe.raw_id', '=', 'ingredient.raw_id')
->leftJoin('ingredient AS ingredients', 'ingredient.ingredient_name', '=', 'ingredients.ingredient_name')
->leftJoin('my_inventory', 'my_inventory.raw_id', '=', 'ingredients.raw_id')
->select(DB::raw('my_recipe.user AS user,my_recipe.raw_id AS raw_id,my_recipe.quantity AS quantity,ingredient.ingredient_name AS ingredient_name,IFNULL(SUM(my_inventory.have_quantity),0) AS have_quantity'))
->where('my_recipe.recipe_id', '=', $recipe_id)
->where('my_recipe.user', '=', $user)
->where('my_inventory.user', '=', $user)
->groupBy('ingredient_name')
->get();
Maybe this can solve you problem
//replace count by sum
$inventory = DB::table('my_inventory as tA')
->leftJoin('ingredient as tB', 'tA.raw_id', '=', 'tB.raw_id')
->leftJoin('my_recipe as tC', 'tC.raw_id', '=', 'tB.raw_id')
->select(DB::raw('tA.user as user, tB.ingredient_name as ingredient_name, SUM(tA.have_quantity) have_quantity'))
->where('user', '=', $user)
->groupBy('tB.ingredient_name, tA.user')
->get();
Let's try this:
$result = DB::table('ingredient as ing')
->rightJoin('my_recipe as rcp', 'ing.raw_id', '=', 'rcp.raw_id')
->rightJoin('my_inventory as inv', 'ing.raw_id', '=', 'inv.raw_id')
->select(DB::raw('
rcp.user as user,
ing.ingredient_name as ingredient_name,
rcp.have_quantity quantity,
SUM(inv.have_quantity) have_quantity
'))
->where('rcp.user', '=', $user)
->where('rcp.raw_id', '=', $raw_id)
->groupBy('rcp.user, ing.ingredient_name')
->get();
I have a table with email addresses and want to make a disctinct select (dont have duplicate email addresses in my result). The column with the email address is called 'mail_address'
I have tried:
ContactMail::distinct('mail_address')->get();
And:
ContactMail::distinct()->get();
But both give me just the complete table, also the rows where I have the same email address.
The complete query that I am building looks like this:
$list = ContactMail::where('campaign_id', '=', $campaign_id)
->where('mail_address', '!=', '')
->distinct('mail_address')
->get();
What am I doing wrong? I did not find good docu for distinct.
Using distinct in ORM is a bit pointless - in the end every model is distinct.
So I suggest, that you don't load the models, but single field that you need:
$list = ContactMail::where('campaign_id', '=', $campaign_id)
->where('mail_address', '!=', '')
->distinct()
->lists('email_address');
This way you get an array of distinct emails, instead of full models, or incomplete models when using select and get
$list = ContactMail::where('campaign_id', '=', $campaign_id)
->where('mail_address', '!=', '')
->select('mail_address')
->distinct()
->get();