Can't get rows from DB using ->where()->get(); [duplicate] - php

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 2 years ago.
I ve got table in data base called invoices and columns automonth and autoyear with values. I don't know why this function returns empty array. I want to get rows with automonth value and autoyear value. Please help me.
$autoyear = date('Y');
$automonth = date('m');
$autonumber = DB::table("invoices as invoices")
->where('automonth', '=', '$automonth')
->where('autoyear', '=', '$autoyear')
->get();

remove quotes from $automonth and $autoyear :
$autonumber = DB::table("invoices as invoices")
->where('automonth', '=', $automonth)
->where('autoyear', '=', $autoyear)
->get();

you pass your variable in a wrong way:
$autonumber = DB::table("invoices as invoices")
->where('automonth', '=', $automonth)
->where('autoyear', '=', $autoyear)
->get();
there is no need of '' when you pass the your variable names ...

Related

how to join three table in laravel [duplicate]

This question already has answers here:
Laravel join with 3 Tables
(5 answers)
Closed 1 year ago.
i have problem when joining my tables the error says Undefined variable: spps
$siswas = DB::table('siswa')
->join('kelas', 'siswa.id_kelas', '=', 'kelas.id_kelas')
->join('spp', 'spp.id_spp', '=', 'siswa.id_spp' )
->get();
$kelass = DB::table('kelas')->get();
return view('admin.data_siswa',compact('siswas','kelass'));
$spps = DB::table('spp', 'kelas')->get();
return view('admin.data_siswa',compact('siswas','spps'));
}
i dont know how to defining the spp table, any one please suggest me idea
the problem is not in the joining , the problem is with two return statement!
the exception won't reach the last return, you should simply united them.
$siswas = DB::table('siswa')
->join('kelas', 'siswa.id_kelas', '=', 'kelas.id_kelas')
->join('spp', 'spp.id_spp', '=', 'siswa.id_spp' )
->get();
$kelass = DB::table('kelas')->get();
$spps = DB::table('spp', 'kelas')->get();
return view('admin.data_siswa',compact('siswas','spps','kelass'));
pass only one parameter in DB::table($tableName);
$spps = DB::table('spp')->get();

