Laravel Undefined variable: books in View - php

The problem is undefined variable in view.
My controller:
use App\Book;
class bookController extends Controller
{
public function index()
{
$books = Book::all();
return view('frontend.index')->with('books', $books);
}
}
I get undefined variable books;
I was searching here and on the web and tried many things and I still get that error.
Not sure why. Can someone help me?
Thank you
EDIT:
View:
#foreach ($books as $key => $book)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $book->title }}</td>
<td>{{ $book->description }}</td>
</tr>
#endforeach

First of all your class name must start with capital letter...
Try this:
return view('frontend.index', ['books' => $books]);
instead of
return view('frontend.index')->with('books', $books);

Try this:
return view('frontend.index')->with(compact($books));

Try this
use App\Book;
class bookController extends Controller
{
public function index()
{
$books = Book::all();
$data['books'] = $books; //You may put other values too in $data array
return view('frontend.index', $data);
}
}

Related

How to solve this in Laravel 6? Trying to get property 'name' of non-object

My Model
class Purchase extends Model
{
public function supplier()
{
return $this->belongsTo(Supplier::class,'supplier_id','id');
}
public function unit()
{
return $this->belongsTo(Unit::class,'unit_id','id');
}
public function category()
{
return $this->belongsTo(Category::class,'category_id','id');
}
public function product()
{
return $this->belongsTo(Product::class,'product_id','id');
}
}
My Controller
class PurchaseController extends Controller
{
public function view()
{
$data['allData'] = Purchase::orderBy('id','desc')->get();
return view('backend.purchase.view-purchases', $data);
}
My View Page
#foreach($allData as $key => $purchase)
<tr>
<td>{{ $loop->index + 1 }}</td>
<td>{{ $purchase->purchase_no }}</td>
<td>{{ $purchase->product->name }}</td>
<td>{{ $purchase->unit->name }}</td>
<td>{{ $purchase->date }}</td>
and 2 more td for edit and delete
#endforeach
But there's still errors in all {{ $purchase-> example}}. I can't find the errors. If I check the dd() method right after the tr tag it works but it doesn't get the {{ $purchase-> example }}. I have the following error message:
Trying to get property 'name' of non-object (View: C:\xampp\htdocs\POS\resources\views\backend\purchase\view-purchases.blade.php).
enter image description here
Try loading the relations in the controller e.g.
$data['allData'] = Purchase::with('supplier', 'unit', 'category', 'product')->orderBy('id','desc')->get();
I would also advise you to change your controller view method body to the following.
$purchases = Purchase::with('supplier', 'unit', 'category', 'product')->orderBy('id','desc')->get();
return view('backend.purchase.view-purchases')->with(['purchases' => $purchases]);
So then in your view try to access it like the following.
#foreach($purchases as $purchase)
...
<td>{{ $purchase->purchase_no }}</td>
...
#endforeach

Call to undefined method Illuminate\Database\Query\Builder::with()

I'm trying to display the name of the category, but I get this error:
BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::with()
AdminController.php
public function gamelist()
{
$categories = Category::all();
$home = DB::table('products')->with(['categories']);
return view('admin.gamelist', ['categories' => $categories, 'home' => $home, 'mode' => 'admin']);
}
Product.php
class Product extends Model
{
public function categories()
{
return $this->belongsTo('App\Category', 'category_id');
}
}
gamelist.blade.php
#foreach ($homes as $home)
<tr>
<td>{{ $home->id }}</td>
<td>{{ $home->name }}</td>
<td>{{ $home->categories->name}}</td>
<td>{{ $home->price }} €</td>
Can someone help me? Thank you
with is used to eager-load Eloquent relationships. By calling DB::table, you decided to use the query builder instead, and this one can't use Eloquent relationships.
You should replace
$home = DB::table('products')->with(['categories']);
by
$home = Product::with('categories')->get();

Laravel - Undefined variable $data on view

I have a problem with this.
Undefined variable: acara (View:
/var/www/html/event_organizer/resources/views/admin/home.blade.php)
But I have declared $acara on the controller like this
Controller
use App\events;
public function lihat_acara()
{
$data['acara'] = events::all();
return view('admin.home')->with($data);
}
and the view like this
home.blade.php
#foreach($acara as $key)
<tbody>
<tr>
<td>{{ $key->nama_acara }}</td>
<td>{{ $key->kategori_acara }}</td>
<td>{{ $key->lokasi_acara }}</td>
</tr>
</tbody>
#endforeach
What's wrong with my code? Any idea? :)
use App\events;
public function lihat_acara()
{
$data['acara'] = events::all();
return view('admin.home', ['acara' => $data['acara'] ]);
}
OR
use App\events;
public function lihat_acara()
{
$data['acara'] = events::all();
return view('admin.home')->with('acara', $data['acara']);
}
OR
use App\events;
public function lihat_acara()
{
$data['acara'] = events::all();
return view('admin.home')->withAcara($data['acara']);
}
From this laravel official site Passing data to view, you can find all the way to send data from controller to view.
You need to change with() it's take an array
Change
with(['data'=>$data]);
or you can do like this also
withData($data);
Now you can receive the variable in view with $data and
in your case it will be
use App\events;
public function lihat_acara()
{
$data['acara'] = events::all();
return view('admin.home')->with(['acara'=>$data['acara']]);
}
Or
use App\events;
public function lihat_acara()
{
$data['acara'] = events::all();
return view('admin.home')->withAcara($data['acara']);
}
In the scenario above, the acara is not a variable you declared acara as an array key. I don't know why you had to do it like this. You can simply write the code like this:
$acara = Event::all();
OR
If you want to keep the controller like that, change the variable in #foreach loop:
#foreach($data['acara'] as $key)
<tbody>
<tr>
<td>{{ $key->nama_acara }}</td>
<td>{{ $key->kategori_acara }}</td>
<td>{{ $key->lokasi_acara }}</td>
</tr>
</tbody>
#endforeach

Trying to get property of non-object when using One to Many relationship in Laravel

So I keep getting this error when trying to get the name of the user who created a post.
My models look like this:
class User extends Authenticatable {
use Notifiable, CanResetPassword;
public function posts() {
return $this->hasMany('App\Post');
}
}
&
class Post extends Model {
public function user() {
return $this->belongsTo('App\User', 'user_id');
}
}
I try to display the name by doing this in my view:
#foreach($posts as $post)
<tr>
<td>{{ $post->user->name }}</td>
</tr>
#endforeach
And my controller looks like this:
public function getAdminIndex() {
$posts = Post::orderBy('id', 'desc')->get();
$comments = Comment::all();
$likes = Like::all();
$users = User::all();
return view('admin.index', ['posts' => $posts, 'comments' => $comments, 'likes' => $likes, 'users' => $users]);
}
Can anyone please tell me what I'm doing wrong?
Thank you very much!
It means not all posts have user, so do something like this:
<td>{{ optional($post->user)->name }}</td>
Or:
<td>{{ empty($post->user) ? 'No user' : $post->user->name }}</td>

Count how many reviews a post has - Laravel 5.2

I need to count how many reviews a post has. How would I go about doing that?
Here is my Listing.php Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Listing extends Model
{
public function reviews()
{
return $this->hasMany('\App\Review');
}
}
Here is my Review.php model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Review extends Model
{
protected $fillable = ['stars','name','comment'];
public function listing()
{
return $this->belongsTo('\App\Listing');
}
}
Here is my method that I'am trying to count in controller
public function mostReviews(Request $request) {
$listings = Review::orderBy('-- most reviews here --')->take(10)->get();
$headline = 'Most Reviewed Listings';
return view('pages.listings', compact('listings', 'headline'));
}
Here is my review table:
I haven't tested it but the query below (using the query builder) should give you what you want - 10 listings with the most reviews. Additionally average_stars column should return average stars rate. You can easily modify the query to get 10 highest rated listings by changing orderBy to average_stars.
$listings = \DB::table('reviews AS r')
->select([
'r.listing_id',
\DB::raw('COUNT(*) AS no_of_reviews'),
\DB::raw('AVG(r.stars) AS average_stars'),
'l.*'])
->join('listings AS l', 'l.id', '=', 'r.listing_id')
->limit(10)
->orderBy('no_of_reviews', 'DESC')
->groupBy('listing_id')
->get();
Please note version up to version 5.2 of Laravel this will return array of stdObject. You can easily access those in your blade template in a similar way as Eloquent Collection.
#foreach($listings as $listing)
<tr>
<td>{{ $listing->id }}</td>
<td>{{ $listing->title }}</td>
<td>{{ $listing->no_of_reviews }}</td>
<td>{{ floor($listing->average_stars) }}</td>
</tr>
#endforeach
This is what I did:
public function mostReviews() {
$reviews = DB::table('reviews')
->select(DB::raw('AVG(stars) as review_score, listing_id'))
->groupBy('listing_id')
->orderBy('review_score', 'desc')
->limit(10)
->get();
foreach($reviews as $key => $review) {
$reviews[$key] = Listing::find($review->listing_id);
}
return view('listings.most-reviews', [
'listings' => $reviews
]);
}
Try this
$review = Review::orderBy('-- most reviews here --');
$reviewCount = $review->count();
$listings=$review->take(10)->get();
The following should work
$listing = Listing::with('reviews')->orderByRaw(function($listing)
{
return $listing->review->count();
}, desc)->take(10)->get();
in your listing model:
public function countReview() {
return count($this->reviews);
}
in your views :
{{$listing->countReview()}}
maybe in controller you can write somthing like :
public function mostReviews() {
Review::orderBy('listing', 'desc')->blahblah; // or 'asc'
}

Categories