0

I want to save more than one university under one student. For html select I used

<select class="selectpicker" multiple data-live-search="true" name="university">
  @foreach($districts as $row)
     <option value= {{$row->name}} > {{$row->name}} </option>
  @endforeach
</select> 

This taking previously saved university. I am using district as university.

Controller->

public function store(Request $request)
    {
        
         Seller_info::create([
            'name' => $request['name'],
            'email' => $request['email'],
            'passport' => $request['passport'],
            'phone_number' => $request['phone_number'],
            'address' => $request['address'],
            'dateofbirth' => $request['dateofbirth'],
            'ielts' => $request['ielts'],
            'openingcharge' => $request['openingcharge'],
            'serviceCharge' => $request['serviceCharge'],
            'applydate' => $request['applydate'],
            'visaStatus'=> $request['visaStatus'],
            'country' => $request['country'],
            'university'=> $request['university'],
           
        ]);
        return back();
    }

This is saving the students. This is a mishmash code I am sorry about that. I am working on a different for fast learning. Here I am using seller_info as student.

The data is is saving university but only one university in the database. I tried to use for loop in case of storing. But could not able to implement it properly.

My table file->

Schema::create('seller_infos', function (Blueprint $table) {
            $table->id();
            $table->string('name')->nullable();
            $table->string('email')->unique();
            $table->string('passport')->nullable();
            $table->string('phone_number')->unique();
            $table->string('address')->nullable();
            $table->string('dateofbirth')->nullable();
            $table->string('ielts')->nullable();
            $table->string('openingcharge')->nullable();
            $table->string('serviceCharge')->nullable();
            $table->string('applydate')->nullable();
            $table->string('visaStatus')->nullable();
            $table->string('country')->nullable();
            $table->string('university')->nullable();
            $table->timestamps();

        });

And model

protected $fillable = [
        'name', 'email', 'passport', 'phone_number','address','dateofbirth','ielts', 'openingcharge','serviceCharge','applydate',
        'visaStatus','country','university'

    ];

1 Answer 1

1

If you want to store multiple university data for one student first of all you need 3 table, students(so your Seller Info), universities and seller_info_university(will be pivot table),

seller_info_university Migration must be like;

Schema::create('seller_info_university', function (Blueprint $table) {
    $table->bigInteger('seller_info_id')->unsigned();
    $table->bigInteger('university_id')->unsigned();
});

Add this code your SellerInfo Model;

public function universities()
{
    return $this->belongsToMany(University::class); // Your University model
}

Add this code your University Model;

public function students()
{
    return $this->belongsToMany(SellerInfo::class); // Your Student(SellerInfo) model
}

In your Controller try this

$student = Seller_info::create([
    'name' => $request['name'],
    'email' => $request['email'],
    'passport' => $request['passport'],
    'phone_number' => $request['phone_number'],
    'address' => $request['address'],
    'dateofbirth' => $request['dateofbirth'],
    'ielts' => $request['ielts'],
    'openingcharge' => $request['openingcharge'],
    'serviceCharge' => $request['serviceCharge'],
    'applydate' => $request['applydate'],
    'visaStatus'=> $request['visaStatus'],
    'country' => $request['country'],
    // 'university'=> $request['university'], you dont need there anymore  
    ]);

    $student->universities()->attach($request['universities']); // You can use attach(), detach() or sync() functions, you can search in Laravel Documentation

    return back();

And View;

<select class="selectpicker" multiple data-live-search="true" name="universities[]">
  @foreach($districts as $row)
     <option value= {{$row->name}} > {{$row->name}} </option>
  @endforeach
</select> 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you a lot. Specially for the comments. It helped me to understand more.
You're welcome, glad to hear if i helped for you

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.