Using where query if data is not null [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have this query
$something = Something::where('height, '>=', $request->heightMin)
->where('height, '<=', $request->heightMax)
->get();
It works but what if I want to use field age. This filed isn't require so I may have value (int) or null. So if i had value "null", I want this field to be ignored. How can I do that?
You can use when to only apply the query when the value exists in the request.
From the docs:
Sometimes you may want clauses to apply to a query only when something
else is true. For instance you may only want to apply a where
statement if a given input value is present on the incoming request.
You may accomplish this using the when method:
$role = $request->input('role');
$users = DB::table('users')
->when($role, function ($query, $role) {
return $query->where('role_id', $role);
})
->get();
The when method only executes the given Closure when the first
parameter is true. If the first parameter is false, the Closure will
not be executed.
Something like this:
$something = Something::where('height', '>=', $request->heightMin)
->where('height', '<=', $request->heightMax)
->when($request->age, function ($query, $age) {
// your query here
$query->where('age', $age);
})
->get();
Use "Between":
MySQL Raw Query WHERE Statement Example:
WHERE height BETWEEN :heightMin AND :heightMax
Looks like laravel, if it is take a look at:
https://laravel.com/docs/8.x/queries#additional-where-clauses
$something = Something::whereBetween('height', [1, 100])
->get();
However, if its not laravel, go in your IDE .. if you type just "where" if you find something like whereBetween in your autocompletion
If there is a problem with null values in the database you can do a (and)Where height IS NOT NULL .. You can also split your query like
$something = Something::where('height, '>=', $request->heightMin);
if($request->heightMax !== null){
something = something->where('height, '<=', $request->heightMax)
}
something->get();
if you want to remove age field and order you have to add where to your query like this:
$something = Something::where('height, '>=', $request->heightMin)
->where('height, '<=', $request->heightMax)
->where('age, '!=', null)
->get();
but if you want to have empty age field at the end of your query result and order just others that have value you should better do something like this:
$something = Something::where('height, '>=', $request->heightMin)
->where('height, '<=', $request->heightMax)
->orderBy(Illuminate\Support\Facades\DB::raw('ISNULL(`age`), `age`'), 'ASC')
->get();

Filter record by using array - Laravel

I have a collection and I want to filter that collection based upon "days_since_last_wallet_transaction" by using an array. For example, I have an array $day = array(2,4,6,8). Now I want to get the record where days_since_last_wallet_transaction is in 2,4,6,8. I think I cannot use "days_since_last_wallet_transaction" in where clause. Here is my query:
Customer::select(
'customers.id',
'customers.wallet_amount',
'customers.onesignal_id',
'customers.phone_number',
'customers.name',
DB::raw('DATEDIFF(NOW(), max(wallet_transactions.created_at)) as days_since_last_wallet_transaction')
)
->join('wallet_transactions', function ($join){
$join->on('customers.id', '=', 'wallet_transactions.customer_id')
->where('wallet_transactions.amount', '>', 0);
})
->groupBy('customers.id')
->where('customers.wallet_amount', '>', 0);
Any help would be highly appreciable.
Considering below your array:
$days = [2,4,6,8];
create a raw query as below (Note: you can write this line in query also )
$raw_query = '(DATEDIFF(NOW(), max(wallet_transactions.created_at))) IN ('.implode(',',$days).')';
Your query
Customer::select(
'customers.id',
'customers.wallet_amount',
'customers.onesignal_id',
'customers.phone_number',
'customers.name',
DB::raw('DATEDIFF(NOW(), max(wallet_transactions.created_at)) as days_since_last_wallet_transaction')
)
->join('wallet_transactions', function ($join){
$join->on('customers.id', '=', 'wallet_transactions.customer_id')
->where('wallet_transactions.amount', '>', 0);
})
->where('customers.wallet_amount', '>', 0)
->havingRaw($raw_query)
->groupBy('customers.id');

Laravel Query where All

I wondered how to make a Where All clause with Laravel
I'm trying to check if the episodes that a user saw are all the episodes of the series.
I'm using the WhereIn clause but i returns the results if i saw one episode of the serie.
$alleps get all the episodes of the serie
$seriessaw get all the episodes a user saw
Thank you for your answers !
$alleps = DB::table('episodes')
->select('episodes.id as ep_id')
->join('seasonsepisodes', 'episodes.id', '=', 'seasonsepisodes.episode_id')
->join('seriesseasons', 'seasonsepisodes.season_id', '=', 'seriesseasons.season_id')
->where('seriesseasons.series_id', '=', $id);
$seriesSaw = DB::table('usersepisodes')
->select('usersepisodes.episode_id as ep_id')
->where('usersepisodes.user_id', '=', Auth::user()->id)
->whereIn('usersepisodes.episode_id', $alleps)
->get();
I think you would need to set a having clause which would force records to only return if there's the same amount of records as there are amount of episodes.
$seriesSaw = DB::table('usersepisodes')
->select('usersepisodes.episode_id as ep_id')
->where('usersepisodes.user_id', '=', Auth::user()->id)
->whereIn('usersepisodes.episode_id', $alleps)
->having(\DB::raw('count(*)'), count($alleps))
->get();
You should note however that this would likely break if there's any possibility of having duplicates in the userepisodes table.
When you use WhereIn, you have to pass an array as the second parameter.
$alleps = DB::table('episodes')
->select('episodes.id as ep_id')
->join('seasonsepisodes', 'episodes.id', '=', 'seasonsepisodes.episode_id')
->join('seriesseasons', 'seasonsepisodes.season_id', '=', 'seriesseasons.season_id')
->where('seriesseasons.series_id', '=', $id)->get()->toArray();
$seriesSaw = DB::table('usersepisodes')
->select('usersepisodes.episode_id as ep_id')
->where('usersepisodes.user_id', '=', Auth::user()->id)
->whereIn('usersepisodes.episode_id', $alleps)
->get();

Laravel SQL select query where DateTimestamp contains this month and year

I'm trying to make a SQL query where I can check if the DateTimestamp contains this month and year.
A part of the query:
$leads = DB::table('leads')
->leftJoin('invoices', function($join2) {
$join2->on('leads.id', '=', 'invoices.lead_id');
})
->where('invoices.chance', '!=', 0)
->where('invoices.updated_at', '=', date('m-Y')) //Check if DateTimestamp(updated_at) contains Year and month
->select('leads.*')
I tried it with date_parse, new DateTime(), but it didn't work. What am I doing wrong?
You can use whereMonth() and whereYear() to achieve this:
$leads = DB::table('leads')
->leftJoin('invoices', function($join2) {
$join2->on('leads.id', '=', 'invoices.lead_id');
})
->where('invoices.chance', '!=', 0)
->whereMonth('invoices.updated_at', '=', date('m')) // changed
->whereYear('invoices.updated_at', '=', date('Y')) // changed
->select('leads.*');

Categories