Laravel DataTable - $dataTable is undefined - php

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");
}

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')

Class 'App\Http\Controllers\Controller' not found in Laravel 8

Controller
namespace App\Http\Controllers;
use App\Models\Article;
use Illuminate\Http\Request;
class ArticlesController extends Controller
{
public function show($id)
{
$article = Article::find($id);
return view('articles.show', ['article' => $article]);
}
}
Routes
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('/about', function () {
return view('about', [
'articles' => App\Models\Article::take(3)->latest()->get()
]);
});
Route::get('/articles/{article}',
'App\Http\Controllers\ArticlesController#show');
show.blade.php
#extends ('layout')
#section ('content')
<div id="wrapper">
<div id="page" class="container">
<div id="content">
<div class="title">
<h3>{{ $article->title }}</h3>
<p>
<img src="/images/banner.jpg" alt=""
class="image image-full"/>
</p>
<p>{{ $article->body }}</p>
</div>
</div>
#endsection
Error
Class 'App\Http\Controllers\Controller' not found
http://localhost:8000/articles/2
I am not sure where I am going wrong. I have looked at the documentation for routes/controllers in Laravel 8, and it looks like what I have should be working.
It seems there is something wrong with your extended controller.
please check Controller.php existed in App\Http\Controllers or not.
All controller same as yours (ArticlesController) extended from Controller

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'));
}

Undefined variable: sales_chart (Laravel Chart)

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 :(

LARAVEL Eloquent: link post to user then user to profile

I am aiming on retrieving the user image (from profile table column 'profile_img') and display on each post's footer.
I can retrieve the name of the author using $post->author->name and the profile picture using $post->author->profile->profile_image but it only works when i have a single record (post). when there is more than one record i get an error Trying to get property 'profile' of non-object (View: xampp/........../home.blade.php)
Can somebody show me where do i go wrong??
Models:
User
public function post()
{
return $this->hasMany('App\Post');
}
public function profile()
{
return $this->hasOne('App\Profile');
}
Profile
public function user()
{
return $this->belongsTo('App\User');
}
Post
public function author()
{
return $this->belongsTo('App\User', 'id');
}
controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class MainController extends Controller
{
public function index() {
$posts = Post::paginate(9);
return view('pages.home')->with('posts', $posts);
}
home view
#if(count($posts) > 0)
#foreach($posts as$posts)
<div>
<div class="post-title"><h3>{{$post->title}}</h3></div>
<div class="post-description">
{!!mb_substr($post>body,10,rand(35,40)) !!} ....
</div>
<div class="featured-details">
<div class="p-clearfix">
<img class="authorimg"src="/storage/profile_images/{{ $post->author->profile->profile_image }}">
<div class="author-title lite">{{ $post->author->name }}</div>
<div class="lite thumbnail-date">{{ date('M j, Y', strtotime($post->created_at)) }}</div>
</div>
</div>
</div>
<hr>
#endforeach
#else
no post yet
#endif
There appears to be some issues in your home view. Try it again with this code and see.
Home View
#if(count($posts) > 0)
#foreach($posts as $post)
<div>
<div class="post-title"><h3>{{$post->title}}</h3></div>
<div class="post-description">
{!!mb_substr($post>body,10,rand(35,40)) !!} ....
</div>
<div class="featured-details">
<div class="p-clearfix">
<img class="authorimg"src="/storage/profile_images/{{ $post->author->profile->profile_image }}">
<div class="author-title lite">{{ $post->author->name }}</div>
<div class="lite thumbnail-date">{{ date('M j, Y', strtotime($post->created_at)) }}</div>
</div>
</div>
</div>
<hr>
#endforeach
#else
no post yet
#endif
You can even go further and avoid the n+1 problem for authors by running your eloquent model query like this om your controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class MainController extends Controller
{
public function index() {
$posts = Post::with("author")->paginate(9);
return view('pages.home')->with('posts', $posts);
}
I hope this helps.
In your controller index method try with :
public function index() {
$posts = Post::with("author.profile")->paginate(9);
return view('pages.home')->with('posts', $posts);
}
And update post model relationship:
public function author()
{
return $this->belongsTo('App\User', 'user_id'); // update to users table foreign key
}

Categories