i'm new to Laravel.
I have trouble in showing data to a view page with this error:
ErrorException
Undefined variable $ikus (View: D:\XAMPP\htdocs\SKP-PA-BKT\resources\views\iku.blade.php)
And Laravel give me solution:
$ikus is undefined
Here is code in iku.blade.php :
#extends('layouts.sidebar')
#section('content')
<section class="main-panel">
<div class="container">
<div class="section-title">
</div>
<div class="row">
<div class="col-md-12">
<div class="table-wrap">
<table class="table table-responsive-xl">
<thead>
<tr>
<th>No </th>
<th>Opsi</th>
</tr>
</thead>
<tbody>
<tr class="alert">
#foreach ($ikus as $iku)
<td>{{ $iku->id }}</td>
#endforeach
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</section>
#endsection
Here's code in IkuController.php
<?php
namespace App\Http\Controllers
use App\Models\Iku;
use Illuminate\Http\Request;
class IkuController extends Controller
{
public function getIku()
{
$ikus = Iku::all();
//dd($ikus);
return view('iku', compact('ikus'));
}
}
And here's in web.php
<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\IkuController;
Auth::routes();
Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/indikator-kinerja', [IkuController::class, 'getIku'])->name('iku');
It seems the $ikus variable from IkuController.php file can not be accessed by iku.blade.php file.
I have looked for solutions and most of what i've found is to add code like this:
return view ('layouts.index')->with(['features' => $features]);
or like this one:
return view ('layouts.index', compact('features'));
in IkuController.php file.
I have added it as written above. But i still get the same error. Is there anything left?
Thanks in advance.
This is most likely because your view is cached, you most likely ran the command
php artisan optimize
So just clear your view cache by doing:
php artisan view:clear
For the future, you can always see what is being cached by doing
php artisan about
Related
sorry if the question is kind of newbie. I am new to php and laravel, still trying to learn through tutorial.
I am trying to pass the 'No' in my database to the url, so that the url when I clicked on Daftar, it will show
http://127.0.0.1:8000/search/{No}
Webpage designed
I did try to put it this way in my href tag but did not manage to get the result I want
here is my code
search.blade.php
#if(isset($namelist))
<table class="table table-hover">
<thread>
<tr>
<th>No</th>
<th>Nama</th>
<th>ID</th>
<th>Tindakan</th>
</tr>
</thread>
<tbody>
#if(count($namelist) > 0)
#foreach($namelist as $nama)
<tr>
<td>{{ $nama->No }}</td>
<td>{{ $nama->Name }}</td>
<td>{{ $nama->ID }}</td>
<td>
<a href="search/".$nama[No]>DAFTAR</a>
</td>
</tr>
#endforeach
#else
<tr><td>Tiada rekod ditemui, sila daftar secara manual di kaunter pendaftaran</td></tr>
#endif
</tbody>
</table>
#endif
</div>
</div>
</div>
</body>
</html>
searchController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class searchController extends Controller
{
function search(request $request){
if(isset($_GET['query'])){
$search_text = $_GET['query'];
$namelist = DB::table('namelist')-> where ('ID','LIKE','%'.$search_text.'%')->paginate(100);
return view('search',['namelist'=>$namelist]);
}
elseif(isset($_GET['query'])){
$search_text1 = $_GET['query'];
$namelist = DB::table('namelist')-> where ('No','LIKE','%'.$search_text1.'%')->paginate(100);
return view('search',['namelist'=>$namelist1]);
}
else{
return view('search');
}
}
}
web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\searchController;
use App\Http\Controllers\daftar;
/*
|--------------------------------------------------------------------------
| 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');
});
route::get('/search',[searchController::class, 'search'])->name('web.search');
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Thank you
You have multiple ways to do that. In my opinion, the simplest way would be
DAFTAR.
Actually, what you have already done. Only with the Bladesyntax. And there is a small mistake in your example. Namely, your double quotes. <a href="search/".$nama[No]>DAFTAR</a> should be:
DAFTAR or better DAFTAR.
For the sake of completeness. the most elegant way would be to work with components.
Try this
<td>
DAFTAR
</td>
Enter the variable at given_variable_here above.
Also, you did not prepare the route to accept the passed variable in your web.php. This can be corrected like this:
Route::get('/search/{No}',[searchController::class, 'search'])->name('web.search');
Lastly, I'm not too sure about capitalizing the 'N' in the No you want to use. Should you have problems, start by placing these in lowercase. And if you're using VS Code make sure to add the extensions Laravel Extra Intellisense, Laravel Blade Snippets and Laravel Snippets. They are a great help. Let me know if this helps.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Other resource routes like order and product controller index function is working and showing index page
but when I request for category index page laravel 5.8 says
Method Illuminate\Database\Eloquent\Collection::currentPage does not exist view categories/index.php
Here is the routing group which all other routes are working with except categories controller for showing index page from index function and dashboard for index function
Route::group(['prefix' => 'dashboard', 'namespace' => 'Dashboard', 'middlware' => ['auth:admin']], function () {
Route::name('dashboard.')->group(function () {
Route::resource('/', 'DashboardController');
Route::resource('/products', 'ProductController'); // ->except(['create', 'index']);
Route::resource('/categories', 'categoryController'); // ->except(['create', 'index']);
Route::resource('/orders', 'orderController'); // ->except(['create', 'index']);
});
});
they are working very well but when I configure multi authentication and make admin auth it starts showing these errors
please help if anyone working on laravel5.8
You're calling currentPage property statically from a collection instance instead of a method on a LengthAwarePaginator instance
Your controller must return a paginated query builder instance Illuminate\Pagination\LengthAwarePaginator like
class categoryController extends Controller
{
public function index()
{
$categories = \DB::table('categories')->orderByDesc('id')->paginate(4);
return view('dashboard.categories.index', compact('categories'));
}
}
Then in your dashboard/categories/index view, use the currentPage() as a function
{{ $categories->currentPage() }}
From the docs
PS: Don't just copy-paste this code, it may cause other errors as I just made my best guess to what your controller looks like
zahid hasan emon bro this is my controller
class categorycontroller extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
$data = [];
// $data['categories'] = \DB::table('categories')->get();
$data['categories'] = \DB::table('categories')->orderByDesc('id')->paginate(4);
return view('dashboard.categories.index',$data);
}
and this is my catagory page
#section('page-subtitle','List of all categories')
#section('content')
<div class="box">
<div class="box-header with-border">
<h3 class="box-title"></h3>
New Category
<div class="box-tools pull-right">
{{(($categories->currentPage() * $categories->perPage())- $categories->perPage() )+1 }}
of {{$categories->total()}}
</div>
</div>
<div class="box-body">
<table class="table table-condensed table-striped table-responsive">
<tr>
<td>ID</td>
<td>Name</td>
<td>Action</td>
</tr>
#forelse($categories as $category)
<tr>
<td>{{$category->id}}</td>
<td>{{$category->name}}</td>
<td>
<div class="action-box">
<i class="fa fa-edit"></i>
<i class="fa fa-remove"></i>
</div>
</td>
</tr>
#empty
<tr class="text-center warning">
<td colspan="3">No Record Found</td>
</tr>
#endforelse
</table>
</div>
<!-- /.box-body -->
<div class="box-footer">
#if(count($categories))
<div class="pull-left">
{{$categories->links()}}
</div>
#endif
</div>
<!-- /.box-footer-->
</div>
<!-- /.box -->
#endsection
i think currentpage() method which i used for showing number of pages is making problem ,but before making multi auth is was working very well
I am new to Laravel. I am learning Laravel from tutorial and I drive into one problem which I can't solve.
I think I have problem somewhere into Routing, but I can't find it
Funny thing is that if the href is {{route('tag.create'}}, then it goes to creating page, but when I need to use ID it's not working...
I had same functionality for posts and categories, but everything worked fine for those two. So I really need your help to see what I can't see. I have these files:
index.blade.php:
#extends('layouts.app')
#section('content')
<div class="card">
<div class="card-body">
<table class="table table-hover">
<thead>
<th>
Tag name
</th>
<th>
Delete
</th>
</thead>
<tbody>
#if($tags->count()>0)
#foreach($tags as $tag)
<tr>
<td>
{{$tag->tag}}
</td>
<td>
<i class="fa fa-trash" aria-hidden="true"></i>
</td>
</tr>
#endforeach
#else
<tr>
<th colspan="5" class="text-center">
No tags yet
</th>
</tr>
#endif
</tbody>
</table>
</div>
</div>
#stop
web.php - this is the place where I define routes for tags for TagsController.php:
//Tags
Route::get('/tags',[
'uses'=>'TagsController#index',
'as'=> 'tags'
]);
Route::post('/tag/update/{$id}',[
'uses'=>'TagsController#update',
'as'=> 'tag.update'
]);
Route::get('/tag/create',[
'uses'=>'TagsController#create',
'as'=> 'tag.create'
]);
Route::post('/tag/store',[
'uses'=>'TagsController#store',
'as'=> 'tag.store'
]);
Route::get('/tag/delete/{$id}',[
'uses'=>'TagsController#destroy',
'as'=> 'tag.delete'
]);
TagsController.php - at first I tried to destroy the element, then I tried to return create view(because when I go through /tag/create rout everything works), but neither worked here
public function destroy($id)
{
return view ('admin.tags.create');
/*
Tag::destroy($id);
Session::flash('success', 'Tag deleted succesfully');
return redirect()->back();*/
}
I believe that you should set the route to Route::get('/tag/delete/{id}',[ 'uses'=>'TagsController#destroy', 'as'=> 'tag.delete' ]); because in your case you are telling the route to expect a variable called $id
Please change the parameters in the route setup in web.php from $id to id. I should solve your issue.
Eg: Route::get('/tag/delete/{id}',[
'uses'=>'TagsController#destroy',
'as'=> 'tag.delete'
]);
Thanks !!.
I have been getting this error all day and cant seem to get it right. my goal is basically to update a user in the admin_users table, in order to change their rolls.
this is the Error.
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message Laravel 5.7
Web.php
Route::get('/dashboard', 'DashboardController#dashboard');
Route::post('/admin_users', 'AdminUController#admin_users')->name('admin_users.update');
Route::get('/admin_users', 'AdminUController#admin_users')->name('admin_users');
This is the Controller AdminUController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use Mail;
use Session;
use Gate;
use App\Admin_users;
class AdminUController extends Controller
{
public function admin_users(){
if(!Gate::allows('isUser')){
if(!Gate::allows('isAdmin')){
abort(404,"Sorry, There is nothing to see here!");
}
}
// data from users table to be passed to the view
$admin_users = Admin_users::all();
$title = 'Earn, Invest, Save, and Learn with the Invo App';
return view('admin_users', ['admin_users' => $admin_users])->with('title',$title);
}
public function update(Request $request, Admin_users $admin_users){
$admin_users->email = $request->email;
$admin_users->user_type = $request->user_type;
$admin_users->save();
session()->flash('User account has been updated!');
return redirect()->back();
}
}
This is the Model Admin_users.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Admin_users extends Model
{
protected $guarded=[];
//public function admin_users(){
// }
}
This is the blade template admin_users.blade.php
#extends('layouts.app_dashboard')
#section('content')
<div class="col-md-12">
<div class="margin-top card strpied-tabled-with-hover">
<div class="card-header ">
<h4 class="card-title">Invo Admin Users</h4>
<p class="card-category">Click to edit each user</p>
<a href="{{ route('register') }}" class="btn btn-primary margin-top">
Add New User
</a>
</div>
<div class="card-body table-full-width table-responsive">
<table class="table table-hover table-striped">
<thead>
<tr><th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Password </th>
<th>User Access</th>
</tr></thead>
<tbody>
#foreach ($admin_users as $user)
#if(session()->has('message'))
<div class="alert alert-success">
{{session()->get('message')}}
</div>
#endif
<form method="POST" action="{{route('admin_users.update', $user->id)}}">
#csrf
#method('put')
<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="{{$user->email}}">
</td>
<input type="file" class="custom-file-input" value="" placeholder="{{$user->email}}">
<td>*********</td>
<td>
<select class="selectpicker">
#if(Auth::id() == $user->id)
<option selected value="{{$user->user_type}}">{{$user->user_type}}</option>
<option value="user">user</option>
#else
<option selected value="{{$user->user_type}}">{{$user->user_type}}</option>
<div role="separator" class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Change User Type to</a>
#if($user->user_type == 'admin')
<option value="user">user</option>
#else
<option value="admin">admin</option>
#endif
#endif
</select>
</td>
<td>
<button type="submit" class="btn btn-primary">
Update
</button>
</td>
</tr>
</form>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
#endsection
I can't comment for clarification but is it not because you need to allow the put method through the routes? :)
Route::put($uri, $callback);
from the docs here:
https://laravel.com/docs/5.7/routing
So I believe the answer to be:
Route::put('/admin_users', 'AdminUController#admin_users')->name('admin_users.update');
Put isn't a method I use a massive amount though so I'm talking purely from my experience with Laravel rather than working with put requests themseleves so apologies if I've gone down the wrong path :)
The main reason you're getting this error is because you set your form to submit with a PATCH method and you've set your route to look for a PUT method.
you could also set your route to:
Route::match(['put', 'patch'], '/admin_users/update/{id}','AdminController#update');
Alternatively, you can use php artisan make:controller AdminUController --resource
Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code. For example, you may wish to create a controller that handles all HTTP requests for admin_users
Then your resource route would look something like:
Route::resource('admin_users', 'AdminController');
Is the method "latest" used in Controllers removed in newest version of Laravel?
In PHP Storm I get follow error: Method latest() not found in App/Thread.
public function index()
{
//
$threads = Thread::latest()->get();
return view('threads.index', compact('threads'));
}
I'm following a LaraCasts tutorial, and browsing to said page gives me following error. -> forum.test/threads.
ErrorException (E_ERROR)
Method Illuminate\Database\Query\Builder::path does not exist. (View: D:\xampp\htdocs\forum\resources\views\threads\index.blade.php)
As per requested, my view: it is in resources/views/threads/index.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Forum Threads</div>
<div class="panel-body">
#foreach ($threads as $thread)
<article>
<h4>
<a href="{{ $thread->path() }}">
{{ $thread->title }}
</a>
</h4>
<div class="body">{{ $thread->body }}</div>
</article>
<hr/>
#endforeach
</div>
</div>
</div>
</div>
</div>
#endsection
Also, my routes.
<?php
Route::get('/', function () {
return view('welcome');
});
Route::resource('threads', 'ThreadController');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
The error is not related to the code you posted. Method Illuminate\Database\Query\Builder::path does not exist.. You are calling somewhere path method which does not exist.
To answer your question, method latest() is still present in the (currently) newest version of Laravel 5.6:
https://laravel.com/api/5.6/Illuminate/Database/Query/Builder.html#method_latest
My guess would be you have an incorrect config of the Thread model relationships. Most probably you did not define path() relationship.
See this answer to similar question: https://stackoverflow.com/a/37934093/1885946