Home > OS >  how to pass v-model value id to laravel controller?
how to pass v-model value id to laravel controller?

Time:06-16

I have a problem passing the select option id to laravel controller using axios post. What i want to achieve here is that i want to pass the id value in v-model to laravel controller so that i can fetch other value of the device based on the id.

Here is my select option:

    <div >
        <select  v-model="choosed">
           <option :value="null">Choose Device to Send SMS</option>
           <option v-for="item in devices" :key="item.id" v-bind:value="item.id">{{ item.device_name }}</option>
        </select>
    </div>

Here is my data:

   export default {
        data() {
            return {
                search: "",
                contacts: {}, 
                devices: {}, 
                file: '',
                paginate: 5,   
                selected: [],
                choosed: null,
                select_all: false,  
                maxCount: 320,
                remainingCount: 320,
                hasError: false,  
                form: {
                    first_name: null,
                    last_name: null,
                    mobile_number: null,
                },
                sms: {
                    // secret: null,
                    // deviceID: null,
                    message: null,
                },
            }
        },

Here is my axios post to connect to controller:

   Send(selected, choosed) {
    const vm = this;
    vm.sms.ids = selected;
    vm.sms.chooseds = choosed;
    axios.post("/api/send-sms", this.sms)
            .then(function(response) {
                if(response.data.status == 'success'){
                    vm.getContact();
                    vm.select_all = false;
                    vm.selected = [];
                    vm.choosed = null;
                    // this.sms.secret = null;
                    // this.sms.deviceID = null;
                    vm.sms.message = null;
                    swal('Send Success!', response.data.message,'success');
                }
                else{
                    swal('Error',response.data.message,'error');  
                }
                // if(response.data.status == 'error'){
                //     swal('Error',response.data.message,'error');  
                // }
                // if(response.data.status == 'errors'){
                //     swal('Error',response.data.message,'error');  
                // }
            })
            .catch(function (error) {
                console.log(error)
            });
    },

Here is my controller:

public function sendMessage(Request $request){

    date_default_timezone_set('Asia/Manila');
    ini_set('max_execution_time', '0');
    // $secret = $request->input('secret');
    // $deviceID = $request->input('deviceID');
    $message = $request->input('message'); 
    $device = $request->input('chooseds');
    
        if(empty($request->input('ids'))) {
            return response()->json([
                'status' => 'error',
                'message' => 'Please Select Contact!'
            ]);
        }
        if($device == null) {
            return response()->json([
                'status' => 'error',
                'message' => 'Please Select Device!'
            ]);
        }
        // if(empty($secret) || empty($deviceID) || empty($message)) {
        if(empty($message)) {
            return response()->json([
                'status' => 'error',
                'message' => 'Input fields are required!'
            ]);
        }
        else
        {
            if(!empty($request->input('ids')) || !empty($device)) {
                $single_user_id = $request->input('ids');    
                $device_secret = Device::where('id', $device)->value('device_secret');   
                $device_id = Device::where('id', $device)->value('device_id');   
                $time = time();
                $secretMd5 = md5($device_secret.$time);  
                foreach ($single_user_id as $id) {   
                    $number = Contact::where('id', $id)->value('mobile_number');
                    file_get_contents("http://server_url/?to=".urlencode(trim($number))."&text=".urlencode($message)."&secret=$secretMd5&time=$time&deviceID=".$device_id);
                    sleep(20);
                }
                return response()->json([
                    'status' => 'success',
                    'message' => 'Selected Contact Succesfully Send SMS!'
                ]);
            }
        }  
}

CodePudding user response:

First you don't need to define 'this' in your function if u used Laravel inertia vue. your code is a little messy ill give you how you should pass and get parameters So try the axios call like below

axios.post("/api/send/sms", {'name' : 'Unswaa'});

then in your controller you can get the name like below

$request->name;
  • Related