This is AdminController.php:
<?php
namespace App\Http\Controllers;
use Response;
use Illuminate\Support\Facades\DB;
use App\Caption;
use App\Image;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
public function admin() {
$images = Image::paginate();
return view('admin',[ '$images' => $images]);
}
}
And this is admin.blade.php:
#extends('template')
#section('title')
Admin Page
#endsection
#section('header')
#endsection
#section('main')
#if (Auth::check())
#foreach ($images as $image)
<p value='{{$image->id}}'>{{$image->content}}</p>
<form action="image/{{$image->id}}/delete" method="post">
<button type="submit">Delete caption</button>
</form>
<form action="image/{{$image->id}}/approve" method="post">
<button type="submit">Accept image</button>
</form>
#endforeach
#else
<p>Login first</p>
#endif
#endsection
#section('footer')
#endsection
Why do I get the following error?
ErrorException in 408148d5409ae6eebf768dc5721bd0d1d9af48af.php line 9:
Undefined variable: images (View: /Users/sahandz/Documents/School/Singapore/CS3226/backend/resources/views/admin.blade.php)
$imagesis clearly defined in my controller.
You have used $images as your variable name when passing the data to the view. This results in blade creating a variable called $$images.
return view('admin',[ 'images' => $images]);
will result in the view creating a variable called $images
Try passing data like this.
$images = Image::paginate();
return view("admin")->with('images',$images);
Basically,you need not use $ in the variable name.
Your code should be like this
public function admin() {
$images = Image::paginate();
return view('admin',[ 'images' => $images]);
}
Why you use [ '$images' => $images]. that is the problem.
You may use this method also to pass your data to view
return view('admin',compact('images'));
Related
PostsController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use App\Models\Post;
class PostsController extends Controller
{
public function index()
{
$posts = Post::orderBy('created_at','desc');
return view('pages.index')->with('posts', $posts);
}
index.blade.php
<h1>Posts</h1>
#if(count($posts) > 0)
#foreach($posts as $post)
<div class="well">
<div class="row">
<div class="col-md-4 col-sm-4">
<img style="width:100%" src="/storage/cover_images/{{$post->cover_image}}">
</div>
<div class="col-md-8 col-sm-8">
<h3>{{$post->title}}</h3>
<small>Written on {{$post->created_at}} by {{$post->user->name}}</small>
</div>
</div>
</div>
#endforeach
#else
<p>No posts found</p>
#endif
when i go to website it pops up error and i do not know what should i do. Actually there is one question with this title which i have but actually it did not help me.
Laravel provide much better solution for this kind of scenario, use forelse
#forelse ($posts as $post)
...
#empty
<p>No posts found</p>
#endforelse
Please the blade document.
You forget to put get() to your query
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use App\Models\Post;
class PostsController extends Controller
{
public function index()
{
$posts = Post::orderBy('created_at','desc')->get();
return view('pages.index')->with('posts', $posts);
}
Check the offical docs for using Count()
https://www.php.net/manual/en/function.count.php
Can you do a "dump" of your "$posts" and see what do you have?
Before using "count" function, i recommend to check the type of your "$posts" or doing something like:
$posts = Post::orderBy('created_at','desc') ?? [];
Or make it more simple by adding a return type on your "orderBy" function like
public static function orderBy(...) : Array {}
Another related issue which can help: https://stackoverflow.com/a/69055096/7186622
I created a Model, Controller, Factory and dataTable files, but laravel keeps showing me the error $dataTable is undefined
How come? Is it a routing issue or I'm not linking the controller to the blade correctly?
My controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\DataTables\DataEntry\ReportsDataTable;
class ReportsController extends Controller
{
public function index(ReportsDataTable $dataTable)
{
return $dataTable->render("pages.dataEntry.reports.index");
}
}
The Route:
// Data Entry pages
Route::prefix('dataEntry')->name('dataEntry.')->group(function () {
Route::resource('reports', ReportsController::class)->only(['index']);
});
index.blade:
<x-base-layout>
<!--begin::Card-->
<div class="card">
<!--begin::Card body-->
<div class="card-body pt-6">
<!--begin::Table-->
{{ $dataTable->table() }}
<!--end::Table-->
{{-- Inject Scripts --}}
{{ $dataTable->scripts() }}
</div>
<!--end::Card body-->
</div>
<!--end::Card-->
</x-base-layout>
public function index()
{
return ReportsDataTable::all()->render("pages.dataEntry.reports.index");
}
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
I'm a beginner in php and in Laravel.
I wanted to add charts to my dashboard. The charts can be displayed on a new separate page but when I include it to my dashboard, it gives me an
error Undefined variable: sales_chart (View:
C:\xampp\htdocs\Laravel\microelephant_updated~\microelephant\resources\views\chart.blade.php)
This is what's in my chart.blade.php
<div class='flex'>
<div class="w-1/2">
{!! $sales_chart-> container() !!}
</div>
{!! $sales_chart-> script() !!}
</div>
My ChartController
namespace App\Http\Controllers;
use App\Invoice;
use Illuminate\Http\Request;
use App\Charts\SalesChart;
class ChartController extends Controller
{
public function index(Request $request)
{
$sales = Invoice::orderBy('created_at')->pluck('total','created_at');
$sales_chart = new SalesChart;
$sales_chart->labels($sales->keys());
$sales_chart->dataset('Sales', 'line', $sales->values());
return view('chart', compact('sales_chart'));
}
}
How I include the chart in my dashboard
<div class="main-content">
#include('chart')
</div>
This is my Route
Route::get('/chart', 'ChartController#index');
There is something wrong with my code but I don't know where it is. Please help :(
i have the images field in Articles table and i will save all images into this field as array : images/image1.jpg,images/image2.jpg, ...
in my index page, i am getting all articles and show them in home page :
$articles = \App\Article::all();
i want to just load first image from every article as cover image and show that but the images field is string. what can i do?
#foreach($articles as $article)
<div class="article_wrapper">
<div class="article_body">
#foreach($article->images as $image)
{{ $image }}
#endforeach
</div>
</div>
#endforeach
Using a route only:
Route::any('/article/images/{id}', ['as' => 'article.image', function($id) {
$article = Article::find($id);
$images = explode(',',$article->images);
return Response::download($images[0]);
}]);
Using a controller:
namespace MyNamespace;
use Illuminate\Routing\Controller;
use App\Article;
use Response;
class ImagesController extends Controller
{
public function show($id)
{
$article = Article::find($id);
$images = explode(',',$article->images);
return Response::download($images[0]);
}
}
Your route:
Route::get('/article/images/{id}', ['as' => 'article.image', 'uses' => 'MyNamespace\ImagesController#show']);
Then in your view:
<img src="{{route('article.image', ['id' => $article->getKey()])}}" />
So that your view will look like:
#foreach($articles as $article)
<div class="article_wrapper">
<div class="article_body">
<img src="{{route('article.image', ['id' => $article->getKey()])}}" />
</div>
</div>
#endforeach
If you aren't saving the fullpath in the images field then prefix it:
return Response::download('C:\downloadpath\' . $images[0]);