Getting undefined variable error though I have declared the variable - php

I've made controller, model and view. I've included compact in the controller for the variable but getting the error
Undefined variable: products (View: C:\xampp\htdocs\laravelapps\coffe\resources\views\shop.blade.php),Possible typo $products
Did you mean $errors?)
controller
namespace App\Http\Controllers;
use App\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function getIndex()
{
$products = Product::all();
dd($products);
return view('shop', compact('products'));
}
}
model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['imagePath', 'title', 'description', 'price'];
}
view
#foreach($products->chunk(3) as $productChunk)
<div class="row">
#foreach($productChunk as $product)
<div class="col-md-3">
<div class="menu-entry">
<div class="text text-center pt-4">
<h3>Coffee Capuccino</h3>
<p>A small river named Duden flows by their place and supplies</p>
<p class="price"><span>$5.90</span></p>
<p><a href="http://localhost/laravelapps/coffe/public/cart"
class="btn btn-primary btn-outline-primary">Add to Cart</a></p>
</div>
</div>
</div>
#endforeach
</div>
#endforeach
route
Route::get('/', ['uses' => 'ProductController#getIndex', 'as' => 'product.index']);

Related

Laravel how to slove Undefined variable in included modal

How to slove Undefined variable error exception for variable declared in another controller.
I have OrderController and CustomerController.
In CustomerController i pass $customerTypes[] to view.
Then that view i try to import in antoher view of OrderController and than i get undefended variable. So OrderController does not know about $customerTypes[].
How is possible to slove that dependency?
If i declare $customerTypes[] in OrderController that not be clean code!No point for that var in order.
CustomerController
namespace App\Http\Controllers;
class CustomerController extends BaseController
{
/**
* Index
*/
public function index()
{
$orders = Order::orderBy('created_at', 'desc')->paginate(20)->withQueryString();
$customerTypes = CustomerType::orderBy('name')->get();
return view('customer.index', [
'customerTypes'=> $customerTypes
]);
}
}
/resources/views/customer/create_modal.blade.php
<!-- Modal -->
<div class="modal fade">
#if($customerTypes)
#foreach($customerTypes as $type)
<div class="form-check">
<input type="checkbox" value="{{ $type->id }}">
<label>{{ $type->name }}</label>
</div>
#endforeach
#endif
</div>
OrderController
namespace App\Http\Controllers;
class OrderController extends BaseController
{
/**
* Index
*/
public function index()
{
//....
return view('customer.index', [
'orders'=> $orders
]);
}
}
/resources/views/order/index.blade.php
here i include modal and get error.
#include('customer.create_modal')

Trying to get property 'Url' of non-object (View: D:\XAMPP71\htdocs\website-ikan\resources\views\gizi\listproduct.blade.php)

I have a problem with making model for one to one relationship. this is my code for the page
<div class="col-sm-8 container">
<div class="row">
#foreach ($ikan as $i)
<div class="col-lg-4 col-md-6 col-sm-10 offset-md-0 offset-sm-1" onclick="location.href='/';">
<div class="card"> <div class="container-gambar" style="aspect-ratio: 3/2;"><img class="card-img-top w-100 h-100" style="object-fit: contain" src="{{$i->fotos->Url}}"></div>
<div class="card-body">
<h6 class="font-weight-bold pt-1">{{$i->nama_ikan}}</h6>
<div class="text-muted description">Space for small product description</div>
<div class="d-flex flex-column">
<div class="h6 font-weight-bold">{{$i->hargas->harga}}</div>
</div>
</div>
</div>
</div>
#endforeach
</div>
</div>
This is my code for the models :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\HargaIkan;
class Ikan extends Model
{
use HasFactory;
protected $table = "ikan";
public function fotos(){
return $this->hasOne('App\Models\FotoIkan', 'ikan_id');
}
public function hargas(){
return $this->hasOne(HargaIkan::class, 'ikan_id');
}
}
this is also my code for the other model :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Ikan;
class HargaIkan extends Model
{
use HasFactory;
protected $table = "harga_ikan";
public function ikans(){
return $this->belongsTo(Ikan::class, 'ikan_id');
}
}
and this is the another one :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class FotoIkan extends Model
{
use HasFactory;
protected $table = "foto_ikan";
public function ikans(){
return $this->belongsTo('App\Models\Ikan', 'ikan_id')->withDefault([
'harga' => 'Tidak diketahui',
]);
}
}
and this is my controller :
public function listprodukgizi(){
$ikan = Ikan::all();
return view('gizi.listproduct', ['ikan'=>$ikan]);
}
Do you have any solution for this? i checked my database and the name for the columns is right. I also check the result for the relation of each array and it doesnt have relation.
When you do this
{{$i->fotos->Url}}
you have to code defensively. What if one of $i does not have a foto? You should be prepared for foto to be null
easiest to avoid error is with the null coalesce operator
{{$i->fotos->Url ?? 'NO FOTO'}}
change NO FOTO to whatever you want to do when Ikan does not have foto

Undefined variable: data , $data is undefined. Laravel 8

