Call to undefined method Illuminate\Database\Query\Builder::exchanges() - laravel

ERROR: BadMethodCallException in Builder.php line 2435:
Call to undefined method Illuminate\Database\Query\Builder::exchanges()
There are two tables, named as EXCHANGES table and FINALTRADES table.
Exchanges table has START_TIME field (with format : 00:00:00) and finaltrades table has exchange_id.
I want to count entries only which come between start_time to start_time+1hrs from final trades table. FOR EXAMPLE: If start_time has 09:15:00, then count between 09:15:00 to 10:15:00 records only.
Function:
public function getCountHour() {
$user_id = Auth::user()->id;
$countTrades = FinalTrade::where('user_id', '=', $user_id)
->exchanges()
->where('start_time', '>=', $your_desired_time)
->where('start_time' , '<=' , strtotime($your_desired_time) + 60*60)
->count();
return response()->json($countTrades);
}
Relationship given to FnalTrade model:
public function exchanges(){
return $this->hasOne('App\EXCHANGE_MODEL_NAME', 'id', 'exchange_id');
}
both table SCREEN

The following should do the trick:
$countTrades = FinalTrade::whereUserId(Auth::id())
->whereHas('exchanges', function($query) use ($your_desired_time) {
$query
->where('start_time', '>=', $your_desired_time)
->where('start_time' , '<=' , strtotime($your_desired_time) + 60*60);
})
->count();

Related

How to add custom if..else statements to the selected columns at a query for exporting data from database into excel file

I'm using Laravel 5.8 and laravel-excel for exporting some data into an Excel file.
So I added this method to the Model:
public static function getAccounts($id)
{
$records = DB::table('members')
->where('mys_creator_id',$id)
->where('mys_olp_id',4)
->join('my_students', 'members.mbr_usr_id', '=', 'my_students.mys_mbr_id')
->join('students', 'members.mbr_usr_id', '=', 'students.std_mbr_id')
->select('members.mbr_name', 'members.mbr_family', 'members.mbr_father_name','members.mbr_national_code','members.mbr_birthday','members.mbr_phone','members.mbr_mobile','members.mbr_post_code','members.mbr_prv_id','members.mbr_cit_id','members.mbr_gender_id','members.mbr_address','students.std_degree_id','students.std_grade_id','students.std_filed_id','students.std_major_id','students.std_school','students.std_education_type_id','my_students.mys_paid_price')
->get()->toArray();
return $records;
}
And this works fine but some of data holds and id instead of text value:
And all these ids are stored in baseinfos table. For example std_degree_id of 8 goes like this:
And now instead of returning number 8, I need to get bas_value and return it in the exported Excel file.
So it would looked like this:
if(students.std_degree_id == 9){
// print Elementry
}elseif(students.std_degree_id == 10){
// print Academy
}else{
...
}
So how can I do that?
UPDATE #1:
$records = DB::table('members')
->where('mys_creator_id',$id)
->where('mys_olp_id',4)
->join('my_students', 'members.mbr_usr_id', '=', 'my_students.mys_mbr_id')
->join('students', 'members.mbr_usr_id', '=', 'students.std_mbr_id')
->join('baseinfos', 'students.std_degree_id', '=', 'baseinfos.bas_id')
->join('baseinfos', 'members.mbr_gender_id', '=', 'baseinfos.bas_id')
->select(...,'baseinfos.bas_value','members.mbr_address','baseinfos.bas_value',...)->get()->toArray();
return $records;
If I add two more joins with baseinfos table, I get this error:
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique
And this mainly because I get baseinfos.bas_value twice in the select.
You'd need to join one more table and show the corresponding field from that table:
public static function getAccounts($id)
{
$records = DB::table('members')
->where('mys_creator_id',$id)
->where('mys_olp_id',4)
->join('my_students', 'members.mbr_usr_id', '=', 'my_students.mys_mbr_id')
->join('students', 'members.mbr_usr_id', '=', 'students.std_mbr_id')
->join('baseinfos', 'students.std_degree_id', '=', 'baseinfos.bas_id')
->select('members.mbr_name', 'members.mbr_family', 'members.mbr_father_name','members.mbr_national_code','members.mbr_birthday','members.mbr_phone','members.mbr_mobile','members.mbr_post_code','members.mbr_prv_id','members.mbr_cit_id','members.mbr_gender_id','members.mbr_address','baseinfo.bas_value','students.std_grade_id','students.std_filed_id','students.std_major_id','students.std_school','students.std_education_type_id','my_students.mys_paid_price')
->get()->toArray();
return $records;
}
I am not sure if you are using getAccounts anywhere else, If you are create a new method to format this data.
public static function getAccounts($id)
{
$degrees = [
9 => 'Elementry',
10 => 'Academy',
];
$records = DB::table('members')
->where('mys_creator_id',$id)
->where('mys_olp_id',4)
->join('my_students', 'members.mbr_usr_id', '=', 'my_students.mys_mbr_id')
->join('students', 'members.mbr_usr_id', '=', 'students.std_mbr_id')
->select('members.mbr_name', 'members.mbr_family', 'members.mbr_father_name','members.mbr_national_code','members.mbr_birthday','members.mbr_phone','members.mbr_mobile','members.mbr_post_code','members.mbr_prv_id','members.mbr_cit_id','members.mbr_gender_id','members.mbr_address','students.std_degree_id','students.std_grade_id','students.std_filed_id','students.std_major_id','students.std_school','students.std_education_type_id','my_students.mys_paid_price')
->get();
// if map is not working add it in collect($records)->map.
$records->map(function($record) use($degrees) {
$record->std_degree_id = $degrees[$record->std_degree_id];
return $record;
});
return $records->toArray();
}
But I am guessing $record->std_degree_id is a relation, so just use the relation title instead of the array map i created.

