mercredi 23 mai 2018

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

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] => neha@sob.com
            [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] => Ankit@sbo.com
            [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);
    }

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire