Support the ongoing development of Laravel.io →
Requests Input Views
Last updated 2 years ago.
0

Try to dd($products); in your controller. Do you get a result?

If database query doesn't return a result, the $results variable will be undefined.

Try to do an if/else in your view to display view without a result.

{{ (isset($product->supplier) ? $result->supplier : "no supplier") }}

Last updated 2 years ago.
0

Try checking if you have an empty result..you can simply do count($results) and put inside the foreach code block if it is more than 0.. and try submitting a supplier that will surely give a not empty result.

Last updated 2 years ago.
0

Volzy said:

Try to dd($products); in your controller. Do you get a result?

If database query doesn't return a result, the $results variable will be undefined.

Try to do an if/else in your view to display view without a result.

{{ (isset($product->supplier) ? $result->supplier : "no supplier") }}

It doesn't throw up the 'Undefined variable' error anymore. Now it just echos 'no supplier' in the view.

Last updated 2 years ago.
0

johnvic said:

Try checking if you have an empty result..you can simply do count($results) and put inside the foreach code block if it is more than 0.. and try submitting a supplier that will surely give a not empty result.

I can't even render the page (view) without Laravel throwing up the error. Do you think that it might be due to an error in my route? I can manage to do user authentication and validation on this form but I just cannot pull data from my db.

Last updated 2 years ago.
0

Got a breakthrough. If I were to include Sesson in my code, the view renders without the 'Undefined variable' BUT the search does not return anything. Code snippet below.

  @if(Session::has('results'))
  @foreach($results as $result)
    <tr>
    <td> {{$result->product_no}} </td>
    <td> {{$result->supplier}} </td>
    <td> {{$result->product_cost}} </td>
    </tr>
  @endforeach
  @endif

If I remove the Session @if code blocks, the view renders properly AND the search works, but only for a minute or two before Laravel complains about the 'Undefined variable' again. I'm a little confused about things at this point.

Last updated 2 years ago.
0

bhbh said:

Volzy said:

Try to dd($products); in your controller. Do you get a result?

If database query doesn't return a result, the $results variable will be undefined.

Try to do an if/else in your view to display view without a result.

{{ (isset($product->supplier) ? $result->supplier : "no supplier") }}

It doesn't throw up the 'Undefined variable' error anymore. Now it just echos 'no supplier' in the view.

That's because there was no supplier in your result. As I wrote, if supplier is set, then display supplier else display "no supplier".

I'm not sure why you would use a session for your search.

Last updated 2 years ago.
0

Volzy said:

That's because there was no supplier in your result. As I wrote, if supplier is set, then display supplier else display "no supplier".

I'm not sure why you would use a session for your search.

I got a little over-excited, I'm sorry. I backtracked and continued working from your angle. My code in ProductController.php is now this.

  public function searchInventory()
{
	$input = Input::get('search');
	$results = DB::table('products')->where('supplier', '=', $input)->get();
	dd($results);
	//return View::make('pages.products')->with('results', $results);
}

And this is the code in products.blade.php

{{ (isset($results->supplier) ? $results->supplier:'no supplier') }}

After searching for the supplier, it returns the the correct results on the page (in a debugging format). It seems the search is successful when using the dd function. Could I get your input on this, Volzy? Sorry for wasting your time earlier man. My apologies.

Last updated 2 years ago.
0

Could you also do

<pre>
 {{print_r($results)}} 
</pre>

in your blade and see if the said results also display?

Last updated 2 years ago.
0

johnvic said:

Could you also do

<pre>
{{print_r($results)}} 
</pre>

in your blade and see if the said results also display?

Hi johnvic, this throws up the 'Undefined variable' as well. The only way I can get results to display is by using dd($results), or by using a @if(Session) THEN removing it.

The latter displays the search results in the view perfectly until I navigate to another page and back, which then throws up the same 'Undefined variable' again.

*Edit: I realized I wasn't very clear. The 'Undefined variable' error prevents me from even loading the basic view by itself, if this information is any help.

Last updated 2 years ago.
0

This is very weird issue since I am using the same code in my queries using eloquent and it is working fine. Can u try using eloquent instead or try using using different variable name instead of $results..:)

Last updated 2 years ago.
0

johnvic said:

This is very weird issue since I am using the same code in my queries using eloquent and it is working fine. Can u try using eloquent instead or try using using different variable name instead of $results..:)

Yeah man this is seriously some ghostbusters nonsense right here. I might give Eloquent a shot later. Yup. I tried to use different variable names but its still the same. Thanks for your help! I really appreciate the input (:

Last updated 2 years ago.
0

I think there is no problem in your controller since as you've said doing dd($results) is displaying the right results. Can u confirm that this will display. Change your products.blade.php to just display some text and do not use the $results variable. Expected it will be display the text. What is ur laravel version?maybe u could try to do composer update.

Last updated 2 years ago.
0

johnvic said:

I think there is no problem in your controller since as you've said doing dd($results) is displaying the right results. Can u confirm that this will display. Change your products.blade.php to just display some text and do not use the $results variable. Expected it will be display the text. What is ur laravel version?maybe u could try to do composer update.

Laravel version is 4.2.6. I just did a composer update, same problem. Created a new project in another folder and manually entered all codes again, still same problem.

I got 2 questions for now.

1 - I just can't figure out why I can get data in its debuggable/array format when using isset but not in a proper view.

2 - Every example I see online is using Models to serve their DB queries instead, unlike my method where I place my logic in the Controllers. Is my way of doing things wrong?

Last updated 2 years ago.
0

I DID IT! I GOT IT TO WORK!!!! Sorry for the caps I'm just really relieved.

This is the new code in my products.blade.php.

  @if (isset($results)) 
  @foreach($results as $result)
  <tr>
  <td> {{$result->path}} </td>
  <td> {{$result->product_no}} </td>
  <td> {{$result->supplier}} </td>
  <td> {{$result->cost}} </td>
  </tr>
  @endforeach
  @endif

This is the code in my ProductController.php

   public function searchInventory() {
	$input = Input::get('search');
	$results = DB::table('products')->where('supplier', '=', $input)->get();;
	return View::make('products')->with('results', $results);
}

I thought that if it works by echoing during a dd($results) or a isset, then I should just place the isset echo within an @if statement and see if it works from there. Thank you both for being so patient with me! I really appreciate the help from you guys!

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

bhbh bhbh Joined 6 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.