Как вызвать пользовательскую функцию из другой пользовательской функции в PHP - PullRequest
2 голосов
/ 29 сентября 2019

У меня есть функция для userDashboard(), и я должен вызвать updateUserlocation() функцию внутри этой userDashboard() функции.

Скажите, пожалуйста, как это сделать.

Я пытался добавить updateUserLocation() внутрь userDashboard(), но он не работает.

Эта ошибка появляется:

Вызов неопределенной функции App \ Http \ Controllers \ userUpdateLocation ()

Это функция userDashboard: -

public function userDashboard()
{
    $user = Session::get('user_account');

    $commonModel = new CommonModel();
    $userModel = new User();
    $data = $commonModel->commonFunction();
    $login = GlobalData::isLoggedIn();

    if ($login) {
        return redirect(url('/'));
    }

    $data['user_session'] = Session::get('user_account');
    // Get the groups joined by the user
    $user_id = $data['user_session']['id'];
    $get_groups = DB::table('trans_group_users')
        ->where('user_id_fk', $user_id)
        ->get();
    // if user's group count is zero, redirect to signup2 to choose groups first
    if (count($get_groups) == 0) {
        Session::flash('error_msg', 'Please select atleast one group to proceed further.');
        return redirect()->guest('/signup2');
    }
    $lang_id = 14;
    $arr_user_data = array();
    $user_id = $data['user_session']['id'];
    /* Differciate date for get record for perticuler month */

    $today_date = date("Y-m-d");
    $first_date = date("Y-m-01");
    $date1 = date_create($today_date);
    $date2 = date_create($first_date);
    $diff = date_diff($date1, $date2);
    $remain_days = $diff->days;
    $today_with_minus_days = date('Y-m-d', strtotime("-$remain_days days"));

    /* Ends here */

    /* notification count */
    $arr_user_data = $userModel->find($user_id);
    $arr_user_data = $arr_user_data;


    $get_events = DB::table('mst_event as e')
        ->join('trans_event_rsvp_status as r', 'r.event_id_fk', '=', 'e.id')
        ->where('r.user_id_fk', $user_id)
        ->where('r.status', 1)
        ->where('e.start_date', '>', $today_with_minus_days)
        ->where('e.end_date', '<', $today_date)
        ->select('e.start_date')
        ->groupBy('e.id')
        ->get();


    $get_e = array();
    if (count($get_events) > 0) {
        foreach ($get_events as $object) {
            $get_e[] = (array) $object;
        }
    }

    $get_meetups = DB::table('mst_meetup as m')
        ->join('trans_meetup_rsvp_status as r', 'r.meetup_id_fk', '=', 'm.id')
        ->where('r.user_id_fk', $user_id)
        ->where('m.start_date', '>', $today_with_minus_days)
        ->where('m.start_date', '<', $today_date)
        ->where('r.status', 1)
        ->select('m.start_date')
        ->groupBy('m.id')
        ->get();


    $get_m = array();
    if (count($get_meetups) > 0) {
        foreach ($get_meetups as $object) {
            $get_m[] = (array) $object;
        }
    }

    $arr = array();
    $arr = array_merge($get_e, $get_m);
    $upcoming_going_event_count = count($arr);
    if ($upcoming_going_event_count > 0) {
        Session::put('going_count', $upcoming_going_event_count);
    } else {
        Session::forget('going_count');
    }
    /* Ends here */
    $data['header'] = array(
        "title" => 'My Home | ATG',
        "keywords" => '',
        "description" => ''
    );

    return view('Frontend.user.dashboard')
        ->with('title', 'Profile')
        ->with('finalData', $data)
        ->with('arr_user_data', $arr_user_data);
}

Это updateUserlocation функция: -

public function updateUserLocation()
{
    /* Update User Location based on input from the browser, else from IP */
    $all = Input::all();
    $latitude = (isset($all['latitude']) ? $all['latitude'] : '');
    $longitude = (isset($all['longitude']) ? $all['longitude'] : '');
    $agent = (isset($all['agent']) ? $all['agent'] : '');
    $commonModel = new CommonModel();
    $data = $commonModel->commonFunction();
    $data['user_session'] = Session::get('user_account');
    $user_id = $data['user_session']['id'];
    if ($latitude == '' && $longitude == '') {
        try {
            $ip = Request::ip();
            $data = \Location::get($ip);
            $latitude = $data->latitude;
            $longitude = $data->longitude;
            $agent = $_SERVER['HTTP_USER_AGENT'];
        } catch (\Exception $e) {
            \Log::alert($e->getMessage());
            \Log::debug($e);
        }
    }
    $this->UpdateUserLocationfx($user_id, 'web', $latitude, $longitude, $agent);
}

1 Ответ

1 голос
/ 29 сентября 2019

Laravel / PHP считает, что вы вызываете класс или контроллер вместо метода.

Если метод userUpadateLocation() находится в том же контроллере, что и userDashboard(), используйте переменную $this для вызова метода из того же контроллера:

$this->userUpadateLocation();

$thisпереводится как «использовать этот метод контроллера».

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...