how to concat join by qb in doctrine? - doctrine

I want to add a join to my query in if condition but it returned an error.
how we can do it in doctrine?
is there any thing like:
$qb->andWhere('p.$gender = :g')->setParameter('g', $gender);
for join in doctrine symfony??
$qb->select("p")->from("PfmSanadBundle:Person", "p");
if ((isset($province) && trim($province) !== '') && (isset($City) && trim($City) !== '')){
$qb->join("p" ,"students", "ps")
->join("ps" ,"organization", "po")
->join("po" ,"cityProvince", "pc")
->join("pc" ,"province", "pp");
}
if ((isset($province) && trim($province) !== '') && !isset($City)) {
$qb->join("p.students", "ps")
->join("ps.organization", "po");
}
$res = $qb->getQuery()->getArrayResult();
error (postman):
"error": {
"code": 500,
"message": "Internal Server Error",
"exception": [
{
"message": "[Semantical Error] line 0, col 49 near 'p students INNER': Error: Class 'p' is not defined.",
"class": "Doctrine\ORM\Query\QueryException",
"trace": [
{

Join method usage is following
// Example - $qb->join('u.Group', 'g', Expr\Join::WITH, $qb->expr()->eq('u.status_id', '?1'))
// Example - $qb->join('u.Group', 'g', 'WITH', 'u.status = ?1')
// Example - $qb->join('u.Group', 'g', 'WITH', 'u.status = ?1', 'g.id')
public function join($join, $alias, $conditionType = null, $condition = null, $indexBy = null);
Thus following lines
$qb->join("p" ,"students", "ps")
->join("ps" ,"organization", "po")
->join("po" ,"cityProvince", "pc")
->join("pc" ,"province", "pp");
change to
$qb->join("p.students", "ps")
->join("ps.organization", "po")
->join("po.cityProvince", "pc")
->join("pc.province", "pp");

Related

Invalid Datetime format: 1366 Incorrect integer value LARAVEL

I'm trying to update or insert multiple data when a post approve. Insert condition is working but update function giving error.
(SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer
value: '[10000,10000]' for column
parijattech_web_app.palika_to_school_kriyakalap.cash at row 3 update
palika_to_school_kriyakalap SET cash = [ 10000, 10000 ],
palika_to_school_kriyakalap.updated_at = 2022 -12 -26 04: 58: 52 WHERE
kriya_name IN (SALARY, POSHAK) AND school_id IN (24, 24))
My code is so messy. So, how can i improve my coding skill?
Here is my Controller
public function approve(Request $request, $biniyojan_id)
{
$upudate = DB::table('biniyojan')
->where('biniyojan_id', $biniyojan_id)
->update(['biniyojan_status' => 1]);
if ($upudate) {
$budget_data = BiniyojanDetails::where('biniyojan_id', $biniyojan_id)->where('biniyojan_id', $biniyojan_id)->get();
$kriya = Palika_ToSchool_Kriyakalap::whereIn('kriya_name', $budget_data->pluck('kriyakalap'))
->whereIn('school_id', $budget_data->pluck('school_id'))
->get();
$budget_cash = BiniyojanDetails::where('biniyojan_id', $biniyojan_id)
->whereIn('school_id', $kriya->pluck('school_id'))
->whereIn('kriyakalap', $kriya->pluck('kriya_name'))
->get();
$updated_bini = $budget_cash->pluck('cash')->toArray();
$updated_kriya_cash = $kriya->pluck('cash')->toArray();
$newArray = array_map(function () {
return array_sum(func_get_args());
}, $updated_bini, $updated_kriya_cash);
if ($kriya->pluck('school_id') == $budget_data->pluck('school_id')) {
Palika_ToSchool_Kriyakalap::whereIn('kriya_name', $budget_data->pluck('kriyakalap'))
->whereIn('school_id', $budget_data->pluck('school_id'))
->update(['cash' => $newArray]);
} else {
foreach ($budget_data as $bud) {
$data = [
'school_id' => $bud->school_id,
'kriya_name' => $bud->kriyakalap,
'cash' => $bud->cash,
];
Palika_ToSchool_Kriyakalap::where('kriya_name' != $bud->kriyakalap)
->insert($data);
}
}
return redirect()->back()->with('status', 'Approved!!!');
}
else{
return redirect()->back()->with('error', 'Error!!!');
}
}

laravel using whereBetween for count multiple table

function fetch_c(Request $request)
{
if($request->ajax())
{
if($request->from_date != '' && $request->to_date != '')
{
$data = DB::select("SELECT (SELECT COUNT(*) FROM pos) as a, (SELECT COUNT(*) FROM customers) as b, (SELECT COUNT(*) FROM
supplires) as c")
->whereBetween('created_at', array($request->from_date, $request->to_date));
}
else
{
$data = DB::select("SELECT (SELECT COUNT() FROM pos) as a, (SELECT COUNT() FROM customers) as b, (SELECT COUNT(*) FROM
supplires) as c");
}
echo json_encode($data);
}
}
function fetch_c(Request $request)
{
if($request->ajax())
{
if($request->from_date != '' && $request->to_date != '')
{
$data = DB::select("SELECT (SELECT COUNT(*) FROM pos) as a, (SELECT COUNT(*) FROM customers) as b, (SELECT COUNT(*) FROM supplires) as c")
->whereBetween('created_at', array($request->from_date, $request->to_date));
}
else
{
$data = DB::select("SELECT (SELECT COUNT(*) FROM pos) as a, (SELECT COUNT(*) FROM customers) as b, (SELECT COUNT(*) FROM supplires) as c");
}
echo json_encode($data);
}
}
ajax
<script>
$(document).ready(function(){
var date = new Date();
$('.input-daterange').datepicker({
todayBtn: 'linked',
format: 'yyyy-mm-dd',
autoclose: true
});
var _token = $('input[name="_token"]').val();
fetch_data();
function fetch_data(from_date = '', to_date = '')
{
$.ajax({
url:"{{ url('fetch_c') }}",
method:"get",
data:{from_date:from_date, to_date:to_date},
dataType:"json",
success:function(data)
{
console.log(data);
// $("#max_score").val(data);
}
})
$('#filter').click(function(){
var from_date = $('#from_date').val();
var to_date = $('#to_date').val();
if(from_date != '' && to_date != '')
{
fetch_data(from_date, to_date);
}
else
{
alert('Both Date is required');
}
});
});
</script>
Route
Route::get('/fetch_c', [App\Http\Controllers\HomeController::class, 'fetch_c']);
Error
GET http://localhost:8000/fetch_c?from_date=2022-03-01&to_date=2022-03-31 500 (Internal Server Error)

Laravel Query Builder Return Results

I have below Laravel Query, which runs very fine and returns the desired results.
$email = (Auth::user()->email);
$UerType = DB::table('tbl_schools')->where('email', $email)->pluck('type');
$parent_ids = DB::table('tbl_schools')->where('email', $email)->pluck('id');
// return $parent_ids;
if ($UerType == 'sub_admin') {
$children = DB::table('tbl_schools')->whereIn('id', $parent_ids)->get();
return $children;
} elseif ($UerType == 'school') {
$children = DB::table('tbl_schools')->where('email', $email)->get();
return $children;
} elseif ($UerType = 'admin') {
$children = DB::table('tbl_schools')->where('email', $email)->get();
return $children;
}
Out from Above is as expected
{
"id": 15,
"type": "sub_admin",
"parent_id": "12",
"username": "",
"access_role_type": "SCH2486518",
"school_code": "SCH2486518",
"school_name": "Test School",
"school_ad_name": "Support",
"address": "",
"email": "someemail#mail.com",
"mobile": "+2332123123123",
"school_logo": "",
"profile_image": "",
"status": "active",
"created": "2020-09-28 16:41:44",
"updated": "0000-00-00 00:00:00",
"token": ""
}
Now I need to Pick specific fields and save them to session for future use, but when I run the below code, strangely I still get the entire Query Result which is strange.
$email = (Auth::user()->email);
$UerType = DB::table('tbl_schools')->where('email', $email)->pluck('type');
$parent_ids = DB::table('tbl_schools')->where('email', $email)->pluck('id');
// return $parent_ids;
if ($UerType == 'sub_admin') {
$children = DB::table('tbl_schools')
->select('id','type','parent_id','school_name')
->whereIn('id', $parent_ids)
->get();
return $children;
} elseif ($UerType == 'school') {
$children = DB::table('tbl_schools')
->Select('id','type','parent_id','school_name')
->where('email', $email)
->get();
return $children;
} else {
$children = DB::table('tbl_schools')
->Select('id','type','parent_id','school_name')
->where('email', $email)
->get();
return $children;
}
I need help
First of all, you don't need to call the same query two times. Next, you are returning an array and compare it with a string. Do the following way. Hope this will give you the exact result.
$email = (Auth::user()->email);
$user = DB::table('tbl_schools')->where('email', $email)->first();
if ($uerType->type == 'sub_admin') {
$children = DB::table('tbl_schools')->whereIn('id', $user->id)->get();
return $children;
} elseif ($user->type == 'school') {
$children = DB::table('tbl_schools')->where('email', $email)->get();
return $children;
} elseif ($user->type = 'admin') {
$children = DB::table('tbl_schools')->where('email', $email)->get();
return $children;
}

Trying to get property 'point' of non-object

I have a problem with get value of object ,I tried many posts here in stackoverflow like this : Trying to get property of non-object - Laravel 5 but it isn't solved.
I have this controller method :
public function user_team($id)
{
$week = Week::where('status','active')->first();
$week_id = $week->id;
$team = Team::where('user_id',$id)->with('userteams')->get();
foreach ($team[0]->userteams as $userteam) {
$userteam->surname = $userteam->Club->surname;
$userteam->price = $userteam->Club->price;
$points = Userpoint::where('club_id',$userteam->club_id)->where('week_id',$week_id)->where('user_id',$id)->first();
$point = $points->point;
$userteam->point = $point;
unset($userteam->Club);
}
return $team;
}
teams return this in post man :
[
{
"id": 40,
"team_name": "",
"season_id": 1,
"week_id": 2,
"user_id": 223269719,
"created_at": "2021-07-30T13:34:44.000000Z",
"updated_at": "2021-07-30T13:34:44.000000Z",
"userteams": [
{
"id": 277,
"team_id": 40,
"club_id": 1,
"created_at": "2021-07-30T13:34:44.000000Z",
"updated_at": "2021-07-30T13:34:44.000000Z"
},
{
"id": 278,
"team_id": 40,
"club_id": 21,
"created_at": "2021-07-30T13:34:44.000000Z",
"updated_at": "2021-08-01T10:37:03.000000Z"
},
and $points->point is return a value : 9
but when I try
$point = $points->point
it give me error : Trying to get property 'point' of non-object
foreach ($team[0]->userteams as $key => $userteam) {
$userteam->surname = $userteam->Club->surname;
$userteam->price = $userteam->Club->price;
$points = Userpoint::where('club_id',$userteam->club_id)->where('week_id',$week_id)->where('user_id',$id)->first();
$team[0]->userteams[$key]['point'] = $points->point;
unset($userteam->Club);
}
There i updated it, can you try again with this code
I solved it :
public function user_team($id)
{
$week = Week::where('status','active')->first();
$week_id = $week->id;
$team = Team::where('user_id',$id)->with('userteams')->first();
foreach ($team->userteams as $userteam) {
$userteam->surname = $userteam->Club->surname;
$userteam->price = $userteam->Club->price;
$points = $userteam->Club->userpoints;
if(isset($oiints)){
foreach ($points as $point) {
if($point->user_id == $id){
$userteam->point = $point->point;
}
}
}else{
$userteam->point = 0;
}
unset($userteam->Club->userpoints);
unset($userteam->Club);
}
return $team;
}

Loop through a string array in laravel

I'm trying to loop through a a string and get array of records where the service status is equals to complete or cancelled. I'm having an issue of get all the records instead it response with the last record from the list.
My Controller:
$id = $request->user_id;
$history = bookings::select('*')->whereIn('service_status', ['complete', 'cancelled'])->where(['specialist_id'=>$id])->orderBy('time', 'DESC')->get();
if($history->isempty()){
return response()->json(['statusCode'=>'5', 'statusMessage' => 'Empty Record', 'data' =>[]]);
}else{
for($i = 0; $i < count($history); $i++){
$b_id = $history[$i]->id;
$service = $history[$i]->service;
$time = $history[$i]->time;
$date = $history[$i]->date;
$location = $history[$i]->location;
$payment = $history[$i]->payment_type;
$price = $history[$i]->amount;
$special_request = $history[$i]->special_request;
$s_id = $history[$i]->costumer_id;
}
$user = User::with('profile')->find($s_id);
$data = array(['id'=>$b_id, 'date'=>$date, 'name'=>$user->name." ".$user->profile->lastname, 'speccial_request'=>$special_request, 'item'=>$service, 'time'=>$time." - 1 hour", 'location'=>$location, 'total'=>"$price"]);
return response()->json(['statusCode'=>'0', 'statusMessage' => 'Successful','data' => $data], 200);
}
My response is as follows:
{
"statusCode": "0",
"statusMessage": "Successful",
"data": [
{
"id": 5,
"date": "2020-07-08",
"name": "User1",
"speccial_request": null,
"item": "Service",
"time": "10:00 - 1 hour",
"location": "street, city",
"total": "300"
}
]
}
I want to get all the items from the DB not just one.
You need to create an empty array ($data) and push to that array in each iteration.
Try below code:
$id = $request->user_id;
$history = bookings::select('*')->whereIn('service_status', ['complete', 'cancelled'])->where(['specialist_id'=>$id])->orderBy('time', 'DESC')->get();
if($history->isempty()){
return response()->json(['statusCode'=>'5', 'statusMessage' => 'Empty Record', 'data' =>[]]);
}else{
$data=[];
for($i = 0; $i < count($history); $i++){
$b_id = $history[$i]->id;
$service = $history[$i]->service;
$time = $history[$i]->time;
$date = $history[$i]->date;
$location = $history[$i]->location;
$payment = $history[$i]->payment_type;
$price = $history[$i]->amount;
$special_request = $history[$i]->special_request;
$s_id = $history[$i]->costumer_id;
$user = User::with('profile')->find($s_id);
array_push($data,['id'=>$b_id, 'date'=>$date, 'name'=>$user->name." ".$user->profile->lastname, 'speccial_request'=>$special_request, 'item'=>$service, 'time'=>$time." - 1 hour", 'location'=>$location, 'total'=>"$price"]);
}
return response()->json(['statusCode'=>'0', 'statusMessage' => 'Successful','data' => $data], 200);
}

Resources