Laravel - Invalid argument supplied for foreach()? - laravel

I have this code in my controller:
public function contact(){
$people = ['Michael', 'martin', 'Peter', 'Marian'];
return view('contact', compact('people'));
}
and in my contact.blade.php:
#extends('layouts.app')
#section('content')
<h1>Contact Page</h1>
#if (count($people))
<ul>
#foreach(#people as $person)
<li>{{$person}}</li>
#endforeach
</ul>
#endif
#endsection
#section('footer')
#endsection
I am getting the error:
Invalid argument supplied for foreach() (View: /home/mao/Documents/blog/resources/views/contact.blade.php)
I do not see the error.. been rewriting it twice. this should be correct as far as i can see on online guides?

I think you just have a mistype in your foreach
Try this
#foreach($people as $person)
<li>{{$person}}</li>
#endforeach
Change the # in $

change #people to $people (# => $)
#extends('layouts.app')
#section('content')
<h1>Contact Page</h1>
#if (count($people))
<ul>
#foreach($people as $person)
<li>{{$person}}</li>
#endforeach
</ul>
#endif
#endsection
#section('footer')
#endsection

Related

URL with two parameters

I want to generate URL with two parameters. In my web.php I created a route:
Route::get('/pojedinacni-turnir/{godina}/kolo/{kolo}', [
'uses' => 'Frontend\PojedinacniTurnirController#show',
'as' => 'pojedinacni.turnir',
]);
where are my two parameters are godina and kolo.
In my Controller I create show function with two parameters: id from godina and id from kolo.
Here is code from my controller:
<?php
namespace App\Http\Controllers\Frontend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Sezona;
use App\TurnirPojedinacni;
class PojedinacniTurnirController extends Controller
{
public function show($id_sezona, $id_kolo)
{
$sezone = Sezona::orderBy('godina', 'desc')->get();
$sezona = Sezona::findOrFail($id_sezona);
$kola = TurnirPojedinacni::where('sezona_id', $id_sezona)->orderBy('id', 'asc')->get();
$kolo = TurnirPojedinacni::findOrFail($id_kolo);
return view('frontend.pojedinacni_turnir', compact('sezone', 'sezona', 'kola', 'kolo'))->render();
}
}
When I try to check my URL i get this message:
Missing required parameters for [Route: pojedinacni.turnir] [URI: pojedinacni-turnir/{godina}/kolo/{kolo}]. (View: C:\WebSites\TkPazin\TK_Pazin\resources\views\frontend\pojedinacni_turnir.blade.php)
I don't understand error because i try URL with two parameters. I created URL with id's that exist like this:
{{ route('pojedinacni.turnir', ['godina' => 1, 'kolo' => 1]) }}
and even then i get the same error message.
Update: added blade file
#extends('layouts.frontend')
#section('title', 'TK Pazin | Pojedinačni turnir')
#section('css')
<link href="/css/style_pojedinacni_turniri.css" rel="stylesheet"/>
#endsection
#section('content')
<!-- Page Content -->
<div class="container">
<!-- Page Heading -->
<h1 class="my-4" style="text-align:center; color: #ba3631;">Pojedinačni turniri {{ $sezona->godina }}</h1>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
#foreach($sezone as $sezona)
<li class="breadcrumb-item">{{ $sezona->godina }}</li>
#endforeach
</ol>
</nav>
<div class="row mb-5">
<div class="col-12 col-md-2 mb-5">
<div class="list-group">
#foreach($kola as $kolo)
<button type="button" class="list-group-item list-group-item-action">{{ $kolo->naziv }}</button>
#endforeach
</div>
</div>
</div>
</div>
<!-- /.container -->
#endsection
You blade file shows that you have only added 1 parameter
#foreach($sezone as $sezona)
<li class="breadcrumb-item">{{ $sezona->godina }}</li>
#endforeach
Add the additional parameter
{{ $sezona->godina }}

How to output table data from controller to view?

