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.
Related
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 :)
So I am creating a medical app for a school project. I am fairly new to Laravel and even though I've learnt some things so far in it, I am stuck with creating a Search engine for users to search by disease name and symptom. I used Voyager for my admin panel and Jetstream for scaffolding. Livewire is also present. I have a database table called Diagnoses with columns such as name, description, types, symptoms, causes, treatments, advice. After a user logs in and reaches the dashboard (dashboard.blade.php) I want there to be a Search form where they can search for by name and symptom then display the results like a typical search should do.
Table that I want the results from
Table name = Diagnoses, Model = Diagnose, Controller = DiagnoseController.php
How can I accomplish this? Here are the codes that I think I should be working with:
web.php:
Route::get('/', function () {
return view('main');
});
Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::group(['prefix' => 'admin'], function () {
Voyager::routes();
});
Route::get("/about", function () {
return view('about');
});
Route::get("/contact", function () {
return view('contact');
});
Model
Diagnose.php:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Diagnose extends Model
{
use HasFactory;
}
Controller
DiagnoseController.php:
<?PHP
namespace App\Http\Controllers;
use App\Model\
use Illuminate\Http\Request;
class DiagnoseController extends Controller
{
//
}
Views
dashboard.blade.php:
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Dashboard') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
*Put code here*
</div>
</div>
</div>
</x-app-layout>
Yes, I am a novice so that's why I am seeking help. Also if there is a better way or easier way then please show me. Thanks in advance.
I have a function that shows the department as treeview,it checks for the root node and then checks if this department has child.When i click in a department it should redirect me at another page that shows the users of this department that was clicked.I have tried to write this code and it shows me all the users but i want only those of this department
DepartmentController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Department;
use App\User;
use Illuminate\Support\Facades\DB;
class DepartmentController extends Controller
{
public function usersdep(){
//THIS IS THE ADDED CODE
$users = DB::table('users')
->join('departments', 'users.department', '=', 'departments.id')
->select('users.id','users.lastname','users.name as username','departments.name')->get();
return view('admin.page-users')->with('users', $users);
//ADDED CODE
}
public function treeView(){
$departments = Department::where('parent', '=', 0)->get();
$tree='<ul id="browser" class="filetree">';
foreach ($departments as $department) {
$tree .='<li class="tree-view closed "'.$department->name.''; //first department
if(count($department->childs)) {
$tree .=$this->childView($department);// if this department has children
}
}
$tree .='</ul>';
//return $tree;
return view('admin.page',compact('tree'));
}
public function childView($department){
$html ='<ul>';
foreach ($department->childs as $arr){
if(count($arr->childs))
{
$html .='<li class="tree-view closed">'.$arr->name.'';
$html.= $this->childView($arr);
}
else
{
$html .='<li class="tree-view" >'.$arr->name.'</a>';
$html .="</li>";
}
}
$html .="</ul>";
return $html;
}
page-user.blade.php
#extends ('layouts.master')
#section('title')
Users | Admin
#endsection
#section('content')
<div class="row">
<div class="col-md-12">
<div class="card">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
<div class="card-header">
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<thead class=" text-primary">
<th>Name</th>
<th>Department</th>
</thead>
<tbody>
#foreach($users as $row)
<tr>
<script>console.log($row)</script>
<td>{{ $row->username }}</td>
<td>{{ $row->name}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
#endsection
#section('scripts')
#endsection
web.php
<?php
use App\User;
use App\Department;
use App\Events\WebsocketDemoEvent;
/*
|--------------------------------------------------------------------------
| 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 () {
broadcast(new WebsocketDemoEvent('some data'));
return view('welcome');
});
Route::get('/page', function () {
return view('admin.page');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::group(['middleware' => ['auth','admin']], function () {
Route::get('/role-register','Admin\DashboardController#registered');
Route::delete('/role-delete/{id}', 'Admin\DashboardController#registerdelete');//delete user
Route::post('/save-user', 'Admin\DashboardController#store');
Route::get('/department', 'Admin\DepartmentController#index');
Route::post('/save-department', 'Admin\DepartmentController#store');
Route::get('/department-edit/{id}', 'Admin\DepartmentController#edit');//edit department
Route::put('/department-update/{id}', 'Admin\DepartmentController#update');
Route::delete('/department-delete/{id}', 'Admin\DepartmentController#delete');//delete department
Route::get('/page-users/{id}', 'Admin\DepartmentController#usersdep');//show users
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/chats', 'ChatsController#index');//chats
Route::get('/messages', 'ChatsController#fetchMessages');//messages
Route::post('/messages', 'ChatsController#sendMessage');//messages
Route::get('/dashboard', 'Admin\DashboardController#dbcheck');//DATABASE
Route::get('/user-edit/{id}', 'HomeController#registeredit');
Route::get('/role-edit/{id}', 'Admin\DashboardController#registeredit');//edit user
Route::put('/role-register-update/{id}', 'Admin\DashboardController#registerupdate');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('store_image', 'StoreImageController#index');
Route::post('store_image/insert_image', 'StoreImageController#insert_image');
Route::get('store_image/fetch_image/{id}', 'StoreImageController#fetch_image');
Route::get('/page',array('as'=>'jquery.treeview','uses'=>'Admin\DepartmentController#treeView'));
Route::get('/pageusers', 'Admin\DepartmentController#usersdep');
User.php
public function department()
{
return $this->belongsTo(Department::class);
}
Department.php
public function users()
{
return $this->hasMany(User::class,'department','id');
}
Change you method like this, you need to find department by id passed to route and then get department users. You can also do it using eager loading.
EDITED: You don't have to take id from request, but as route param, so this have to work
public function usersdep($id){
$department = Department::with('users')->find($id);
return view('admin.page-users')->with('users', $department->users);
}
ok so I wanted to fetch a post from my database with my controller and then show it in my view in welcome.blade.php but the message keep showing ErrorException:
Undefined variable: post
I guess the problem is in how I write my code to get the getIndex function in my routes, so I've been trying to change that but I keep getting the same result.
so this is the line in welcome.blade.php:
<!-- Blog item -->
<div class="blog-item">
<div class="blog-thumb">
<img src="asset/img/blog/1.jpg" alt="">
</div>
<div class="blog-text text-box text-white">
<div class="top-meta"><small><i>{{ Carbon\Carbon::parse($post->created_at)->format('d-m-Y') }} by {{ $post->name }}</i></small>/ di Rakitan</div>
#foreach($posts as $post)
<h3 class="blog-post-title">{{ $post->title }}</h3>
<p>{!! \Illuminate\Support\Str::words($post->description, 30, '...') !!}</p>
<blockquote>
<p>
Learn more </p>
</blockquote>
</div><!-- /.blog-post -->
#endforeach
Lanjutkan Baca <img src="asset/img/icons/double-arrow.png" alt="#"/>
</div>
</div>
<!-- Blog item -->
this is the controller PostController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class PostController extends Controller
{
public function getIndex() {
$posts = DB::table('users')->leftjoin('posts', 'users.id', '=', 'posts.author')->paginate(2);
return view('welcome', ['posts' => $posts]);
}
}
and this is the routes file web.php
<?php
/*
|--------------------------------------------------------------------------
| 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('/', 'PostController#getIndex',function () {
return view('welcome')->name('index');
});
Route::get('/', 'PostController#getIndex')->name('index');
Route::get('/2019', 'BlogController#blog');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/author/post','HomeController#getPostForm')->name('post.form');
Route::post('/author/post', 'HomeController#createPost')->name('post.form');
Route::get('/author/post/detail/{id}', 'HomeController#getPost')->name('post.detail');
Route::get('/author/post/edit/{id}', 'HomeController#editPost')->name('post.edit');
Route::post('/author/post/edit/{id}', 'HomeController#updatePost')->name('post.update');
Route::get('/author/post/delete/{id}', 'HomeController#deletePost')->name('post.delete');
You are setting the meta in the HTML before you are defining post in your for loop.
#foreach($posts as $post)
<!--moved meta in here-->
<div class="top-meta"><small><i>{{ Carbon\Carbon::parse($post->created_at)->format('d-m-Y') }} by {{ $post->name }}</i></small>/ di Rakitan</div>
...
#endforeach
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') }}