Debug School

rakesh kumar
rakesh kumar

Posted on

How to create public profile and authenticated profile using jquery and laravel

Applying Ternary operator on Input element of value
Retrieves the value of the hidden input field with the id
Applying Ternary operator to select route based on login condition
Apply route variable in html element

Step 1: hidden input field named "admin_id" is being used to store the ID of the currently authenticated user
Applying Ternary operator on Input element of value

 <input type="hidden" value="{{ Auth::user() ? Auth::user()->id : '' }}" name="admin_id" id="admin_id" /> 
Enter fullscreen mode Exit fullscreen mode

Step 2: retrieves the value of the hidden input field with the id "admin_id" using jQuery

Retrieves the value of the hidden input field with the id

  id = $('#admin_id').val();
  var isLoggedIn = $('#admin_id').val() !== '';  
Enter fullscreen mode Exit fullscreen mode

Step3: decide route based on id
Retrieves the value of the hidden input field with the id

   var isLoggedIn = $('#admin_id').val() !== '';
    var profileRoute = isLoggedIn ? '/profiles/' : '/public-profiles/';
Enter fullscreen mode Exit fullscreen mode

Step4: Apply link that lead to link will lead to a user profile
Apply route variable in html element

 html += '<div class="col-md-2 text-right">';
    html += '<a href="' + profileRoute + item.user_id + '" target="_blank" class="btn btn-sm btn-primary" role="button">View Profile</a>';
    html += '</div>';
Enter fullscreen mode Exit fullscreen mode
     if (isLoggedIn) {
    if (isAdded) {
        // Add a row for the buttons
        html += '<div class="row">';
        html += '<div class="col-md-8 text-right">';      
        html += '</div>';
    html += '<div class="col-md-2 text-right">';
    html += '<a href="' + profileRoute + item.user_id + '" target="_blank" class="btn btn-sm btn-primary" role="button">View Profile</a>';
    html += '</div>';
        // Add a column for the "Go to Cart" button
        html += '<div class="col-md-1 text-right">';
        html += '<a href="{{ route('carts') }}" class="btn btn-sm btn-primary" role="button">Go to Cart</a>';
        html += '</div>';
        // Add a column for the existing button
        html += '<div class="col-md-1 text-right">';
        html += '<button class="btn btn-sm ' + buttonClass + ' add-to-cart-button" data-id="' + item.id + '" ' + buttonDisabled + ' onclick="mySocialsClick(this, event)" style="cursor: ' + (isAdded ? 'default' : 'pointer') + ';">' +
    buttonText +
    '</button>';
        html += '</div>';
        html += '</div>';
    } else {
        // Add a row for the existing button
        html += '<div class="row">';
        html += '<div class="col-md-9 text-right">';   
        html += '</div>';
        html += '<div class="col-md-2 text-right">';
    html += '<a href="' + profileRoute + item.user_id + '" target="_blank" class="btn btn-sm btn-primary" role="button">View Profile</a>';
    html += '</div>';
        html += '<div class="col-md-1 text-right">';
        html += '<button class="btn btn-sm ' + buttonClass + ' add-to-cart-button" data-id="' + item.id + '" ' + buttonDisabled + ' onclick="mySocialsClick(this, event)" style="cursor: ' + (isAdded ? 'default' : 'pointer') + ';">' +
    buttonText +
    '</button>';

        html += '</div>';
        html += '</div>';
    }
}

            else{


            html += '<div class="row">';
            html += '<div class="col-md-9 text-right">';   
            html += '</div>';
            html += '<div class="col-md-2 text-right">';
            html += '<a href="' + profileRoute + item.user_id + '" target="_blank" class="btn btn-sm btn-primary" role="button">View Profile</a>';
            html += '</div>';
            html += '</div>';

            }
Enter fullscreen mode Exit fullscreen mode

Image description

step5: Mention two different route based on login condition

Route::get('/profiles/{slug}', [App\Http\Controllers\HomeController::class, 'profiles'])->name('profiles');
Enter fullscreen mode Exit fullscreen mode
Route::get('/public-profiles/{slug}', [App\Http\Controllers\GuestController::class, 'publicprofiles'])->name('public-profiles');

Enter fullscreen mode Exit fullscreen mode

step 5 mention controller function for two different route.