I got an error says undefined variable data referring to my foreach in my blade
so my errors is :
Undefined variable: data (View: C:\Users\adila\Desktop\onlineshop\onlineshop\resources\views\user\product.blade.php)
so this is my blade file :
<div class="latest-products">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="section-heading">
<h2>Latest Products</h2>
view all products <i class="fa fa-angle-right"></i>
</div>
</div>`enter code here`
#foreach($data as $product)
<div class="col-md-4">
<div class="product-item">
<img src="assets/images/product_06.jpg" alt="">
<div class="down-content">
<h4>{{$product->title}}</h4>
<h6>$ {{$product->harga}}</h6>
<p>{{$product->deskripsi}}</p>
</div>
</div>
</div>
#endforeach
</div>
</div>
</div>
and this is my controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Models\Product;
class HomeController extends Controller
{
public function redirect(){
$usertype = Auth::user()->usertype;
if($usertype=='1'){
return view('admin.home');
}
else{
return view('user.home');
}
}
public function index(){
if(Auth::id()){
return redirect('redirect');
}
else{
$data = Product::all();
return view('user.home',compact('data', $data));
}
}
}
and my Route :
Route::get('/', [HomeController::class, 'index']);
Please help me
Please remove $data from compact() function
return view('user.home',compact('data'));
You passed the variable ‍$data‍ to the ‍‍‍‍view('user.home', compact('data')) but you have an error in the product.blade I think the problem is on the product. blade file. check that file
You error is in the product.blade.php view and in your controller you're returning the home.blade.php view.
You can avoid the Undefined variable error in your product.blade.php adding an isset in your view like this :
#if(isset($data))
#foreach($data as $product)
(...)
#endforeach
#endif

$post->user->name not working (ErrorException Trying to get property 'name' of non-object) || Laravel 8

I was trying to show the name who have posted the post. It was working well. But after a few days, It is showing an error (ErrorException Trying to get property 'name' of non-object). I was searching for a solution for the last few days. And I have found laravel 8 introduced jetstream for authentication purposes. But I have already started with the laravel ui.
I have checked the model of mine. But I could not find anything which could solve the problem. Here are my codes.
view
<!-- Main Content -->
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
#foreach ($posts as $post)
<div class="post-preview">
<a href="{{route('singlePost',$post-> id )}}">
<h2 class="post-title">
{{$post->title}}
</h2>
<h3 class="post-subtitle">
{!!$post->content!!}
</h3>
</a>
<p class="post-meta">Posted by
{{$post->user->name}}
on {{date_format($post->created_at,'F d,Y')}}
|| <i class="fa fa-comment" aria-hidden="true"></i> {{$post->comments->count()}}
</p>
</div>
<hr>
#endforeach
<!-- Pager -->
<div class="clearfix">
<a class="btn btn-primary float-right" href="#">Older Posts →</a>
</div>
</div>
</div>
</div>
<hr>
#endsection
Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
public function user(){
return $this->belongsTo('App\Models\User');
}
public function comments(){
return $this->hasMany('App\Models\PostComments');
}
}
Controller
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request;
class PublicController extends Controller
{
//This is the function which is showing posts
public function index(){
$posts = Post::all();
return view("welcome",compact('posts'));
}
public function contact(){
return view("contact");
}
public function about()
{
return view('about');
}
public function samplePost(Post $post){
return view('samplePost', compact('post'));
}
}

How to view two tables data on the same blade template (view)?

I have a DB with multiple tables. I want to display the DB data on my laravel application. I want to display two tables data on the same page. I have created the model, view, and controller for the app but I am being able to display only one table. I cannot show the other table.
I think I need to define a model with multiple relationships which I am not getting how to do.
My tables are called posts and videos I have nothing on my model. the controller and the view is given below.
Controller
namespace App\Http\Controllers;
use App\Post;
use App\Video;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function index()
{
$posts = Post::all();
return view('landing')->with('posts', $posts);
}
}
View
#extends('layouts.app')
#extends('layouts.navbar')
#section('title')
Landing Page
#endsection
#section('content')
<main class="py-4">
<div class="container">
<div class="row">
<div class="col-md-8">
<h3>Section 1</h3>
#foreach ($posts as $post)
<ul class="list-group">
<li class="list-group-item">
<a href="/posts/{{ $post->id }}">
{{ $post->title }}
{{ $post->brief }}
{{ $post->body }}
{{ $post->cover_image }}
</a>
</li>
</ul>
#endforeach
</div>
<div class="col-md-4">
<h3>Section 2</h3>
#foreach ($videos as $video)
<ul class="list-group">
<li class="list-group-item">
<a href="/videos/{{ $video->id }}">
{{ $video->title }}
</a>
</li>
</ul>
#endforeach
</div>
</div>
</div>
</main>
#endsection
Route on the web.php
<?php
use App\Http\Controllers\PagesController;
use Illuminate\Support\Facades\Route;
Route::get('/', 'PostsController#index');
Route::get('/', 'VideosController#index');
Route::get('/posts/{post}', 'PostsController#show');
Route::resource('posts', 'PostsController');
Route::resource('videos', 'VideosController');
Auth::routes();
Controller for videos data table
<?php
namespace App\Http\Controllers;
use App\Video;
use Illuminate\Http\Request;
class VideosController extends Controller
{
public function index()
{
$videos = Video::all();
return view('landing')->with('videos', $videos);
}
}
On this code I see this problem
If I remove the route for videos on my web.php I could see posts data.
So how could I display both the posts and the videos data at the same time?
Pass both Model together :
<?php
namespace App\Http\Controllers;
use App\Video;
use App\Post;
use Illuminate\Http\Request;
class VideosController extends Controller
{
public function index()
{
$videos = Video::all();
$posts = Post::all();
return view('landing', compact('videos','posts'));
}
}
public function profile($id)
{
$id = Crypt::decrypt($id);
$measurements = DB::select( DB::raw("SELECT * FROM measurements WHERE custom_id = '$id'") );
$customers = DB::table('customers')->where('customer_id', '=', $id)->get();
return view('measurements.profile', compact('measurements','customers'));
}

Categories