Facade\Ignition\Exceptions\ViewException Error on Laravel? - laravel

0
I reached a part I cannot figure out. When trying to display a product on home page I get the following error:
Undefined variable: products (View: /home/acer/test/project_basket/basket/resources/views/home.blade.php)
For me is the first project in php and i'm not very practice in this language.
home.blade.php:
#section('content')
<div class="card-deck">
/*Problem Here */
#foreach ($products as $product)
<div class="card">
<img src="{{ $product->imagePath }}" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">{{ $product->title }}</h5>
<p class="color">{{ $product->color }}</p>
Buy Now
<button type="button" class="btn btn-primary float-right">Add to Cart</button>
<div class="price">${{ $product->price }}/div>
</div>
</div>
#endsection
Product.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = ['imagePath', 'title', 'price', 'color'];
}
ProductController.php:
<?php
namespace App\Http\Controllers;
use App\Product;
use Illuminate\Http\Request;
//use Illuminate\Http\Request;
class ProductController extends Controller
{
/**
*#return \Illuminate\Http\Response
*/
public function index()
{
$products = Product::inRandomorder()->take(6)->get();
return view('home')->with('products', $products);
}
}
Routes:
//Route::view('/`home`', 'home');
Route::get('/', 'ProductController#index')->name('home');
Auth::routes();
Route::get('/home', 'ProfilesController#index')->name('home');
Route::get('/', 'ProfilesController#index')->name('welcome');
//Route::get('/home', 'DasboardController#index')->name('dashboard');

You hare "hitting" the /home endpoint, which looking at your route is pointing to ProfilesController, but you are working on ProductController so have written the wrong Controller in web.php

Related

Undefined variable $statistic_teachers

I was trying to pass data to my view, but don't why there is a error
my blade which I want to pass data there
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header">{{ __('StatistikAnsicht') }}</div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
<table id="statisticforteacher" class="table-responsive" style="width:100%">
<thead>
<th>FrageTitel</th>
<th>Kapitel</th>
<th>RichtigeRate</th>
</thead>
<tbody>
#foreach($statistic_teachers as $value)
<tr>
<td>{{$value -> question_title}}</td>
<td>{{$value -> chapters_id}}</td>
<td>{{$value -> correct_rate}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
and my controller, and use a connection with model to pass data
<?php
namespace App\Http\Controllers;
use App\Models\StatisticTeacher;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class StatisticTeacherController extends Controller
{
//
public function statisticteacher(){
$statistic_teachers = StatisticTeacher::all();
return view('statisticsA',compact('statistic_teachers'));
}
}
and this is my route, I think it isn't about my route, when I didn't pass data to the view, everythink works.
Route::get('/author_views.statisticsA', 'PagesController#getStatisticsAdmin')->name('statisticsA')->middleware('auth');
Route::get('/statisticsA', 'StatisticTeacherController#statisticteacher');
and this is model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class StatisticTeacher extends Model
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'id','question_title','chapters_id','correct_answer','wrong_ansers','correct_rate'
];
use HasFactory;
public $timestamps = false;
}
I think everything is ok, can not find such a syntax error, but it still doesn't work
can anyone help?
by the way this is stack trace
enter image description here
Please try below approach.
<?php
class StatisticTeacherController extends Controller
{
//
public function statisticteacher(){
$statistic_teachers = StatisticTeacher::all();
return view('statisticsA',['statistic_teachers' => $statistic_teachers]);
}
}
Check on which line in your blade file is throwing error. Compiled vies are stored inside storage\framework\views directory.

Show counter result in dashboard

I would like to create a dasboard with Laravel 8. I want to count all tickets in the database and display the number in the dashboard. Unfortunately it does not work do you have an idea?
Controller Code
namespace App\Http\Controllers;
use App\Models\Ticket;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
public function index()
{
//
$ticketsCount = Ticket::count();
return view('dashboard.index', compact('ticketsCount'));
}
}
View Code
<div class="col-lg-3 col-6">
<!-- small box -->
<div class="small-box bg-info">
<div class="inner">
<h3>{{ $ticketsCount->count() }}</h3>
<p>Open Tickets</p>
</div>
<div class="icon">
<i class="ion ion-bag"></i>
</div>
More info <i class="fas fa-arrow-circle-right"></i>
</div>
</div>
As I can see, your router returns view file, and not getting into controller.
Route::get('/dashboard', function () {
return view('dashboard.index');
});
change your router to (Laravel version before 8)
Route::get('/dashboard', 'DashboardController#index');
After Laravel version 8
use App\Http\Controllers\DashboardController;
Route::get('/dashboard', [DashboardController::class, 'index']);
Docs
Remove ->count() from your view, you already counted it in controller
$ticketsCount = Ticket::count();
The view code:
<div class="col-lg-3 col-6">
<!-- small box -->
<div class="small-box bg-info">
<div class="inner">
<h3>{{ $ticketsCount }}</h3>
<p>Open Tickets</p>
</div>
<div class="icon">
<i class="ion ion-bag"></i>
</div>
More info <i class="fas fa-arrow-circle-right"></i>
</div>
</div>
I have tried to get the number of users displayed on the view. Unfortunately I always get an error message...
Error:
ErrorException
Undefined variable: counter (View: /laravel/resources/views/dashboard/index.blade.php)
Route (web):
Route::get('/dashboard', function () {
return view('dashboard.index');
});
Model (Dashboard)
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Dashboard extends Model
{
use HasFactory;
}
Controller (DashboardController)
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class DashboardController extends Controller
{
public function index()
{
$counter = DB::table('users')->count();
return view('dashboard.index', compact('counter'));
}
}
View (dashboard/index.blade.php):
Users: {{ $counter }}