first controller function without login

  public function publicprofiles($slug)
    {


        $user_slug_image = Addprofile::where('user_id', $slug)->first();
        $get_number = $user_slug_image->mobile;
        // $finally_get_number = substr_replace($get_number,"xxxxxx",2,6);
        $get_email = $user_slug_image->user_email;
        // $finally_get_email = substr_replace($get_email,"xxxxxx",3,8);
        log::info('get email me kya aa rha hai' . $get_email);
        // log::info('now final get number with hide options'.$finally_get_number);
        $getting_slug_image = $user_slug_image->file_pic;
        Log::info('aar ha hai ki nhi ' . $getting_slug_image);
        $url = Addprofile::where('user_id', $slug);
        $profile_getting = Addprofile::where('user_id', $slug)->first();
        $country_id = $profile_getting['country_id'];
        Log::info('now get country_id ka value' . $country_id);
        $country = Country::where('country_id', $country_id)->first();
        $get_country_name = $country['country_name'];
        Log::info(' getting country name ' . $get_country_name);
        $state_id = $profile_getting['state_id'];
        $state = State::where('state_id', $state_id)->first();
        $get_state_name = $state['state_name'];
        Log::info(' getting state name ' . $get_state_name);
        $city_id = $profile_getting['city_id'];
        $city = City::where('city_id', $city_id)->first();
        $get_city_name = $city['city_name'];
        Log::info(' getting city name ' . $get_city_name);
        $profile_user_url = Addprofile::where('user_id', $slug)->first();
        Log::info('we are getting' . $profile_user_url);
        $gets_image = $profile_user_url->file_pic;
        $pay_user = payments::where('admin_id', $slug)->first();
        $price_social = DB::connection('payments')
        ->table("social_url")
        ->where('user_id', $slug)
        ->first(); // This will return a JSON-encoded string
        $get_digital_marketer = $profile_getting['digital_marketer'];
        return view('pages.profile', compact('profile_getting', 'getting_slug_image', 'get_country_name', 'get_state_name', 'get_city_name', 'get_number', 'get_email', 'gets_image', 'profile_user_url', 'pay_user','get_digital_marketer','price_social'));
    }
Enter fullscreen mode Exit fullscreen mode

Second Controller function with login

 public function profiles($slug)
    {
        $slugs = Auth::user()->slug;
        $id = Auth::user()->id;
        Log::info('we are getting user profiledfv user id ' . $id);
        $user_slug_image = Addprofile::where('user_id', $slug)->first();
        $get_number = $user_slug_image->mobile;
        // $finally_get_number = substr_replace($get_number,"xxxxxx",2,6);
        $get_email = $user_slug_image->user_email;
        // $finally_get_email = substr_replace($get_email,"xxxxxx",3,8);
        log::info('get email me kya aa rha hai' . $get_email);
        // log::info('now final get number with hide options'.$finally_get_number);
        $getting_slug_image = $user_slug_image->file_pic;
        Log::info('aar ha hai ki nhi ' . $getting_slug_image);
        $url = Addprofile::where('user_id', $slug);
        $profile_getting = Addprofile::where('user_id', $slug)->first();
        $country_id = $profile_getting['country_id'];
        Log::info('now get country_id ka value' . $country_id);
        $country = Country::where('country_id', $country_id)->first();
        $get_country_name = $country['country_name'];
        Log::info(' getting country name ' . $get_country_name);
        $state_id = $profile_getting['state_id'];
        $state = State::where('state_id', $state_id)->first();
        $get_state_name = $state['state_name'];
        Log::info(' getting state name ' . $get_state_name);
        $city_id = $profile_getting['city_id'];
        $city = City::where('city_id', $city_id)->first();
        $get_city_name = $city['city_name'];
        Log::info(' getting city name ' . $get_city_name);
        $profile_user_url = Addprofile::where('user_id', $slug)->first();
        Log::info('we are getting' . $profile_user_url);
        $gets_image = $profile_user_url->file_pic;
        $pay_user = payments::where('admin_id', $slug)->first();
        $price_social = DB::connection('payments')
        ->table("social_url")
        ->where('user_id', $slug)
        ->first(); // This will return a JSON-encoded string
        $get_digital_marketer = $profile_getting['digital_marketer'];
        return view('pages.profile', compact('profile_getting', 'getting_slug_image', 'get_country_name', 'get_state_name', 'get_city_name', 'get_number', 'get_email', 'gets_image', 'profile_user_url', 'pay_user','get_digital_marketer','price_social'));
    }
Enter fullscreen mode Exit fullscreen mode

In both controller function renders same blade file

Image description

output

Image description

Top comments (0)