0

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

2 Answers 2

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

try this on controller

Sign up to request clarification or add additional context in comments.

2 Comments

In vueJS with laravel i have used component instead of blade therefore the solution will not work as I don't have view this is how my controller look like public function index() { return Student::latest()->paginate(5); }
This answer is correct. in your component pass the data as a prop. e.g. <component :students="{{ json_encode($students) }}" /> if you can not do this you will need to create that retrieve an api endpoint and get it with axios
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.