Eloquent select statement based on a condition in another table - mysql

I have an laravel eloquent select statement which looks like this:
$test = Test::with(['a.b.companies']) .. and so on
Now, I want to return results for this query based on some company names in companies table.
I tried to write a where clause with various trial and errors but it doesn't work. I am new to laravel and mysql. Any help in the right direction will be good. thanks.

You may use where, for more reference : - Eloquent ORM
$test = Test::where('company_name1', '=', $company_name1)->orWhere('company_name2', '=', $company_name2)->get();

Related

DB Laravel Query, get a datagroup in join for each data

I am using Laravel, I would like to know how I can obtain this result using DB::
[{'id':1,'name':'A',pictures:[{'property_id':1,'filename':'A01'},{'property_id':1,'filename':'A02'},{'property_id':1,'filename':'A03'}]}]
Right now I'm using eloquent relationships and it works fine but it's slow so I want to do it directly with DB::
PROPERTIES
PICTURES
Is it necesary to make it exactly in that format?
To just get all data you want as one object try something like:
DB::table('properties')
->join('pictures', 'property_id',
'=', 'properties.id')
->select('*')
->get()->toArray();

Using SUM in Laravel

I am a new Laravel learner and having difficulty to convert from sql
here is my sql
select sum(employee_income)
from employee
group by employee_id, employee_department
this query works when I test.
here is my simplified Laravel but it doesn't work.
DB::raw('(select sum(employee_income) from employee group by employee_id, employee_department)')
Can anybody see something wrong?
You shouldn't use DB::raw() unless you have to. Laravel has the Eloquent Query Builder, which provides an easy to type, Database-agnostic (works with all DBs) method of writing queries. This one should be pretty simple:
$sum = Employee::groupBy('employee_id')
->groupBy('employee_department')
->sum('employee_income');
// Or, if you don't have an `Employee.php` model
$sum = DB::table('employees')
->groupBy('employee_id')
->groupBy('employee_department')
->sum('employee_income');

how to search by letter in column in database (laravel)

I want to find by letter customer's first_name on my 'customers' table.
And the only thing that I get is an empty array.
For instance, I put in the parameter 'q' value 'E' to get customer Elena from my database by I get an only empty array.
I use the following code to get first_name :
$search = Input::get('q');
if($search)
{
$customers = Customer::all()->where('full_name', 'LIKE', "%{$search}%");
return ($customers);
}
Can someone help me?
Your query don't work because you are calling the all() method before the where(). That's actually not wrong, but it have different behavior.
When you call all(), it actually does the SQL query. After that, any chained methods are being called into a Eloquent Collection class, and it also have a where method, but that's simpler since it runs on PHO instead of running on SQL.
Since the collection's where() method doesn't support LIKE operator, it's probably searching for a value that is exactly %E%.
Hope it can help you understanding why your query doesn't work as expected.
Try this
$customers = Customer::where('full_name', 'LIKE', "%{$search}%")->get();
Laravel Eloquent

Laravel 5.3 Query - Left join some table

I'm trying to get the most recent record for each candidate_id from a ìnterviews` table.
This is what I want to achive:
I'm using Eloquent on laravel and have already tried this methods (with and without eloquent):
$candidates = DB::table('interviews')->select('interviews.*', 'i2.*')
->leftJoin('interviews as i2',
function ($join) {
$join->on('interviews.candidate_id', '=', 'i2.candidate_id');
$join->on('interviews.created_at', '<', 'i2.created_at');
}
)
->whereNull('i2.candidate_id')
->get();
and with eloquent I've tried this:
$candidates = Interview::leftJoin('interviews as i2',
function ($join) {
$join->on('interviews.candidate_id', '=', 'i2.candidate_id');
$join->on('interviews.created_at', '<', 'i2.created_at');
}
)->whereNull('i2.candidate_id')
->get();
If I change get() to toSql() I have exactly the same query that's shown on the above image, but running on laravel I'm getting always these results (this using the first method, with query builder):
Anyone know why I get this results? Is hard to understand that laravel is doing the same query that I do in HeidiSql but I get diferent results :(
Any tip?
Thanks in advance!
Because you are using ->select('interviews.*', 'i2.*') combined with ->whereNull('i2.candidate_id') I am assuming the second select parameter is overriding all fields on the interviews table with nulls, try reversing the order to ->select('i2.*','interviews.*') or not use the i2.* at all.
This is because the output ignores the alias and only uses the fieldname as element key in the returned collection.
Hope it works.
Perfect case scenario you pick the exact columns you want from each of the joined tables for e.g. it may go like this: table1.id,table1.column1,table1.column2,table2.column2 as smth_so_it_doesnt_override

Writing Query in laravel

Hello i'm new to laravel framework
i have a MySQL query .This work perfectly fine.
select sample.name, ABS((COALESCE(sample.openingbalance, 0)) + COALESCE(trs.TotalAmount, 0)) from sample left join (select ledger,sum(amount) AS TotalAmount from transaction group by transaction.ledger) AS trs on sample.name = trs.ledger
I want to write this query so that it is executed in laravel framework
i tried the following query but its not working
DB::table('sample')->select('sample.name',abs((COALESCE('sample.openingbalance',0))+COALESCE('trs.totalamount',0)))->leftjoin('transaction','sample.name','=','transaction.ledger')->select('ledger','sum(amount) as totalamount')->groupBy('transaction.ledger as trs') ->get();
i think what you need is this.
Raw Expressions
Sometimes you may need to use a raw expression in a query. These expressions will be injected into the query as strings, so be careful not to create any SQL injection points! To create a raw expression, you may use the DB::raw method:
sample code
$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();
for more info please refer http://laravel.com/docs/5.1/queries#selects