I want to see the post created in the same month of Carbon::now

public function showallcategoryfood(){
$carbon = Carbon::now();
$foods=Food::all();
DB::table('food')->whereMonth('created_at','=',$carbon);
return view('test.logout')->with('foods',$foods);
}
It keeps showing all the records, its not taking it by the month of June!
Have you read the docs?
The whereMonth method may be used to compare a column's value against a specific month of a year:
$users = DB::table('users')
->whereMonth('created_at', '12')
->get();
So in your case it will be:
$users = DB::table('users')
->whereMonth('created_at', Carbon::now()->month)
->get();
The all() method returns the underlying array represented by the collection.
Try this :
public function showallcategoryfood(){
$carbon = Carbon::now()->format('m');
$foods=Food::whereMonth('created_at', '=', $carbon)->get();
return view('test.logout')->with('foods', $foods);
}

Cant get laravel query with relationship

I have model class Post with relationship
function postanchors(){
return $this->hasMany('App\PostAnchors', 'post_id', 'id');
}
posts_anchors is simple table
id | post_id | anchor_id
Now I want to get posts if they has anchor_id = 51
$query = Post::query();
$posts = $query->with('postanchors', function ($query){
$query->where('anchor_id','=', 51);
})
error mb_strpos() expects parameter 1 to be string, object given
$posts = $query->with('postanchors')->where('anchor_id', 51)->paginate(8);
does not working too
Undefined column: 7 ERROR: column "anchor_id" does not exist LINE 1: select count() as aggregate from "posts" where "anchor_id"*
It mast be simple request. May be relationship wrong?
try below query:
$query = Post::query();
$posts = $query->whereHas('postanchors', function ($query){
$query->where('anchor_id','=', 51);
})->get();

How to implement relationship in where condition Laravel's eloquent?

I have two tables:
Students
Results
The two tables have one to may relationship.
Student model:
public function results()
{
return $this->hasMany('App\Result');
}
Result model:
public function student()
{
return $this->belongsTo('App\Student');
}
In the students table I have a field called average_score.
How can I execute the following query, this is not working it says "Undefined property: Illuminate\Database\Query\Builder::$student":
$data = Result::with('student')->where('score', '>=', function($q){
$average_score = $q->student->average_score;
return $average_score;
})->get();
In order to get the results that are only higher or equal to than the "average_score".
If score and average_score are columns of the same table (student), try this;
$data = Result::with(['student' => function ($query) {
$query->whereColumn('score', '>=', 'average_score');
}])->get();
If score and average_score are columns of the different tables, try this;
$data = Result::with('student')->whereHas('student', function($q) {
$q->whereColumn('students.average_score', '<=', 'results.score');
})->get();
You can use whereHas to find those results:
$data = Result::with('student')->whereHas('student', function($q) {
$q->whereColumn('average_score', '<=', 'Results.score');
})->get();
get value from column Results.average_score then compare to student.score:
Result::with([
'student' => function($query) {
$query->where("score",">=", "Results.average_score"););
}
])

Eloquent / Laravel - Putting a WHERE Clause on a Reference Table With Chained Relationships

I have the following relationship functions in my Job model:
public function resourceTypes(){
return $this->belongsToMany('ResourceType', 'job_requests');
}
public function resources(){
return $this->belongsToMany('Resource', 'jobs_resources')->withPivot('flow_type', 'resource_type_id');
}
I am able to get an object with data from both of the above relationships using:
$job = Job::findorfail($projectId);
$result = $job->with('resources.resourceTypes')->get();
I would like to put a where clause on the jobs_resources pivot table - specifically on the column flow_type.
How would I do this?
Try something like this:
$job = Job::with('resources' => function($q) {
$q->with('resourceTypes')->where('flow_type',2);
})->findorfail($projectId);
In above you will get only those resources with flow_type = 2
I ended up using the following statement:
Job::with(['resources' => function ($query){
$query->wherePivot('flow_type', '=', '1' );
}, 'resources.resourceTypes'])->where('id', $projectId)->firstOrFail();
$result = DB::table('job')
->join('job_resources', 'job.id', '=', 'job_resources.job_id')
->join('job_requests', 'job_resources.request_id', '=', 'job_requests.id')
->where('job_resources.flow_type', '=', CONDITION)
->get();
Your table data is not clear from your input, but this method (query builder) should work

Resources