-2

I am having a issue where when i write out a eloquent query using a variable it is returning NULL but if i write out that variable value in text it returns data.

This is my code

$code = 'Test123';

LiveProducts::where('code', $code)->first();

When i dump that query out i get NULL, but if i type it out like this:

LiveProducts::where('code', 'Test123')->first();

Then it will return data from the database.

I have dumped out the variable and it is defiantly a string.

9
  • 2
    Please keep in mind that Laravel 5 is EOL since nearly five years!
    – Nico Haase
    Commented Mar 26 at 9:22
  • 2
    For the reason Nico Haase specified, I don't have a Laravel 5 app available to test this, but in Laravel 10 and 11 running the query $uuid = '...', then Model::where('uuid', $uuid)->first() and Model::where('uuid', '...')->first() both return the same single result (I don't have a code column, but same idea)... I can't recreate this, and I have no idea why this would be an error in your code; that's the syntax I've been using for over a decade 😅 How are you outputting this? dd(LiveProducts::where(...)->first())? Or some other method like the query logger? Did you try ->toSql()?
    – Tim Lewis
    Commented Mar 26 at 18:46
  • 1
    wild-guess: Have you tried adding an operator? LiveProducts::where('code', '=', $code)->first(); if you want directions on the upgrade to 11 feel free to reach out. It's pretty straight forward, but there's a couple nuances.
    – admcfajn
    Commented Mar 26 at 19:06
  • 2
    Which version of Laravel 5 is it? Did you dump the query to see what is really executing? do basic debugging and come back with info please Commented Mar 26 at 21:26
  • 1
    @TimLewis it is blowing my mind as well as i have used this type of query 1000s of times and never had this issue. I have tried ->first() and ->get() and same outcome. tried the ->toSQL() as well then inputted that directly into my database and worked fine. for the output i tried dd() the code direct, they i assigned it to a variable and dd() it as well. I am baffled!
    – Adam Allen
    Commented Mar 27 at 10:44

2 Answers 2

0

You can check with the following code as there may be some data value or data type matching issue with database column

$code = trim($code); // It will remove extra spaces
$code = (string) $code; // It will Ensure about string type value

LiveProducts::where('code', $code)->first();
-1

What you show makes no sense. Regardless the Laravel version (as somebody pointed out is way outdated) a variable that equals the text is the same as writing the test.

If I assume that you have this code in your Controller, both should be in the same function and then send them to the view.

LiveProductController should look like this (I'd prefer this query instead):

public function getCode() {
    $code = 'Test123';
    $product = LiveProducts::query()
        ->where('code', $code)
        ->first();
    return view('liveproducts.index', compact('product'));
}

Then your liveproducts.blade.index should be able to access the variable thru blade

{{ $product }}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.