Call to a member function get() on null for a function in a model

I am trying to have a button of an employee show the text “Already booked” if the variable
count($bookingRequests) returns something and if nothing then show “Book Me’
I get the error above.
I have the following models and their relationships:
User.php and Bookings.php
User:
public function bookings() {
return $this->hasMany('App\Models\Bookings');
}
public function BookingRequest()
{
$this->bookings()->where('booking_status','=','confirmed')->get();
}
Bookings:
public function user(){
return $this->belongsTo('App\Models\User');
}
The view mentioned above is Employeeblock.blade.php:
<div class="row no-gutters mb-5 mb-lg-0" style="padding:10px">
#if(count($bookingRequests))
<a href="#" class="btn btn-primary" data-
toggle="modal" data-target=“#bookingModal">Already Booked</a>
#else
<a href="#" class="btn btn-primary" data-
toggle="modal" data-target="#bookingModal">Book Me</a>
#endif
</div>
Which has data passed onto it by a controller SearchController:
In the following way:
<?php
namespace App\Http\Controllers;
use DB;
use Auth;
use App\Models\Employee;
use App\Models\User;
use App\Models\Bookings;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use Session;
.
.
.
$bookingRequests=Auth::user()->BookingRequest()->get();
return view(‘search.results’)
->with('bookingRequests',$bookingRequests)
results.blade.php above has the following html:
<div class="panel panel-default">
<div class="panel-heading"><h3></h3></div>
<div class="panel-body">
<div class="row">
<div class="col-lg-12">
#foreach($Employees as $Employee)
#include('user/partials/employeeblock')
#endforeach
</div>
</div>
</div>
</div>
</div>
Replace:
$bookingRequests=Auth::user()->BookingRequest()->get();
with
$bookingRequests=Auth::user()->BookingRequest();
Because your BookingRequest() method already use ->get() method to obtain data from db.

Laravel 5.2 controller index returns blank page

I created a route to a simple contact page. I use a controller to save the data in the database and display them on the same page. When I submit the form I get a blank page but I want the user to stay on the contact. I tried to pass the index view but then I get errors.
The files are
route.php
Route::get('contact', 'ContactController#index');
Route::post('contact', 'ContactController#create');
ContactController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Contact;
class ContactController extends Controller
{
public function index() {
$contacts = Contact::orderBy('created_at', 'asc')->get();
return view('/contact', [ 'contacts' => $contacts ]);
}
public function create(Request $request) {
$name = $request->input('name');
$contact = new Contact;
$contact->name = $name;
$contact->save();
#return view('/contact');
}
}
Contact.php
<?php
Namespace App;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
protected $fillable = ['name'];
}
?>
contact.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Contact page</div>
<div class="panel-body">
{!! Form::open(array('url' => 'contact')) !!}
{!! Form::label('name', 'Name') !!}
{!! Form::text('name'); !!}
{!! Form::submit('Submit'); !!}
{!! Form::close() !!}
#if (count($contacts) > 0)
#foreach ($contacts as $contact)
{{ $contact->name }}
#endforeach
#endif
</div>
</div>
</div>
</div>
</div>
#endsection
Try to put contact.blade.php inside views folder and use view('contact', [...]) instead of view('/contact');, you don't need the slash and add return back() to create method:
public function create(Request $request) {
$name = $request->input('name');
$contact = new Contact;
$contact->name = $name;
$contact->save();
return back();
}

Undefined Variable in view, Laravel

I am very new in Laravel. I have made a controller. And there I have declared a variable. I want to pass it to a view. But it says variable undefined.
this is the controller.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class PagesController extends Controller
{
//
public function about(){
$name= 'XYZ';
return view('pages.about')->with('name', $name);
}
}
this is the view
<html>
<body>
<div class="container">
<div class="content">
<div class="title"> About Me: {!! $name !!}} </div>
</div>
</div>
</body>
</html>
Try and return your data to the view like this:
return View::make('pages.about' , array(
'name' => $name
));
Then echo the data in the view with blade like this:
<div class="title"> About Me: {{ $name }} </div>

Resources