How can get the value from the third table in laravel and order by with id in descending order

Shruti

In controller i have a code like this :

public function index(Request $request)
    {
        $course=Student::with('StudentDetail')->get();
        $courses=$course->toArray();
        echo "<pre>";print_r($courses);exit;
        return view('student.student_listing',['result'=>$courses]);

    }

through this i am getting result like :

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Neha
            [email] => [email protected]
            [roll_no] => 101
            [created_at] => 2018-05-22 09:20:18
            [updated_at] => 2018-05-22 00:00:00
            [student_detail] => Array
                (
                    [id] => 1
                    [student_id] => 1
                    [phone] => 11691110876
                    [course] => Java
                    [city] => Goa
                    [state] => 1
                    [created_at] => 2018-05-22 16:48:36
                    [updated_at] => 2018-05-22 09:20:43
                )

        )

    [1] => Array
        (
            [id] => 2
            [name] => Ankit
            [email] => [email protected]
            [roll_no] => 102
            [created_at] => 2018-05-22 09:20:18
            [updated_at] => 2018-05-22 00:00:00
            [student_detail] => Array
                (
                    [id] => 2
                    [student_id] => 2
                    [phone] => 12313311
                    [course] => Phps
                    [city] => Delhi
                    [state] => 2
                    [created_at] => 2018-05-23 13:28:19
                    [updated_at] => 2018-05-15 03:15:13
                )

        )

)

Here i am getting a field state where i am getting the id this id related to the states table id how can i get the value from states table bassed on this id and i want this result in Descending order with name or id how can i do that can anyone please help me related this .

models i have like :

StudentDetail Model:
<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use App\Http\Model\Student;
use App\Http\Model\State;
class StudentDetail extends Model
{ 
    public function Student()
    {
         return $this->belongsTo(Student::class);

    }
  public function state()
    {
         return $this->hasOne(State::class);

    }

}



Student model:
<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use App\Http\Model\StudentDetail;

class Student extends Model
{ 
     public function StudentDetail()
    {
        return $this->hasOne(StudentDetail::class);
    }

}
Adnan Mumtaz

You can use nested with()

public function index(Request $request)
    {
        $course=Student::with(['StudentDetail' => function($query){

           $query->with('state);
}])->get();
        $courses=$course->orderBy('studentDetail.state');
        echo "<pre>";print_r($courses);exit;
        return view('student.student_listing',['result'=>$courses]);

    }

Or you can simply get using.

public function index(Request $request)
    {
        $course=Student::with('StudentDetail.state')->get();;
        $courses=$course->orderBy('studentDetail.state');;
        echo "<pre>";print_r($courses);exit;
        return view('student.student_listing',['result'=>$courses]);

    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

migrate laravel table in descending order

From Dev

Delete a value from table in descending order PHP (Possible?)

From Dev

Delete a value from table in descending order PHP (Possible?)

From Dev

How can I paginate in descending order by child value with firebase?

From Dev

How can I paginate in descending order by child value with firebase?

From Dev

How to show the Id number in descending order?

From Dev

How can i print in descending order?

From Dev

How can I sort comments in descending order?

From Dev

How can I get the order ID in WooCommerce?

From Dev

how to order ascending descending?

From Dev

How to order by case, descending?

From Dev

How to get an order's items from an Order ID? (OpenCart 2.0)

From Dev

How to get the order id from $observer in magento

From Dev

Order Lua Table in Descending Order (Highest to Lowest)

From Dev

How can I make my query set order by descending in django based on auto increment id field

From Dev

How to join two tables and order by a third table?

From Dev

How to get order ID from shipment ID or from observer?

From Dev

How can I get the sale_order.id from sale_order_line onchange method? In odoo 9

From Dev

How to select rows with id just larger than x in descending order

From Dev

How to select rows with id just larger than x in descending order

From Dev

How to Fetch data in Descending order in Laravel 5.6 using provider

From Dev

Get order total value using jquery from table

From Dev

Numpy - how to sort an array of value/key pairs in descending order

From Dev

How to sort in descending order with numpy?

From Dev

How to get list of Call log with descending order in android?

From Dev

How to get list of Call log with descending order in android?

From Dev

How to get 5 record of each group with order by descending using mysql?

From Dev

how to sort a series with positive value in assending order and negative value in descending order

From Dev

how to sort a series with positive value in assending order and negative value in descending order

Related Related

  1. 1

    migrate laravel table in descending order

  2. 2

    Delete a value from table in descending order PHP (Possible?)

  3. 3

    Delete a value from table in descending order PHP (Possible?)

  4. 4

    How can I paginate in descending order by child value with firebase?

  5. 5

    How can I paginate in descending order by child value with firebase?

  6. 6

    How to show the Id number in descending order?

  7. 7

    How can i print in descending order?

  8. 8

    How can I sort comments in descending order?

  9. 9

    How can I get the order ID in WooCommerce?

  10. 10

    how to order ascending descending?

  11. 11

    How to order by case, descending?

  12. 12

    How to get an order's items from an Order ID? (OpenCart 2.0)

  13. 13

    How to get the order id from $observer in magento

  14. 14

    Order Lua Table in Descending Order (Highest to Lowest)

  15. 15

    How can I make my query set order by descending in django based on auto increment id field

  16. 16

    How to join two tables and order by a third table?

  17. 17

    How to get order ID from shipment ID or from observer?

  18. 18

    How can I get the sale_order.id from sale_order_line onchange method? In odoo 9

  19. 19

    How to select rows with id just larger than x in descending order

  20. 20

    How to select rows with id just larger than x in descending order

  21. 21

    How to Fetch data in Descending order in Laravel 5.6 using provider

  22. 22

    Get order total value using jquery from table

  23. 23

    Numpy - how to sort an array of value/key pairs in descending order

  24. 24

    How to sort in descending order with numpy?

  25. 25

    How to get list of Call log with descending order in android?

  26. 26

    How to get list of Call log with descending order in android?

  27. 27

    How to get 5 record of each group with order by descending using mysql?

  28. 28

    how to sort a series with positive value in assending order and negative value in descending order

  29. 29

    how to sort a series with positive value in assending order and negative value in descending order

HotTag

Archive