0

I currently have this in my view to output data from an array that comes from my controller:

<!DOCTYPE html>
<html>
  @foreach ($details as $detail)
  <head>
    <meta charset="utf-8">
    <title>{{ $detail->name }}</title>
  </head>
  <body>
    <h1>in view</h1>

      {{ $detail->name }}
      <br>
      {{ $detail->street }}
      <br>
      {{ $detail->city }}, {{ $detail->state }}. {{ $detail->zip }}


  </body>
    @endforeach
</html>

This is the function in my controller:

  $details = DB::table('restaurants')->where('id', $restaurant_id)->get();
  return view ('restaurant.detail')->with('details', $details);

My question is: is there a better way to do this? I tried using the blade syntax without the @foreach and didn't have any luck.

I don't want to output this multiple times, the only reason I have the foreach there is because it is the only way I could get it to output.

If this is how it is supposed to work, no worries, I am just not familiar enough yet with blade to know if there is a better way to output this.

Thank you!

8
  • You shouldnt be recreating entire HTML documents. What is it exactly you want? Commented Jun 10, 2017 at 18:48
  • @ImAtWar first I had the foreach in just the body, but I wanted to be able to output the $detail->name in the <title>, therefore I just wrapped it around the full HTML doc to get that to work. I want to be able to output this array, but it only works with the foreach. Is there a way where I won't need that? Commented Jun 10, 2017 at 18:51
  • Having the <!DOCTYPE html> and <html> elements inside a loop looks very wrong. A browser will probably only render one <html> document and ignore any subsequent ones. Commented Jun 10, 2017 at 18:53
  • @apokryfos correct. It will work if I move the foreach (fixed above), but is there a way to output the array without the need for the foreach loop? Commented Jun 10, 2017 at 18:55
  • @Ddrossi93 technically speaking, no. However you can use one of the many functions that does the looping for you like e.g. $details->each(function ($detail) { /* do stuff */ }); Commented Jun 10, 2017 at 19:10

3 Answers 3

2

You seem to be selecting something by id, so that's probably unique. When you do ->get() by default it will return a collection of results because it assumes there's always a chance that there's more than 1. When selecting by ID however you know if something exists by that id, there's only going to be 1 of it. You can change the code to:

$detail = DB::table('restaurants')->where('id', $restaurant_id)->first();
return view ('restaurant.detail')->with('detail', $detail); 

<html>
  <head>
    <meta charset="utf-8">
    <title>{{ $detail->name }}</title>
  </head>
  <body>
    <h1>in view</h1>

      {{ $detail->name }}
      <br>
      {{ $detail->street }}
      <br>
      {{ $detail->city }}, {{ $detail->state }}. {{ $detail->zip }}


  </body>
</html>

I highly recommend you look into Eloquent

Example of an eloquent model:

class Restaurant extends Model {} //The table name for model "Restaurant" is assumed to be restaurants

Then you can find a single restaurant:

$detail = Restaurant::find($restaurant_id);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks again. I'll start digging more into Eloquent
0

Why do you want multiple title tags in your page ? It doesn't make any sense.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title><!-- What do you want to show here ? --></title>
  </head>
  <body>
    <h1>in view</h1>
        @foreach ($details as $detail)
            {{ $detail->name }}
            <br>
            {{ $detail->street }}
            <br>
            {{ $detail->city }}, {{ $detail->state }}. {{ $detail->zip}}
        @endforeach
  </body>
</html>  

5 Comments

I don't want multiple title tags. The $detail->name will not work unless it is within the foreach loop. That is my question. Is there a way to do it without the foreach
Is there always one element in your array ?
In your controller just put $detail = details[0] or $detail = current(details). Pass $detail to your view then in your view simply do $detail->name.
If there are two elements in your array wich one do you want to show in your title tag ?
I added the code from my controller function above to assist you.
0

You can achieve this by below methods,

1. Use first() to retrieve single record like below,

// Controller Code
$detail = DB::table('restaurants')->where('id', $restaurant_id)->first();

<!-- View Code -->
<title>{{ $detail->name }}</title>

2. Use find() to retrieve single record by its primary key like below. It works similar to first() but it retrieves data by its primary key.

// Controller Code
$detail = DB::table('restaurants')->find($restaurant_id);

<!-- View Code -->
<title>{{ $detail->name }}</title>

3. You can use get() as you did in your question but you need to access it as array with 0th index. I assumed that your query will retrieve only one record. It won't fail even if it retrieves multiple records but you will only first record in your view.

// Controller Code
$details = DB::table('restaurants')->where('id', $restaurant_id)->get();

<!-- View Code -->
<title>{{ $detail[0]->name }}</title>

There could be some more approaches but I used only these approaches so far.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.