BinuMon
100
1
Laravel

How to send data to database from a form using if or similar conditions:

Posted 3 months ago by BinuMon

Hey guys, i was creating a project on choosing two types of data to be sent to the database using conditions.

in that form, there are two Radio buttons "Artist, Label", and each radio button has sub input options.

  1. Account type: Artist (radio button) -> Artist Name -> Music Genre -> Sub Music Genre
  2. Account type: Label (radio button) -> Label Name

In this form, when i select Artist, it should not send data of Label name and when i select Label name it should not send any data under Artist to the Database. How do I do this?

// RegisterController.php

    /**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */

    protected function validator(array $data)
{
    return Validator::make($data, [
        'first_name' => ['required', 'string', 'max:255'],
        'last_name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
        'account-type' => ['required', 'string'],
        'artist_name' => ['required', 'string', 'max:25'],
        'label_name' => ['required', 'string', 'max:50'],
        'music_genre_id' => ['required', 'string'],
        'sub_music_genre_id' => ['required', 'string'],
    ]);
}
    protected function create(array $data)
{
    return User::create([            
        'first_name' => $data[ 'first_name'],
        'last_name' => $data[ 'last_name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
        'account-type' => $data['account-type'],
        'artist_name' => $data['artist_name'],
        'label_name' => $data['label_name'],
        'music_genre_id' => $data['music_genre'],
        'sub_music_genre_id' => $data['sub_music_genre'],

    ]);
    }

// User.php

use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
        'first_name', 
        'last_name', 
        'email',
        'password',
        'account-type',
        'artist_name',
        'label_name',
        'music_genre_id',
        'sub_music_genre_id',

];

Please sign in or create an account to participate in this conversation.