mchomvu
mchomvu

Reputation: 717

How to select data from two related table in Laravel and VueJS

I have two tables, students and parents, student table references parent id, I need when i register a parent when it come to register a student i select first the parent name of a particular student. Please anyone who can help me

I have managed to create both tables the models and its relationship

Here is my student model

namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
       protected $fillable = [
          'parentt_id','admission', 'fname', 
          'mname','lname','dob','gender','form_admitted',
          'transfered_from',
          'transfered_to'
        ];

         public function student()
         {
             return $this->belongsTo(Parentt::class);
         }

         public function fee()
         {
            return $this->belongsTo(Student::class);
         }
 }

This is by parent model

namespace App;
use Illuminate\Database\Eloquent\Model;
class Parentt extends Model
{
       protected $fillable = [
          'fname', 'mname','lname','occupation',
          'residence','address','phone',
       ];
       public function parentt()
       {
              return $this->hasMany(Student::class);
       }
 }

This is my view component at student component

<div class="form-group">
     <select  class="form-control" >
          <option v-for ="parentt in parentts" :key="parentt.id"> 
             {{parentt.fname}}
          </option>
     </select>
</div>

my expecting output

Upvotes: 0

Views: 919

Answers (2)

Joseff
Joseff

Reputation: 21

Try to make an XHR request once the view is charged, to get the data. Or maybe you can try to use this package:

https://github.com/laracasts/PHP-Vars-To-Js-Transformer

To write global variables in your javascript, but with PHP.

Upvotes: 0

iAmGroot
iAmGroot

Reputation: 868

public function retrive(Request $request)
{   
     $parent = Parent::with('student')->get();
     return view('your_page',['parent'=>$parent]);
}

try this on controller

Upvotes: 1

Related Questions