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') }}
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");
}
web.php
<?php
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('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Auth::routes();
Route::get('/pingu/{id}', [App\Http\Controllers\HomeController::class, 'pingu']);
Auth::routes();
Route::prefix('jobs')->group(function(){
Route::get('create', function () {
return "create";
});
Route::get('update', function () {
return "update";
});
});
TaskController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TaskController extends Controller
{ public function create(){
return view ('create');
}
}
create.blade.php`
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Dashboard') }}</div>
<div class="card-body">
<form action="{{route('jobs.store')}}" method="POST">
<input type="text" name="title" class="form-control">
<button type="submit" class="btn btn-success">Submit</button>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
I did exactly what to do in the turtorial but it dind't appear , please find fix that works. I use laravel 8 i tried to config :cache clear a nd then artisan serve again butdind't work .If you know a laravel enough ( 8 ) you should know that everything is done right .
Picture 1
You are instructing Laravel to return the word 'create' via the closure on your routing in web.php here:
Route::get('create', function () {
return "create";
});
It never gets to the TaskController because it just sees the closure and returns the single word. You will need to give the routing instruction to go to the TaskController in order to get this to work.
See docs on routing Laravel Routing Docs to give you a head start.
It will tell you something similar to this (but please review the docs to ensure this will work for you):
Route::get('create', [App\Http\Controllers\TaskController::class, 'create']);
I don't know your folder structure, but I also suspect that when you get the routing correct, the create() method in that controller may have an issue with finding the file for the view here:
return view ('create');
Make sure you specify any folder if it is not in the views folder directly :)
web.php
<?php
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('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Auth::routes();
Route::get('/pingu/{id}', [App\Http\Controllers\HomeController::class, 'pingu']);
Auth::routes();
Route::prefix('jobs')->group(function(){
Route::get('create', function () {
return "create";
});
Route::get('update', function () {
return "update";
});
});
TaskController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class TaskController extends Controller
{ public function create(){
return view ('create');
}
}
create.blade.php`
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Dashboard') }}</div>
<div class="card-body">
<form action="{{route('jobs.store')}}" method="POST">
<input type="text" name="title" class="form-control">
<button type="submit" class="btn btn-success">Submit</button>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
I did exactly what to do in the turtorial but it dind't appear , please find fix that works. I use laravel 8 i tried to config :cache clear a nd then artisan serve again butdind't work .If you know a laravel enough ( 8 ) you should know that everything is done right .
Picture 1
The issue is with your route.
Route::prefix('jobs')->group(function(){
Route::get('create', function () {
return "create";
});
That will return the word create. You most likely actually want:
Route::prefix('jobs')->group(function() {
Route::get('create', [App\Http\Controllers\TaskController::class, 'create']);
});
The above tells Laravel to use the create function on your TaskController which returns your create view.
You also don't need to include Auth::routes() more than once. Include it before your routes just once.
Update
For the Did not work; here is a working example in black and white for you.
https://phpsandbox.io/n/round-morning-pqpk-dm8j1
Instead of return "create";, I think you meant to return view('create'). Please check and confirm.
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
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');
});