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 :)
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
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.
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.
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
Hello fellow Stackoverflow users,
I am currently working on a hobby-project (a Reddit clone) using Laravel.
What I am trying to achieve is to use a similar routing to posts as Reddit does.
In my case I want to navigate to a page using the current subreddit-name and ID of the post (b/subreddit-name/postId) to determine where the website has to route to. I made it work to request the ID of the post, but I couldn't manage to get it done with the current subreddit-name the user clicks on (Subbie).
By the way: Subbie's the name which I am using instead of the term "subreddit", each post has a column stored in the database with the name of the subbie, as well as the ID of the post, so it should be all retrievable.
How can I make it possible so Laravel recognizes the "{subbie}" variable?
(Maybe I am using the wrong words to describe my problem, but I hope it all makes sense.)
Here is the code that I am using:
Router: 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('/', 'IndexController#index');
Route::get('/b/{subbie}/{id}', 'IndexController#post');
Controller: IndexController.php:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Index;
class IndexController extends Controller
{
public function index() {
$posts = Index::all();
return view('index', ['posts' => $posts]);
}
public function post($id) {
$posts = Index::findOrFail($id);
return view('post', ['posts' => $posts]);
}
}
Model: Index.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Index extends Model
{
protected $table = 'posts';
protected $primaryKey = 'id';
}
View: index.blade.php:
#extends('layouts.app')
#section('content')
<div class="container mt-5">
<div class="col-12">
#if(count($posts) > 0)
<ul class="list-group">
#foreach ($posts as $post)
<a href="{!! url()->current() !!}/b/{{ $post->subbie }}/{{ $post->id }}">
<li class="list-group-item" style="text-decoration: none;">b/{{ $post->subbie }} ยท Posted by u/Test</li>
<li class="list-group-item" style="text-decoration: none;"><h5 class="h5">Tekst: {{ $post->body }}</h5></li>
</a>
#endforeach
</ul>
#else
<h1>Helaas zijn er nog geen artikels beschikbaar</h1>
#endif
</div>
</div>
#endsection
View 2: post.blade.php:
#extends('layouts.app')
#section('content')
<ul class="list-group">
<li class="list-group-item">ID: {{ $posts->id}}</li>
</ul>
#endsection
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') }}