Undfined variable 'posts' - Laravel 5 - laravel

In my ShowController, I returned a view with variable posts, like
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Depress;
use DB;
use App\Http\Requests;
class ShowController extends Controller
{
public function showPost(Request $request)
{
$posts = Depress::all();
return view('homeview')->with('posts', $posts);
}
}
And in my, homeview.blade.php ,
#foreach($posts as $post)
<div class="panel panel-default">
<div class="panel-heading">{{ $post->name }}</div>
<div class="panel-body">
{{ $post->depression }}
</div>
</div>
#endforeach
But, it's showing,
Undefined Variable: posts
Can anyone please help ?

The posts variable is in session. So use Session::get('posts') to get it.
Or use return view('homeview', compact('posts')); and $posts will be available in view as php variable.

add use App\Post; to your ShowController

Related

Showing data from database using Laravel Eloquent Model

So, I'm quite new to Laravel and what I am trying to achieve is to display some string from my data to a page. I'm using Eloquent Model and I can't figure out what I do wrong here. I've attached some of my code below. I hope I make it clear enough.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
class ProductsController extends Controller
{
public function index()
{
$products = Product::all();
return view('products.index')->with('products', $products);
}
Here's where I want the data to be displayed.
#extends ('layouts.app')
#section ('content')
<div class="container-fluid">
<h1>Products</h1>
#if(count($products) > 1 )
#foreach ($products as $product)
<div class="well">
<h3>{{$product->prod_name}}</h3>
</div>
#endforeach
#else
<p>No product found</p>
#endif
</div>
#endsection
UPDATED: The problem is with my loop logic
Since I have only one item inside my database, my loop is supposed to be >= 1 instead of > 1
#if(count($products) >= 1 )
#foreach ($products as $product)
<div class="well">
<h3>{{$product->prod_name}}</h3>
</div>
#endforeach
#else
<p>No product found</p>
#endif
Let's fix up your code and use the correct approach.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
class ProductsController extends Controller
{
public function index()
{
$products = Product::all();
return view('products.index', compact('products'));
}
Notice how I've passed an array via compact as the 2nd parameter of the view function. You can also use view('products.index', ['products' => $products]), I just prefer compact as it is cleaner.
#extends('layouts.app')
#section('content')
<div class="container-fluid">
<h1>Products</h1>
#if(!$products->isEmpty())
#foreach($products as $product)
<div class="well">
<h3>{{ $product->prod_name }}</h3>
</div>
#endforeach
#else
<p>No products found.</p>
#endif
</div>
#endsection
Notice how I've made use of isEmpty, which checks if a collection (obtained here from Eloquent) is empty or not.
Instead of with() maybe View::share can work.
$products = Product::all();
View::share('$products',$products);
return view('products.index');
And dont forget to import View class from Facades. Put this at the begining of page
use Illuminate\Support\Facades\View;

Undefined variable Laravel (strange case)

I am m having this annoying problem. Can anyone give a hand to sort out it? I read all posts and I cannot find the solution.
This is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use DB;
use App\Post;
use App\RegisteredCourse;
class DashboardsController extends Controller
{
public function indexA()
{
return view('dashboards.admin-dashboard');
}
public function indexS()
{
$id = Auth::user()->id;
$courses = DB::table('registered__courses')->select('user_id')->where('user_id', '=', $id)->count();
$posts = Post::all();
return view('dashboards.student-dashboard', compact('id', 'courses', 'posts'));
}
public function indexT()
{
return view('dashboards.teacher-dashboard');
}
}
This is a fragment of the blade view. The name of the view is dashboards.student-dashboard
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">Posts</h3>
</div>
<div class="card-body">
<div class="tab-content">
<div class="active tab-pane" id="activity">
<!-- Post -->
<!--forEACH-->
#foreach ($posts as $post)
<div class="post">
<div class="user-block">
<span class="username">
{{$post->title}} | {{$post->author}}
</span>
<span class="description">{{optional($post->created_at)->format('d-m-Y')}}</span>
</div>
<!-- /.user-block -->
<p>
{{$post->content}}
</p>
</div>
#endforeach
</div>
</div>
</div>
</div>
</div>
And these are the routes
Route::group(['prefix' => 'dashboard',], function () {
Route::get('/admin', 'DashboardsController#indexA')->name('dashboards.indexA');
Route::get('/teacher', 'DashboardsController#indexT')->name('dashboards.indexT');
Route::get('/student', 'DashboardsController#indexS')->name('dashboards.indexS');
});
I tried to pass al the variable to the view and I always have the same problem. ("Undefined variable").It seems like blocked the possibility to pass variable to the blade view.
What I did before:
1-dd($posts) It does not working, it appears the exception "Undefined variable: posts".
2- I remove the whole content of the blade file and I tried with a simple varible and it stills appearing the exception "Undefined variable".
3- I ran php artisan view:clear and restarted the server.
Any sugestions?
Thank you very much.
Instead of
return view('dashboards.student-dashboard', compact('id', 'courses', 'posts'));
try
return view('dashboards.student-dashboard', ['id'=>$id, 'courses'=>$courses, 'posts'=>$posts]);

Laravel reports error "Parameter must be an array or an object that implements Countable"

This is the error:
"count(): Parameter must be an array or an object that implements
Countable (View: C:\xampp\htdocs\blog\resources\views\inc\sidebar.blade.php)"
This is my views/inc/sidebar.blade.php:
#if(count($articles)>0)
#foreach($articles as $article)
{{$article->title}}
#endforeach
#endif
Here is my controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Article;
class HomepageController extends Controller
{
public function sidebar(){
$articles = Article::orderBY('created_at', 'desc')->where('active', '1')->take(2)->get();
return view('inc.sidebar')->with('articles', $articles);
}
}
And on my views/pages/view_article.blade.php
#extends('layouts.layout')
#section('content')
<div class="container">
<div class="row no-gutters" style="margin-top: 10px;margin-left: 5px;margin-right: 5px; min-height: 450px;">
<div class="col-md-9 tex-justify">
<table class="table-responsive">
<tr><td>{{$articles->title}}</td></tr>
<tr><td>{{$articles->created_at->format('M d Y')}}</td></tr>
</table>
<img src="/img/{{$articles->img_article}}">{!!$articles->body!!}</div>
</div>
<div class="col-md-3">
#include('inc.sidebar')
</div>
</div>
</div><br>
#endsection
What am I doing wrong?
The use of orderBY instead of orderBy might be the problem, but if that's not the issue, try replacing:
$articles = Article::orderBY('created_at', 'desc')->where('active', '1')->take(2)->get();
with:
$articles = Article::orderBy('created_at', 'desc')->where('active', '1')->take(2)->get()->all();
The reason behind this is that Illuminate\Database\Query\Builder::get returns an Illuminate\Support\Collection, not an array, whereas Illuminate\Support\Collection::all returns a straight array containing the items in the collection which works with count.
Use #if(isset($articles[0]->id) instead of #if(count($articles)>0)

Retrieving from DB in laravel

I'm trying to retrieve a column from a localhost db using laravel framework and i get this error:
ErrorException in cd582248476d4ab73365f604ce058f390fc48144.php line
10: Undefined variable: Delivery (View:
C:\xampp\htdocs\testt\resources\views\Delivered.blade.php)
#extends('layouts.master')
#section('title')
Pharmacies
#endsection
#section('content')
<section class="row posts">
<div class="col-md-6 col-md-offset-3">
<header><h3>Details of Delivered Orders</h3></header>
#foreach ($Delivery as $Delivery) {
echo $Delivery->Address;
}
#endforeach
}
</div>
</section>
#endsection
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Collection;
class DeliveredController extends Controller
{
public function getDeliveredPage()
{
return view('Delivered');
}
public function index()
{
$Delivery = Delivery::all()->get();
return view('Delivered' ,['Delivery'=>$Delivery]);
}
You don't need to use ->get() after all().
Also, it's better to use a different variable for your collection and your items.
And quick tip for the section in Blade, you can use this:
#section('title', 'Pharmacies')
instead of this
#section('title')
Pharmacies
#endsection
Here's your updated code:
#extends('layouts.master')
#section('title', 'Pharmacies')
#section('content')
<section class="row posts">
<div class="col-md-6 col-md-offset-3">
<header><h3>Details of Delivered Orders</h3></header>
#foreach ($deliveries as $delivery)
{{ $delivery->Address }}
#endforeach
</div>
</section>
#endsection
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Collection;
class DeliveredController extends Controller
{
public function getDeliveredPage()
{
return view('Delivered');
}
public function index()
{
$deliveries = Delivery::all();
return view('Delivered', compact('deliveries'));
}

Undefined variable: names

I am facing difficulties pushing data from the Controller to the View. Below is my code script.
I created my controller ListController using artisan
php artisan make:controller ListController
ListController - show method
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ListController extends Controller
{
public function listNames()
{
$names = array(
'Daenerys Targaryen',
'Jon Snow',
'Arya Stark',
'Melisandre',
'Khal Drogo'
);
return view('welcome', ['names' => $names]);
}
}
Created a view welcome.blade.php (which is default)
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">API DOCUMENTATION</div>
<div class="panel-body">
#foreach ($names as $n)
<p>{{$n}}</p>
#endforeach
</div>
</div>
</div>
</div>
</div>
#endsection
Then, added the appropriate route for it (routes.php)
<?php
Route::get('/', 'ListController#listNames');
When opening localhost:8000 after running php artisan serve, it throws an Exception
ErrorException in 56f6d37e6c39b528e3f5a170141b734befe0f7a2.php line 14:
Undefined variable: names (View: /Applications/MAMP/.../views/welcome.blade.php)
So far I have tried the following:
In the Controller:
return view('welcome', compact('names')); --> DOESN'T WORK
return view('welcome', $names); --> DOESN'T WORK
return view('welcome')->with($names); --> DOESN'T WORK
return view('welcome')->with('names', $names); --> DOESN'T WORK
Hard coding the array in the view and assigning it a variable works
<?php $names = array('John Snow', 'Arya Stark');?>
<?php foreach ($names as $n):?>
<tr>
<td><?php echo $n;?></td>
</tr>
<?php endforeach;?>
I can't seem to detect the problem. Any help is appreciated.
You should pass variables using an array:
return view('welcome', ['names' => $names]);
Or just:
return view('welcome', compact('names'));
To check it do this in the view:
{{ dd($names) }}
First, pass your variables as Alexey said,
return view('welcome', ['names' => $names]);
And then, on the view, do
#foreach ($names as $n)
<p>This is name {{ $n }}</p>
#endforeach
Documentation here: https://laravel.com/docs/master/blade#displaying-data
Managed to resolve the issue.
After spending so much time looking for the bug, I tried to check if laravel version could have something to do with this. The routes.php doesn't work in Laravel v5.3+ and I was using Laravel v5.4.x.
The routes are available in a directory called routes and so I pasted the code from routes.php to routes/web.php and it worked.
<?php
Route::get('/', 'ListController#listNames');

Resources