The Laravel background-image:url function is yielding a result of 1

Currently working on a web application built in Laravel and encountered an issue with the code snippet below:

<div class="user-info" style="background-image:url({{ asset(Auth::user()->partner->background) or '/images/default/background1.jpg'}})">

Upon rendering in HTML, this is what I see:

<div class="user-info" style="background-image:url(1)">

Expected output from the database should be:

images/backgrounds/HRVl7TXkAxlhASj14vAV.png

It's puzzling because when I execute the following line of code:

{{dd(Auth::user()->partner->background)}}

The filename

images/backgrounds/HRVl7TXkAxlhASj14vAV.png
is displayed correctly. Why does it show 1 instead of the filename when used in a background-image property?

Answer №1

Give this a try

{!! "'".asset(Auth::user()->partner->background)."'" or '/images/default/background1.jpg'!!}

UPDATE

A bit messy, but it should do the job

{!! Auth::user()->partner && Auth::user()->partner->background ? "'".asset(Auth::user()->partner->background)."'" : '/images/default/background1.jpg' !!}

I suggest creating a separate method in the User model for this functionality.
Something similar to this:

public function background()
{
    // your amazing logic here
    return $pathToBackground;
}

and then you can use it like this:

<div class="user-info" style="background-image:url('{!! asset(Auth::user()->background()) !!}');" >

Answer №2

attempt

   <div class="profile-details" style="background-image:url({{ asset(($bg=Auth::user()->partner->background) ? $bg :'/images/default/background1.jpg' )}})">

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Steps to activate CORS using .htaccess in a ZURB Foundation Project

Embarking on my first website development project from scratch, I took on the task of handling front-end, back-end, and database integration. Utilizing the ZURB Foundation Framework (ZURB Template), I structured my project to be modular with Webpack4, Babe ...

Setting up a checkbox to accept either a 0 or 1 value from a database

I am trying to integrate an HTML table where each table data cell contains a drop-down menu and a checkbox. These elements are connected to a database table named "checkbox" which contains values of 1s and 0s. I want to dynamically display the check box as ...

Utilizing Google Analytics with Laravel Jetstream

Currently, I'm facing challenges with setting up Google Analytics to properly report data with the InertiaJS stack on Laravel Jetstream. My goal is to track individual page visits within this single-page application, but I'm uncertain about the a ...

Database Update Failure in CodeIgniter Using AJAX

Apologies for my poor English, I will try my best to explain my issue. I am facing a problem with using AJAX to send data to the controller and update the model. The AJAX request fails when attempting to update the database. Below is the AJAX code in my v ...

Import JSON data containing multiple objects into MySQL database

I am working with some JSON data that looks like this: [{"data1":"body1"},{"data1":"body2"}] I need to save the data1 values to my database using the following PHP code: <?php $query = mysql_query("INSERT INTO data VALUES('$datadecode')"); ...

Crack the code within the string

echo $name displays Mount Kimbie &mdash; Carbonated. What is the correct way to display Mount Kimbie — Carbonated? The characters &mdash;, quotes and others should be decoded into their regular symbols. I have attempted both htmlspecialchars_d ...

Using Ajax (Jquery) to send data to a PHP script

Currently, I am working on an application where users can click a checkmark to complete a task. When this action is taken, a popup window appears (created using bootstrap), prompting the user to enter their hours worked on the task. After entering the hour ...

Tips for invoking the controller's update method

I have been encountering an issue while attempting to utilize the update function of my controller on the dashboard.blade.php page. Upon clicking "Save", instead of updating, the page switches to show.blade.php. Content of my dashboard.blade.php: ...

Storing two SQL values in a new array

I am brand new to PHP and I am looking to perform two SELECT COUNT queries on a MySQL column, and then store the results in an array. However, the code I have written is not functioning correctly. // Establish DB connection $link = mysqli_connect("local ...

Receiving an incorrect UTF-8 sequence in the JSON input

I currently have an ec2 server running with PHP version 5.3, hosting 2 virtual hosts: one for development and one for live production. Interestingly, on the development host, if someone uploads a file containing invalid UTF-8 characters, it simply ignores ...

Arranging elements in an array through the use of usort while incorporating a function that is

Currently, I am utilizing the PHP usort function to arrange an array. To achieve this, a custom PHP function is essential due to its dynamic nature. $intCompareField = 2; $functSort = function($a, $b) { return ($a[$intCompareField] > $b[$intCompareFi ...

You may encounter an Apache 406 error message if you try to specify the Accept header in

I've been struggling to find a solution for this particular issue, and so far nothing I've tried has worked. Despite enabling PHP and the Apache web server on my Mac, I keep encountering a 406 error when passing a request (POST or GET) with the A ...

Acquiring administrative authorization upon user login with PHP

I have developed a registration form and a login form using PHP. However, whenever I run this code, it displays the message "invalid username" when the username is invalid, or it says "your account isn't approved by admin yet." Can anyone please guide ...

What is the proper way to pass variables for alternative if-syntax?

<?php $a=3; $dir= '/php/'; ?> <?php if($a ==3){ ?> <form action="/php/form.php" method="post"> <p>Name: <input type="text" name="name" /></p> <p>Age: <input type="text" name="age" /></p> ...

Displaying HTML with extracted message form URL

I have a link that redirects to this page Is there a way for me to extract the message "Message Sent Successfully" from the URL and display it in the form below? <form action="send_form_email.php" name="contactForm" method="post"> //I want to d ...

The anticipated JSON layout does not align with the Laravel's composer package at the local level

I've been attempting to import a local package into my Laravel 8 project, but I keep encountering the following error in ./composer.json: "./composer.json" does not match the expected JSON schema: ...

The jQuery upload feature fails to function properly when attempting to upload multiple images simultaneously on a single webpage

Overview: I am currently in the process of developing an instant upload feature using Jquery/Ajax. The goal is to display a Fancybox with an upload field when a user double-clicks on an image. Once the user selects an image, it should be uploaded and the s ...

editing a json object using php

I am working on altering a JSON file using PHP. Currently, I have the following code that is not functioning as expected. How should I modify it to change the "obj_name" property in the JSON object to the value of $name? <?php $json = $_POST['m ...

Sort entries in a database using PDO with the combination of "

Hello, I am attempting to retrieve a list of names from a table using PDO with a specified first letter and sorting them in ascending order. However, I am encountering issues with empty Arrays. include('core/engine/login.php'); $con = new PDO(&a ...

The array that I am getting back is not being returned as an object

After making an AJAX request using a certain function, I noticed that the returned array is not in a valid format. It's quite different from what I'm used to in my AJAX experience. function get_within($latitude, $longitude) { global $pdo; // ...