I am using repository for querying day from date.
if i use only db raw then it works fine, but i don't like to use whole db::raw query in repository so i used db:raw where i needed.
when i used it, it shows error like table not found as table is found if that qyery is removed.
<?php
namespace App\Repositories\Api\Order;
use DB;
use Bosnadev\Repositories\Contracts\RepositoryInterface;
use Bosnadev\Repositories\Eloquent\Repository;
/**
* Class ArticleRepository
*
* #package app\Repository
*/
/**
* Created by PhpStorm.
* User: sabin
* Date: 5/10/16
* Time: 10:29 AM
*/
class OrderRepository extends Repository
{
/**
* #return string
*/
function model()
{
return 'App\Models\Order\Order';
}
function getAll($id)
{
return $this->makeModel()
->join('tbl_sales as sales', 'sales.id', '=', 'tbl_order.sales_id')
->select('sales.date', 'sales.delivery_date', 'sales.total_amount', DB::select(DB::raw("select day(tbl_sales.delivery_date) as day")))
->where('sales.client_id', '=', $id)
->groupBy('tbl_order.sales_id')
->get();
}
}
Error:
SQLSTATE[42S02]: Base table or view not found: 1109 Unknown table 'tbl_sales' in field list (SQL: select day(tbl_sales.delivery_date) as day)
s same problem.
note: i have used sales.delivery_date too, but it show
I have tried putting table command too like:
function getAll($id)
{
return $this->makeModel()
->join('tbl_sales as sales', 'sales.id', '=', 'tbl_order.sales_id')
->select('sales.date', 'sales.delivery_date', 'sales.total_amount', DB::select(DB::raw("select month(tbl_sales.delivery_date) as month from tbl_sales")))
->where('sales.client_id', '=', $id)
->groupBy('tbl_order.sales_id')
->get();
}
but also it is showing this problem.
ErrorException in Grammar.php line 58:
strtolower() expects parameter 1 to be string, array given
You create an alias to the tbl_sales as sales here:
->join('tbl_sales as sales', 'sales.id', '=', 'tbl_order.sales_id')
So I am guessing you should refer to it as sales elsewhere, can you try this to see if it helps:
return $this->makeModel()
->join('tbl_sales as sales', 'sales.id', '=', 'tbl_order.sales_id')
->select(
'sales.date',
'sales.delivery_date',
'sales.total_amount',
// sales here not tbl_sales
DB::select(DB::raw("select day(sales.delivery_date) as day"))
)
->where('sales.client_id', '=', $id)
->groupBy('tbl_order.sales_id')
->get();
If it doesn't work then my approach would be to get the raw sql and get the query working via sql firstly.
To get the raw sql eloquent is generating you could try the following on your query then dd()ing it:
$q = $this->makeModel()
->join('tbl_sales as sales', 'sales.id', '=', 'tbl_order.sales_id')
->select(
'sales.date',
'sales.delivery_date',
'sales.total_amount',
// sales here not tbl_sales
DB::select(DB::raw("select day(sales.delivery_date) as day"))
)
->where('sales.client_id', '=', $id)
->groupBy('tbl_order.sales_id')
->toSql();
dd($q);
or:
DB::enableQueryLog();
dd(DB::getQueryLog());
or install https://github.com/barryvdh/laravel-debugbar
Once you've got the raw sql of the query you can play around with it in your favourite editor and run it directly on the database until you get the desired results then make the required modifications in elqouent to reflect the correct code.
Hope that helps!
Related
I'm using laravel 8, and I make a query builder to make a left join, when laravel execute the query show me a error, but in SQL Server MS execute correctly
SQLSTATE[42S22]: [Microsoft][ODBC Driver 17 for SQL Server][SQL
Server]Invalid column name '2'. (SQL: select [a].[id] as [id1],
[b].[id] as [id2], [a].[reg], [a].[parte], [a].[ubiclinea],
[a].[descripcion], [a].[numConteo] as [conteo1], [a].[cantidad] as
[cantidad1], [a].[counter] as [contador1], [a].[created_at] as
[created1], [b].[numConteo] as [conteo2], [b].[cantidad] as
[cantidad2], [b].[counter] as [contador2], [b].[created_at] as
[created2] from [directosCount] as [a] left join [directosCount] as
[b] on [a].[reg] = [b].[reg] and [b].[numConteo] = [2] and
[b].[deleted_at] is null where [a].[numConteo] = 1 and
[a].[deleted_at] is null)
that is the error that show
but I copy the query and execute in SQL Server and it works
my query in laravel is this
DB::table('directosCount as a')
->leftJoin('directosCount as b', function($join)
{
$join->on('a.reg', '=', 'b.reg');
$join->on('b.numConteo', '=', '2')->whereNull('b.deleted_at');
})
->select('a.id as id1', 'b.id as id2', 'a.reg', 'a.parte', 'a.ubiclinea', 'a.descripcion', 'a.numConteo as conteo1', 'a.cantidad as cantidad1', 'a.counter as contador1', 'a.created_at as created1', 'b.numConteo as conteo2', 'b.cantidad as cantidad2', 'b.counter as contador2', 'b.created_at as created2')
->where('a.numConteo', '1')
->whereNull('a.deleted_at')
->get();
hope someone can help me
i think this should be outside of join
$join->on('b.numConteo', '=', '2')->whereNull('b.deleted_at');
please change on to where
->where('b.numConteo', '=', '2')->whereNull('b.deleted_at');
You have 2 on statements in your join. Try this
DB::table('directosCount as a')
->leftJoin('directosCount as b', function($join)
{
$join->on('a.reg', '=', 'b.reg')
->where('b.numConteo', '=', '2')
->whereNull('b.deleted_at');
})
->select('a.id as id1', 'b.id as id2', 'a.reg', 'a.parte', 'a.ubiclinea', 'a.descripcion', 'a.numConteo as conteo1', 'a.cantidad as cantidad1', 'a.counter as contador1', 'a.created_at as created1', 'b.numConteo as conteo2', 'b.cantidad as cantidad2', 'b.counter as contador2', 'b.created_at as created2')
->where('a.numConteo', '1')
->whereNull('a.deleted_at')
->get();
I don't know what I am doing wrong but I assume it's something right under my nose.
I am trying to retrieve multiple rows from a database by their ID, by subtracting a number from the original query like so:
$week4 = DB::table('weekly_territory_data')->where([['area_id', '=', $area_id], ['start_date', '=', $startDate]])->first();
Log::info(print_r($week4, true));
$selectedWeekId = $week4->id;
$selectedWeekIdSub2 = $selectedWeekId - 236;
$selectedWeekIdSub3 = $selectedWeekId - 472;
$selectedWeekIdSub4 = $selectedWeekId - 708;
Log::info($selectedWeekIdSub4);
$week1 = DB::table('weekly_territory_data')->where('id', '=', $selectedWeekIdSub4)->first();
Log::info(print_r($week1,true));
I know I am getting the right ID numbers, but I can't retrieve the rows using the code I have. What is going on here? Is there some sort of exact case issue?
I strongly recommend using Eloquent,
Assuming you created a model WeeklyTerritoryData
your query should look like this :
$week1 = WeeklyTerritoryData::find($selectedWeekIdSub4)->first();
or
$week1 = WeeklyTerritoryData::where('id',$selectedWeekIdSub4)->first();
this will return a Collection, then you can access id or any other field like this :
$week1->id
The issue was typecasting.
I was trying to get the data set with an ID, treating it as an integer... but my ID column is a string for some stupid reason.
$selectedWeekId = $week4->id;
$week3id = $selectedWeekId - 236;
$week3string = (string) $week3id;
$week3 = WeeklyTerritoryData::where('id', '=', $week3string)->get();
Log::info('week3 to follow');
Log::info($week3);
$week2id = $selectedWeekId - 472;
$week2string = (string) $week2id;
$week2 = WeeklyTerritoryData::where('id', '=', $week2string)->get();
Log::info('week2 to follow');
Log::info($week2);
$week1id = $selectedWeekId - 708;
$week1string = (string) $week1id;
$week1 = WeeklyTerritoryData::where('id', '=', $week1string)->get();
when i apply join server show me error
(2/2) QueryException
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
my controller
$player_bet = DB::table('player_bet')
->join('poll', 'player_bet.poll_id = poll.id')
->join('poll_type', 'poll_type.id = poll.poll_type_id')
->join('tournament_week', 'poll.week_id = tournament_week.id')
->join('tournament', 'poll.tournament_id = tournament.id')
->join('users', 'player_bet.users_id = users.id')
->join('poll_winner', 'poll_winner.poll_id = poll.id AND poll_winner.user_id = users.id', 'left')
->where('users.id ='.$user_id)
->select('poll_winner.id AS winner_id, poll_winner.winning_amount AS won_amount,, poll_winner.tax_amount AS won_tax_amount, player_bet.id as bet_id, player_bet.create_time,poll.id poll_id,poll.winning_amount,poll.amount poll_amount,poll.week_id week_id,poll.description,poll.description_detail,poll_type.name poll_type,poll_type.id poll_type_id,tournament_week.week_no week_no, poll.is_close is_close, tournament.id tournament_id, tournament.name tournament_name,tournament.start_date,tournament.end_date,tournament.logo, users.username username')
->where('users_id',$user_id)
->get();
Your join syntax is incorrect: https://laravel.com/docs/7.x/queries#joins
Also Your Where condition is Not proper
Problem 1:
Change the multiple condition join method
->join('poll_winner', 'poll_winner.poll_id = poll.id AND poll_winner.user_id = users.id', 'left')
to
->leftjoin('poll_winner', function($join) {
$join->on('poll_winner.poll_id', '=', 'poll.id')
->whereColumn('poll_winner.user_id', 'users.id');
})
Problem 2:
you have two where condition, the where need to be like this, the one is the column, the other be the column's value:
->where('users.id', $user_id)
Problem 3:
If you want to use one string as parameter of select method, you need to use selecRaw
->selectRaw('poll_winner.id AS winner_id, poll_winner.winning_amount AS won_amount,, poll_winner.tax_amount AS won_tax_amount, player_bet.id as bet_id, ...')
Or you need to use multiple strings as parameters of select method:
->select('poll_winner.id AS winner_id', 'poll_winner.winning_amount AS won_amount', ...)
I have a question about query builder.
I wanna made a query that will have conection with two databases
I will show you the SQL query :
SELECT
naviones_db16.ps_orders.payment,
naviones_db16.ps_orders.total_paid AS total,
naviones_db16.ps_orders.current_state AS current_state,
naviones_db16.ps_orders.id_order AS id_order,
naviones_db16.ps_shop.NAME AS name_shop,
naviones_db16.ps_customer.firstname AS customer_firstname,
naviones_db16.ps_customer.lastname AS customer_lastname,
naviones_db16.ps_orders.reference,
naviones_db16.ps_orders.date_add AS fecha
FROM
ps_orders
INNER JOIN naviones_db16.ps_customer ON
naviones_db16.ps_customer.id_customer=
naviones_db16.ps_orders.id_customer
INNER JOIN naviones_db16.ps_shop ON naviones_db16.ps_shop.id_shop =
naviones_db16.ps_orders.id_shop
WHERE
naviones_db16.ps_orders.current_state = 16
AND
naviones_db16.ps_orders.id_order NOT IN (
SELECT
multimarket.productos.id_order
FROM
multimarket.productos
WHERE
multimarket.productos.id_order = naviones_db16.ps_orders.id_order
)
ORDER BY
naviones_db16.ps_orders.id_order DESC
This is that i have done in query builder
$naviones = DB::connection('naviones')->table('ps_orders')
->select('ps_orders.payment', 'ps_orders.total_paid AS total', 'ps_orders.current_state AS current_state', 'ps_orders.id_order AS id_order',
'ps_shop.NAME AS name_shop', 'ps_customer.firstname AS customer_firstname', 'ps_customer.lastname AS customer_lastname', 'ps_orders.reference',
'ps_orders.date_add AS fecha')
->join('ps_customer', 'ps_customer.id_customer', '=', 'ps_orders.id_customer')
->join('ps_shop', 'ps_shop.id_shop', '=', 'ps_orders.id_shop')
->where('ps_orders.current_state', '=', '16')
->orderBy('id_order', 'DESC')
->get()->toArray();
Of course, i have other conection this is: DB::connections('multimarket') but this point i dont have any idea how i could do that. Do you have any idea?
Thank u
I would like to return back each thread only 1 time, however I keep getting errors returned back saying I have to have additional items in the groupBy clause. This makes it so that some threads display more than once (once for the first time it is posted, then again if I have voted on it). How can I limit it to 1 time with it preferring the time where threads_users.user_id is filled.
$threads = Thread::leftjoin('users', 'users.id', '=', 'threads.user_id')
->leftjoin('users as poster','poster.id', '=', 'threads.user_id')
->leftjoin('lewps_threads', 'lewps_threads.thread_id','=','threads.id')
->leftjoin('threads_users', 'threads_users.thread_id','=','threads.id')
->where('threads.id', $id)
->wherein('threads_users.user_id', array(null, $users->id))
->groupBy(array('threads.id', 'users.username','threads_users.boosts'))
->get(array('threads.*','users.username','threads_users.boosts'));
To get out what is going on and what query is executed under the hood you can put this after executing query:
dd(DB::getQueryLog());
To return only 1 raw you can add this to your code:
->take(1)->get();
So this would be something like that:
$threads = Thread::leftjoin('users', 'users.id', '=', 'threads.user_id')
->leftjoin('users as poster','poster.id', '=', 'threads.user_id')
->leftjoin('lewps_threads', 'lewps_threads.thread_id','=','threads.id')
->leftjoin('threads_users', 'threads_users.thread_id','=','threads.id')
->where('threads.id', $id)
->wherein('threads_users.user_id', array(null, $users->id))
->groupBy(array('threads.id', 'users.username','threads_users.boosts'))
->take(1)
->get(array('threads.id','users.username','threads_users.boosts'));
NOTE that I have changed 'threads.*' to 'threads.id' in your get part ... tou have to sync your select list with your group by calusule.