Blade not working (Laravel), why is nothing shown? - php

Blade not working (Laravel), why is nothing shown?
1.show.blade.php
#extends('book.show')
#section('comment')
Yuup!
#endsection
2.book/show.blade.php
<ul class="cols">
<li>
#yield('comment')
</li>
</ul>

Nothing was wrong with the displayed code. The problem was in your routes. Here is a snippet from your routes:
Route::get('book/{book}', function () {
$comment = 'Hello';
return view('comment.show', ['comment' => $comment]);
});
Route::resource('book', 'BookController');
Route::resource('book') creates the exact same URI as 'book/{book}' so it overrides the first one. In other words, your closure is never triggered. You have several options.
Don't use Route::resource. I like to be explicit with my routes.
Put your Route::get('book/{book} after the Route::resource. This would work too.
Remove Route::get('book/{book}') and just add your code inside your BookController classes show method.
Any of these 3 options will work. I suggest option #3 if you like using Route::resource. Otherwise, I would work with option #1. Option #2 and overriding other routes and such isn't a very nice way of going about things in my opinion.

According to your code in pastebin.
Change return view('comment.show', ['comment' => $comment]); to return view('book.show', ['comment' => $comment]);
EDIT
Another problem is you end your section with #endsection. It will be #stop.
Your code should look like this:
#extends('book.show')
#section('comment')
Yuup!
#stop

Related

Laravel Route not defined breeze

I have a weird situation, where my route seems to be not defined.
I am using Laravel's starter kit Breeze, and I am adding top left navigational links. This is my 4th link already, but it seems that It's the first time I face such issue.
Controller:
public function index(Request $request)
{
$user = $request->user();
$data = $user->campaigns;
return view('subscribes.index', ['data' => $data]);
}
Route:
Route::resource('/my_campaigns', App\Http\Controllers\SubscribeController::class);
Navigational link code:
<div class="hidden space-x-8 sm:-my-px sm:ml-10 sm:flex">
<x-nav-link :href="route('subscribes.index')" :active="request()->routeIs('subscribes.index')">
{{ __('My Campaigns') }}
</x-nav-link>
</div>
Error message:
Route [subscribes.index] not defined.
I have spent 2 hours trying to check every single spot, where the issue may lay, but to no avail. I have already cleared route:cache.
I'd like to note, that the page /my_campaigns opens without any issues, and is working, until I try to add it within the navigational links.
My folders and files seems to be created correct as well, due to the fact, that /my_campaigns is working.
Any idea, where I missed something?
The issue was within my navigational link code. Where the href route is defined, I have used incorrect URL, therefore, this issue was caused.

Link SEO title automatically builder in Laravel

Title atributes in Laravel, how to do it?
Hello, my question to Laravel masters is:
my /routes/web.php looks something like this:
Route::get('/', function () {
return view('index', []);
})->name('index');
Route::get('/services', function () {
return view('services', []);
})->name('services');
Route::get('/contact', function () {
return view('contact', []);
})->name('contact');
And then in the blade template I want to list the links, they are in the navigation bar that is located in the master layout and I yield the content of the site. Sample:
<ul class="nav-menu align-to-right">
<li>Home</li>
<li>Services</li>
<li>Contact</li>
</ul>
Surely you understand that because of SEO I don't want to mention only the name of the company/webpage, but also the subpage. For example, "MyCompany | Our services".
The problem is that these links can also be, for example, in the footer, somewhere in the text on some page, etc. I have to go everywhere and manually change the wording of the title attribute if necessary. For example, on "MyCompany | Our great service".
Well, and especially when I am in the blade template services.blade.php where I define the title of the page, it looks like this:
#section('title', '| Services')
So I just need this title to match the title attribute in a href anywhere on the site.
Perhaps I have explained and described my problem in order. The project runs on Laravel 9.x
I am new to it (but not in PHP), but I still ask you for a specific explanation, solution, which is more detailed, so that it is easier for me to understand this problem and I can learn from it for the future solutions. Thank you

Laravel 9 edit route with {id} parameter returns Not Found (404) despite being defined

Below is an example of the route definition in my routes/web.php file.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\WidgetController;
Route::controller(WidgetController::class)->name('widgets.')->prefix('widgets')->group(function () {
Route::get('{id}/{slug}', 'show')->name('show');
Route::get('create', 'create')->name('create')->middleware('auth');
Route::post('store', 'store')->name('store')->middleware('auth');
Route::get('{id}/edit', 'edit')->name('edit')->middleware('auth');
Route::post('update', 'update')->name('update')->middleware('auth');
});
All the routes listed work fine except for one.
Route::get('{id}/edit', 'edit')->name('edit')->middleware('auth');
For whatever reason, it works completely fine when it's written like this:
Route::get('{id}', 'edit')->name('edit')->middleware('auth');
or this:
Route::get('{id}//edit', 'edit')->name('edit')->middleware('auth');
With "Route::resource", the default would be the first "edit" route. However, I get a 404 when defining it like that. My associated controller method is written like this:
public function edit($id)
{
// Rest of code here
}
Could it be that Laravel is mistaking "edit" as a second parameter? But that doesn't really make much sense considering it's the default for the resource method.
Try putting
Route::get('{id}/edit', 'edit')->nam
Above
Route::get('{id}/{slug}'
Since {slug} could be anything it's more then likely getting executed first. I would suggest putting that last.

Missing required parameters for [Route: homepage.edit] with link in layout

I'm new to Laravel 5.4 and php and I'm having some trouble to do my routing, so I wish you would explain to me what's going on !
I'm developing a back office for my future projects, and for my current project I need to be able to update data sent to the homepage. The edit of the page and the update of the data is now fully functional, I'm just having some trouble to do the link in the layout. Pretty sure it's a trivial problem, yet i'm having trouble figuring it out.
Here is my code :
web.php :
Route::group(['prefix' => 'admin', 'middleware' => 'admin'], function()
{
Route::resource('homepage', 'Admin\HomeController');
});
HomeController :`
public function edit($id) {
$contentExists = Homepage::first();
$homepage = Homepage::findOrFail($id);
return view('admin/homepage/edit', compact('homepage', 'contentExists'));
}`
In the layout :
<ul class="list-unstyled">
<li>
Page d'accueil
</li>
</ul>
Now for that I know I need to pass a parameter to the route, so I did like so :
<ul class="list-unstyled">
<li>
Page d'accueil
</li>
</ul>
But then it says the homepage variable is not defined, and I have no idea where I can define this variable since I'm in the layout, and also have no idea what needs to be inside this variable, what's his purpose... Can you help me with that ?
route second parameter, must be an array like this for example:
route('homepage.edit', ['id' => 2])

Laravel NotFoundHttpException on route

I have been struggling with something very simple here nevertheless the vagueness around Laravel's routing system that complicates its easy routing approach. I have gone through questions listed here but nothing seems to help me so here it goes.
I have formerly defined a route to my controller on an action named "create". This action is suppose to accept a post data from the form and persist it. This create method has one parameter which defaults to null for a project id if its add else we pass an id e.g domain/projects/add/22 to edit and domain/projects/add to create a new one.
Below is the skeleton of the function:
public function create( $id = null ){ ... }
I then defined a route for this which is:
Route::post( 'projects/add', 'ProjectsController#create' );
Inside my form I have {{ Form::open(array('url' => 'projects/add', 'method' => 'post')) }} .
I keep on getting errors related to routing, Http or method not found exceptions. I tried to follow every suggestion on the net but cannot for the life of me find my way.
Please help me point to the right direction, thanks.
try with below sample code
routs.php►
Route::post('projects/add/{id?}',array('as'=>'project_create','uses'=>'ProjectsController#create'));
ProjectsController.php►
class ProjectsController extends BaseController{
public function create( $id = null ){
...
}
projects.blade.php► (for used blade templating your views should have blade.php extention )
<html>....
<form action="{{ route('project_create') }}"method="post">
....
</form>
</html>
Thank you guys for all your responses. After being swamped with work I finally came back to my project and wanted to try out some of your suggestions but I failed.
I did find a tutorial that I tried to follow and basically was able to have my routes working.
Inside the routes I added (see below):
Route::get('/projects/list', [
'as' => 'post.list',
'uses' => 'ProjectsController#listProjects'
]);
Inside my controller I just created a function that is named listProjects(). As for getting my form displayed I followed the same pattern except point to newProject() method in my controller.
As much as I was not keen on this approach I ended up creating another function just for saving my POSTed form data after a new project form has been filled out and submitted. I still used the same url as projects/add except pointing it to a different function in may controller named saveProject().
About the view I just added the as part of the same save route and it worked. below is a link to the tutorial I followed and taking a look at the code.
http://www.codeheaps.com/php-programming/creating-blog-using-laravel-4-part-1/

Categories