I'm in the beginning stages of trying to make sense of Laravel and am having trouble displaying the 'illuminate collection object' passed to a blade from a controller.
My print_r is outputting "Illuminate\Support\Collection Object ( [items:protected] => Array ( ) ) 1" which I thought meant that it was seeing one item in the array (just one record in the table currently), but I'm hitting the #else statement so I'm guessing it's actually empty. I'm getting no errors, but I have not been able to display anything from $products despite $title outputting just fine.
public function shop(){
$products = DB::table('products')->get();
$data = array(
'title'=>'Shop',
'products' => $products
);
return view('pages.shop')->with($data);
}
#section('content')
<h1>{{$title}}</h1>
{{ print_r($products) }}
#if($products->count())
<ul class="list-group">
#foreach($products as $product)
<li class="list-group-item">{{$product->title}}</li>
#endforeach
</ul>
#else
<p>No products</p>
#endif
#endsection
Why is my array empty?
On your Controller:
$title = 'Shop';
$products = DB::table('products')->get();
return view('pages.shop', compact('title', 'products');
On your Blade:
I would also suggest to put your unordered list tag <ul> outside of the loop then use #forelse for a cleaner code, like so:
#section('content')
<h1>{{$title}}</h1>
<ul class="list-group">
#forelse($products as $product)
<li class="list-group-item">{{$product->title}}</li>
#empty
<li class="list-group-item">No products</li>
#endforelse
</ul>
#endsection
Try this..........
public function shop()
{
$products = DB::table('products')->get();
$title = "Shop";
return view('pages.shop', compact('products', 'title'));
}
#section('content')
<h1>{{ isset($title) ? $title : '-' }}</h1>
#if($products->count())
<ul class="list-group">
#foreach($products as $product)
<li class="list-group-item">{{ isset($product->title) ? $product->title : '-' }}</li>
#endforeach
</ul>
#else
<p>No products</p>
#endif
#endsection
You can display the data by first passing it as an array to the view
return view('pages.shop')->with('data', $data);
then in the blade
#section('content')
<h1>{{$data['title']}}</h1>
#if(count($data['products']))
<ul class="list-group">
#foreach($data['products'] as $product)
<li class="list-group-item">{{$product->title}}</li>
#endforeach
</ul>
#else
<p>No products</p>
#endif
#endsection
Pass both $products and $title to the view, I prefer to use compact feels cleaner.
$title = 'shop';
$products = DB::table('products')->get();
return view('pages.shop', compact('title', 'products');
Then in your view you can reference them directly. As you are doing now.
#section('content')
<h1>{{$title}}</h1>
#if($products->count())
<ul class="list-group">
#foreach($products as $product)
<li class="list-group-item">{{$product->title}}</li>
#endforeach
</ul>
#else
<p>No products</p>
#endif
#endsection
In your controller
$title = 'shop';
$products = DB::table('products')->get();
return view('pages.shop', compact('title', 'products');
OR
$products = DB::table('products')->get();
$data = array(
'title'=>'Shop',
'products' => $products
);
return view('pages.shop')->with('data',$data);
In your Blade
#section('content')
<h1>{{$title}}</h1>
#if(count($products))
<ul class="list-group">
#foreach($products as $product)
<li class="list-group-item">{{$product->title}}</li>
#endforeach
</ul>
#else
<p>No products</p>
#endif
#endsection

Displaying A Laravel Collection in a blade template

I'm having issues displaying a collection in a blade template.
$comments = Comment::all();
return view('comments/index')->with(compact('comments'));
The code for the blade is:
#isset($comments)
#foreach($comments as $comment)
<div>
<p>
<{{ $comment->commentor }}
</p>
</div>
<hr>
#endforeach
#endisset
#empty($comments)
<div>
<p>There were no comments available.</p>
{{ $comments }}
</div>
#endempty
But not sure how to get the data to render in the template. It just renders a blankpage.
Use this instead :
$comments = Comment::all();
return view('comments.index')->with(compact('comments'));
Use dot notation to reference the view folder structure, view('comments.index'). This represents the file resources/views/comments/index.blade.php. Use this.
#forelse ($comments as $comment)
<div>
<p>
{{ $comment->commentor }}
</p>
</div>
<hr/>
#empty
<div>
<p>There were no comments available.</p>
</div>
#endforelse

NotFoundHttpException in Handler.php line 131:

I know this is probably something really stupid but I just can't seem to fix it.
I'm getting this error
NotFoundHttpException in Handler.php line 131: No query results for model [App\Modules\Menus\Models\Menu].
And can't seem to fix it. At the moment it shouldn't even be asking for the Menu model.
Here is my route.php
Route::get('/signup', [
'uses' => 'OpenController#signup',
'as' => 'signup'
]);
Here is my OpenController.php
<?php
namespace App\Modules\Open\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Modules\Menus\Models\Menu;
use App\Modules\Authors\Models\Story;
class OpenController extends Controller
{
public function index(){
$menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
$menu = Menu::where('id', 1)->orWhere('title', 'home')->firstOrFail();
return view('open::index', compact('menus_child', 'menu'));
}
public function content($id){
$menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
$menu = Menu::where('id', $id)->firstOrFail();
$layout = $menu->type;
$stories = Story::where('type', 'public')->get();
return view('open::public/'.$layout, compact('menus_child', 'menu', 'stories'));
}
public function signup(){
echo "sign up";
die();
}
}
Here is my signup.blade.php
#extends('templates::layouts.public')
#section('content')
<h1>signup blade</h1>
#stop
Here is my public.blade.php layout
<!DOCTYPE html>
<html>
<head>
<title>Website</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://fonts.googleapis.com/css?family=Cherry+Swash|Crafty+Girls|Homemade+Apple|Italianno|Parisienne|Ranga|Rochester" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
{!! Html::style('css/bootstrap.min.css') !!}
{!! Html::style('css/style.css') !!}
{!! Html::script('js/jquery-3.1.1.min.js') !!}
{!! Html::script('js/bootstrap.min.js') !!}
{!! Html::script('js/main.js') !!}
</head>
<body>
<div class="container">
<div id="main">
<div id="header">
<div id="logo">
<div class="row">
<div class="col-lg-6">
<h1>Website</h1>
</div>
<div class="col-lg-6">
<div class="slogan">
{!! Html::link('/signup', 'Signup') !!} / {!! Html::link('/login', 'Login') !!}
</div>
</div>
</div>
</div>
#include('menus::menu')
</div>
<div id="site_content">
<div id="content">
#yield('content')
</div>
</div>
</div>
</div>
</body>
</html>
And this is my menu.blade.php
<div id="menubar">
<ul class="nav navbar-nav" id="menu">
#foreach($menus_child as $grandfather)
#if($grandfather->menu_id)
<li>
#elseif($grandfather->title == 'Home')
<li class="parent {{ menu_active([$grandfather->id]) }}">
#elseif(count($grandfather->menusP()->where('menu_id', '>', 0)->get()))
<li class="dropdown {{ menu_active([$grandfather->id]) }}">
#else
<li class="parent {{ menu_active([$grandfather->id]) }}">
#endif
#if(count($grandfather->menusP()->where('menu_id', '>', 0)->get()))
{!! HTML::decode(HTML::link($grandfather->id, $grandfather->title.'<span class="caret"></span>', array('class' => 'dropdown-toggle')))!!}
#else
{!! HTML::link($grandfather->id, $grandfather->title) !!}
#endif
#if(count($grandfather->menusP))
<ul class="dropdown_menu">
#foreach($grandfather->menusP as $father)
#if($father->menu_id)
<li class="parent_child">
#else
<li>
#endif
{!! HTML::link($father->id, $father->title) !!}
#endforeach
</ul>
#endif
#endforeach
</ul>
</div>
I found out what caused the issue. It was how I had my routes placed. So all I did was put my signup route on top of my routes and that solved my problem
#Gus, #Isis
This post helped me solve a very similar (or possibly same) issue. In my particular case, I immediately realized it was simple user error - and this may help explain why this may happen...
Consider visiting the following URL given the subsequent routes:
GET app.com/team/add-favorite
[bad] Routes:
// Bad Routing Logic
Route::get('/team/{team}', 'TeamController#show');
Route::get('/team/add-favorite', 'UserTeamController#default');
Route::get('/team/add-favorite/{conference}', 'UserTeamController#index');
Obviously, the 2nd route ( /team/add-favorite ) will never be called because it's simply going to route to /team/{team} with the [team] parameter filled with a value of "add-favorite".
My solution was to also simply re-arrange the order of the routes, like so...
[better] Routes:
// Still not good practice, but works
Route::get('/team/add-favorite', 'UserTeamController#default');
Route::get('/team/add-favorite/{conference}', 'UserTeamController#index');
Route::get('/team/{team}', 'TeamController#show');
This is still definitely not best practice, but will at least allow each of the routes to resolve properly.
app.com/team/add-favorite will now match the first route, while something like app.com/team/17 will still fall to the 3rd listed route and be handed to the TeamController with an appropriate ID.
Again - I don't recommend routing this way with "shared" endpoints as it could definitely still cause problems. i.e - better hope there were never a team with and ID = 'add-favorite'. Highly improbable, but you get the point...
Hope this helps!

ErrorException in helpers.php line 519: htmlspecialchars() expects parameter 1 to be string, object given

It says : (View: C:\xampp\htdocs\lar\resources\views\welcome.blade.php)
And my Welcome blade code is:
#if (count($errors) > 0)
<div class="row">
<div class="col-md-6">
<ul>
#foreach($errors -> all() as $error)
<li>{{$errors}}</li>
#endforeach
</ul>
</div>
</div>
#endif
What is wrong here?
It should be $error not $errors inside foreach loop.

Resources