I am trying to create charts on my application. I have the following error when I am trying to pass a variable to a view:
Undefined variable: chart (View:
/Users/ZK/Desktop/project/resources/views/home.blade.php)
I have pasted my controller code below;
<?php
namespace App\Http\Controllers\Admin;
use Charts;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class DashboardController extends Controller
{
public function index()
{
$chart = Charts::new('link', 'highcharts')
->setTitle("My Chart")
->setLabels("A", "B", "C")
->setValues([100, 50, 25])
->setElementLabel(["Total Users"]);
return view('home', [ 'chart' => $chart ]);
}
home.blade.php code;
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
<div class="card">
<div class="card-header">Dashboard</div>
<div class="card-body">
{!! $chart->render() !!}
</div>
</div>
</div>
</div>
</div>
#endsection
Within my layouts.app I have also added the assets for my chart in the header;
{!! Charts::assets() !!}
Can anyone suggest what I might be doing wrong?
Thanks
Related
I'm new with Laravel 8, I have been trying uploading a form with a file field but I keep getting an error "image must be an image", even though I have enctype='multipart/form-data' included in a form.
Blade Template
#extends('layouts.app')
#section('contents')
<div class="container">
<h1> Create a Posts</h1>
{!! Form::open(['action' =>['App\Http\Controllers\PostController#store','enctype'=>'multipart/form-data', 'method'=>'POST']]) !!}
<div class="form-group">
{{Form::label('title', 'Title')}}
{{Form::text('title','',['class'=>'form-control','placeholder'=>'Title'])}}
</div>
<div class="form-group">
{{Form::label('body', 'Body')}}
{{Form::textarea('body','',['class'=>'form-control','id'=>'article-ckeditor','placeholder'=>'Bodytext'])}}
</div>
<br>
<div class="form-group">
<div class="image">
{{Form::file('cover_image')}}
</div>
</div>
<br>
{{Form::submit('Submit',['class'=>'btn btn-primary']);}}
{!! Form::close() !!}
</div>
#endsection
Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
use App\Models\User;
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => ['required', 'unique:posts', 'max:500000'],
'body' => ['required'],
'cover_image'=>['image','nullable','max:19999']
]);
//handle file uploads
if($request->hasfile('cover_image')){
return '123';
}
}
Route
Route::resource('/post', PostController::class);
You need to allow file uploads when you open the form :
#extends('layouts.app')
#section('contents')
<div class="container">
<h1> Create a Posts</h1>
{!! Form::open(['action' =>['App\Http\Controllers\PostController#store','enctype'=>'multipart/form-data', 'method'=>'POST',
// add files true in the form open
'files' => true]]) !!}
<div class="form-group">
{{Form::label('title', 'Title')}}
{{Form::text('title','',['class'=>'form-control','placeholder'=>'Title'])}}
</div>
<div class="form-group">
{{Form::label('body', 'Body')}}
{{Form::textarea('body','',['class'=>'form-control','id'=>'article-ckeditor','placeholder'=>'Bodytext'])}}
</div>
<br>
<div class="form-group">
<div class="image">
{{Form::file('cover_image')}}
</div>
</div>
<br>
{{Form::submit('Submit',['class'=>'btn btn-primary']);}}
{!! Form::close() !!}
</div>
#endsection
see https://laravelcollective.com/docs/6.x/html#opening-a-form
I had a problem while trying out the recently released laravel 8, I'm trying to find out what the changes are and how it works. When I did that I had a problem with the paginate laravel 8 UI getting messy and somehow it happened. Is there anyone who can help me? or have experienced the same thing?
Like this the view I got in laravel 8
Laravel 8 paginate UI
and this is the code I use "Index.blade.php"
#extends('layouts.app')
#section('title', 'Post')
#section('contents')
<div class="container">
<div class="row">
#foreach ($posts as $post)
<div class="col-md-4 mb-4">
<div class="row">
<div class="card mb-4">
<div class="card-header">
{{ $post->title }}
</div>
<div class="card-body">
{{ $post->body }}
</div>
<div class="card-footer">
{{ $post->created_at->diffForHumans() }}
</div>
</div>
</div>
</div>
#endforeach
</div>
<div class="d-felx justify-content-center">
{{ $posts->links() }}
</div>
</div>
#endsection
the code I use for PostController
<?php
namespace App\Http\Controllers;
use App\Models\Posts;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function index()
{
$posts = Posts::latest()->paginate(6);
// dd($post);
return view('post.index', compact('posts'));
}
}
just make sure you have this in your AppServiceProvider.
use Illuminate\Pagination\Paginator;
public function boot()
{
Paginator::useBootstrap();
}
and you're good to go.
I tried doing adding the Paginator::useBootstrap(); in the AppServiceProvider.php but it didn't work.
But this worked:
// Directly in your blade file
$posts->links('pagination::bootstrap-4')
First go to the file app\Providers\AppServiceProvider.php and add:
use Illuminate\Pagination\Paginator;
public function boot()
{
Paginator::useBootstrap();
}
Use this after #endforeach in your post.blade.php:
{{ $blogs->links() }}
Don't use this way:
$posts->links('pagination::bootstrap-4')in AppServiceProvider.php
Right way :
use Illuminate\Pagination\Paginator;
public function boot()
{
Paginator::useBootstrap();
}
Then in any blade, you want to use pagination like this:
{{ $posts->links() }}
i want to create some CMS project with the laravel, when the user trying to click the categories, the app will show the post that only shows the category that user clicked. When the user click the categories, the app says error Bad Method Call. What should i do? Any help will be appreciated.
Here's the code from web.php
<?php
use App\Http\Controllers\Blog\PostsController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', 'WelcomeController#index')->name('welcome');
Route::get('blog/posts/{post}', [PostsController::class, 'show'])->name('blog.show');
Route::get('blog/categories/{category}', [PostsController::class, 'category'])->name('blog.category');
Route::get('blog/tags/{tag}', [PostsController::class, 'tag'])->name('blog.tag');
Auth::routes();
Route::middleware(['auth'])->group(function () {
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('categories', 'CategoriesController');
Route::resource('posts','PostsController');
Route::resource('tags','TagsController');
Route::get('trashed-posts','PostsController#trashed')->name('trashed-posts.index');
Route::put('restore-post/{post}', 'PostsController#restore')->name('restore-posts');
});
Route::middleware(['auth', 'admin'])->group(function () {
Route::get('users/profile', 'UserController#edit')->name('users.edit-profile');
Route::put('users/profile', 'UserController#update')->name('users.update-profile');
Route::get('users','UserController#index')->name('users.index');
Route::post('users/{user}/make-admin', 'UserController#makeAdmin')->name('users.make-admin');
});
Here's the code from PostsController.php
<?php
namespace App\Http\Controllers\Blog;
use App\Http\Controllers\Controller;
use App\Post;
use Illuminate\Http\Request;
use App\Tag;
use App\Category;
class PostsController extends Controller
{
public function show(Post $post) {
return view('blog.show')->with('post', $post);
}
public function category(Category $category) {
return view('blog.category')
->with('category', $category)
->with('posts', $category->posts()->simplePaginate())
->with('categories', Category::all())
->with('tags', Tag::all());
}
public function tag(Tag $tag) {
return view('blog.tag')
->with('tag', $tag)
->with('posts', $tag->posts()->simplePaginate(3));
}
}
Here's the code from App\Category.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = ['name'];
public function post() {
return $this->hasMany(Post::class);
}
}
Here's the code from category.blade.php
#extends('layouts.blog')
#section('title')
Category {{ $category->name }}
#endsection
#section('header')
<header class="header text-center text-white" style="background-image: linear-gradient(-225deg, #5D9FFF 0%, #B8DCFF 48%, #6BBBFF 100%);">
<div class="container">
<div class="row">
<div class="col-md-8 mx-auto">
<h1>{{ $category->name }}</h1>
<p class="lead-2 opacity-90 mt-6">Read and get updated on how we progress</p>
</div>
</div>
</div>
</header><!-- /.header -->
#endsection
#section('content')
<main class="main-content">
<div class="section bg-gray">
<div class="container">
<div class="row">
<div class="col-md-8 col-xl-9">
<div class="row gap-y">
#forelse($posts as $post)
<div class="col-md-6">
<div class="card border hover-shadow-6 mb-6 d-block">
<img class="card-img-top" src="{{ asset($post->image) }}" alt="Card image cap">
<div class="p-6 text-center">
<p>
<a class="small-5 text-lighter text-uppercase ls-2 fw-400" href="#"></a>
{{ $post->category->name }}
</p>
<h5 class="mb-0">
<a class="text-dark" href="{{ route('blog.show',$post->id) }}">
{{ $post->title }}
</a>
</h5>
</div>
</div>
</div>
#empty
<p class="text-center">
No Results found for query <strong>{{ request()->query('search') }} </strong>
</p>
#endforelse
</div>
{{-- <nav class="flexbox mt-30">
<a class="btn btn-white disabled"><i class="ti-arrow-left fs-9 mr-4"></i> Newer</a>
<a class="btn btn-white" href="#">Older <i class="ti-arrow-right fs-9 ml-4"></i></a>
</nav> --}}
{{ $posts->appends(['search' => request()->query('search') ])->links() }}
</div>
#include('partials.sidebar')
</div>
</div>
</div>
</main>
#endsection
If you guys didn't see which file that i don't show up, just ask,.
If you guys see any problems that confusing, i can use teamviewer.
Any Help will be appreciated.
Thank you.
The error told that you don't have posts. Indeed it is. What you have is post() method on Category class, as evidently seen in this line:
public function post() {
You should change this ->with('posts', $category->posts()->simplePaginate()) to be:
public function category(Category $category) {
return view('blog.category')
->with('category', $category)
->with('posts', $category->post()->simplePaginate())
->with('categories', Category::all())
->with('tags', Tag::all());
}
In your model, you defined a post relationship whereas you use a posts relationship then, they're not the same one. You should replace post by posts because it's a to-many relationship.
Your relationship is post and you have multiple ->with('posts', $category->posts()->simplePaginate()). Can you change those to ->with('post') and try again?
I want to pass the username to the url in the browser as in "/profile/{username that's logged in here}". But to test things out i tried to pass id first.
What's wrong with this is not when i press the actual link. The problem occurs when i tried to access just the welcome page.
I've called the route in the view and passed the correct param 'user' to it but it's still giving me an undefined variable error.
here's the route
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/profile/{user}', 'ProfilesController#index')->name('profile.show');
The ProfilesController:
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class ProfilesController extends Controller
{
public function index($user)
{
$user = User::find($user);
return view('home', [
'user' => $user,
]);
}
}
The welcome blade:
<div class="flex-center position-ref full-height">
#if (Route::has('login'))
<div class="top-right links">
#auth
Home
#else
Login
#if (Route::has('register'))
Register
#endif
#endauth
</div>
#endif
<div class="content">
<div class="title m-b-md">
gramClone
</div>
<p class="content">
where IG pics get to be shittier.
</p>
</div>
</div>
the home blade it calls after pressing home link:
#section('content')
<div class="container">
<div class="row">
<div class="col-4 p-5">
<img src="/png/logo.png" alt="logo" style="height: 6rem; width: 6rem;" class="float-right rounded-circle">
</div>
<div class="col-8 pt-5">
<div><h1>{{$user->username}}</h1></div>
<div class="d-flex">
<div class="pr-5"><strong>number here</strong> posts</div>
<div class="pr-5"><strong>number here</strong> followers</div>
<div class="pr-5"><strong>number here</strong> following</div>
</div>
<div class="pt-4 font-weight-bold">Bio Summary Here</div>
<div>Bio Here</div>
<div>Link Here</div>
</div>
</div>
<div class="row pt-5">
<div class="col-4"><img src="https://images.unsplash.com/photo-1566624790190-511a09f6ddbd?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80" alt="" class="w-100"></div>
<div class="col-4"><img src="https://images.unsplash.com/photo-1544127715-bafd09df7c52?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80" alt="" class="w-100"></div>
<div class="col-4"><img src="https://images.unsplash.com/photo-1566592952746-15ea1f1a4133?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=653&q=80" alt="" class="w-100"></div>
</div>
</div>
#endsection
Some of my friends said it's cause i have to put the param at get'/' as well but when i tried it, it gave me the previous error of unpassed param again.
Please help me with this, i've been on overflow for 3 hours already and still got no actual answer to this.
Found out the problem after commenting out my route calling in the welcome blade, apparently it's the #auth making things freaky. After some quick googling i found out that apparently you can't just say route('profile.show', $user) if you have an #auth there.
You need to call it like this: auth()->user().
i'm getting this error while passing data from controller to blade, every things looks good for me but i don't know what i'm missing
ErrorException in 3f66fc6a06d60b9d9b9d521f50ebd1074b04f3bc.php line 228:
Undefined variable: users (View: C:\wamp\www\transport_mithra\resources\views\agents.blade.php)
My controller is
<?php
namespace App\Http\Controllers;
use Response;
use Session;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
class UsersController extends ApiController
{
public function index()
{
$users = User::all();
return View('agents', compact($users));
//return view('agents')->with('users', $users); with this is also same error
}
}
and my agents.blade.php
#foreach ($users as $agent)
<div class="col s12 m6 l3">
<div class="card small card-agent">
<div class="card-image">
<img src="assets/images/profile-image-2.png" alt="">
</div>
<div class="card-content">
<div class="card-details">
<p>Name:</p><span>{{ $agent->name }}</span>
</div>
<div class="card-details">
<p>Phone:</p><span>{{ $agent->phone_number }}</span>
</div>
<div class="card-details">
<p>Email:</p><span>{{ $agent->email }}</span>
</div>
<div class="card-details">
<p>Address:</p><span>{{ $agent->address_city_village }}</span>
</div>
</div>
<div class="card-action">
<p>Drivers: 502</p>
View
</div>
</div>
</div>
#endforeach
looking forward for much needed help
Thank you
You should use correct syntax:
return view('agents', compact('users'));
compact('users') is the same as ['users' => $users]
Also the following will work:
return view('agents')->with(['users' => $users]);