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
Related
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'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']);
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'));
}
I have error Laravel Undefined variable: articles . Why is happen ? Please help me
blog/home.blade.php
<div>
#forelse ($articles as $article)
<div>
<img class="news-image" src="{{URL::to('/images').'/'.$article->image_path}}" alt="">
<h1>{{$article->title}}</h1>
<p>{!!$article->description!!}</p>
</div>
#empty
<h2 class="text-center">Not found</h2>
#endforelse
</div>
Route
Route::get('/blog/home', 'BlogController#articlesAll')->name('articles');
BlogController
class BlogController extends Controller
{
public function articlesAll()
{
return view('blog.home', ['articles' => Article::all()]);
}
}
Try
class BlogController extends Controller {
public function articleAll()
{
$articles = Article::get();
return view('blog.home', compact('articles'));
// OR
return view('blog.home')->with('articles',$articles);
}
}
You can use that for multiple variables to view
return view('blog.home')->with(['articles' => $articles]);
Controller
class BlogController extends Controller {
public function articlesAll() {
$articles = Article::get()->toArray();
return view('blog.home',compact('articles'));
}
}
blog/home.blade.php
#if(!empty($articles))
#foreach($articles as $key => $article)
<div>
<img class="news-image" src="{{URL::to('/images').'/'.$article->image_path}}" alt="">
<h1>{{$article->title}}</h1>
<p>{!!$article->description!!}</p>
</div>
#endforeach
#else
<h2 class="text-center">Not found</h2>
#endif
I could. I deduced. The error was that I forgot to remove route
Route::get('/', function () {
return view('blog.home');
});
i remote this and put other route and it work for me
Route::get('/', 'BlogController#articlesAll', function () {
return view('blog.home');
});
am trying to show a simple value stored in session to a view, it is not working correctly
routes.php file
<?php
Route::get('/', 'Test#index');
Route::get('/nnn', 'Test#nnn');
Route::group(['middleware' => ['web']], function () {
});
controller file
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Session;
class Test extends Controller
{
public function index(){
Session::put('name', 'abcdef');
return view('welcome');
}
public function nnn(){
return view('welcome');
}
}
view file:
<body>
<div class="container">
<div class="content">
<div class="title">Laravel 5</div>
#if(Session::has('name'))
{{ Session::get('name') }}
#endif
</div>
</div>
</body>
when i go to localhost:8000 it is showing session value abcdef
but when i go to localhost:8000/nnn it is not showing any value
try to change your routes file to
<?php
Route::group(['middleware' => ['web']], function () {
Route::get('/', 'Test#index');
Route::get('/nnn', 'Test#nnn');
});
in your controller method nnn(){ echo Session, see what you get ? show me the results that you getting the value or not?
public function nnn(){
echo Session::get('name') ;
return view('welcome');
}
also try this in your view
{!! Session::get('name') !!}
instead
{{ Session::get('name') }}