laravel pathing in link - laravel

I am using same thing but result is different. In posts.blade.php i can call like this
<a href = "posts/{{post->id}}"edit>Edit</a>
But when i use same thing in rooms.blade.php indisde rooms folder i had to use like this.
<a href = "{{$room->id}}/edit" class = 'btn btn-primary'> Edit</a>
It is really confusing me. Any solution? i want to add link for editing post inside room. I have tried many things but i dont understand.
My Route
Route::get('/','PagesController#index');
Route::get('/about', 'PagesController#about');
Route::get('/services', 'PagesController#services');
Route::get('/register', 'PagesController#register');
Route::get('/logout', 'PagesController#logout');
Route::get('/posts', 'PostsController#posts');
Route::resource('posts','PostsController');
Route::resource('rooms','RoomsController');
Auth::routes();
Route::get('/dashboard', 'DashboardController#index');

You can give the name of the route
Foe example:
Route::get('/post/{id}', 'PostController#showPost')->name('post.show');
And then call the blade
Link to Resource {{ $id }}

Related

Route Not Hitting Controller

When I try to create a view for a product, the URL gets built correctly.
http://localhost:8000/product/my-slug
However, I get a 404 page not found and I have no idea why. It's like the controller is not getting called.
Initiation
<a href="{{ route('product.view', $product->slug) }}">
Route
Route::get('/product/{$slug}', 'ProductsController#view')->name('product.view');
Controller
public function view($slug)
{
$product = Product::find($slug);
return view('products.view', compact('product'));
}
View
<h1>{{ $product->name }}</h1>
EDIT
web.php
Route::get('/', 'ProductsController#index')->name('product.index');
Route::get('/products/create', 'ProductsController#create')->name('product.create');
Route::post('/products', 'ProductsController#store')->name('product.store');
Route::get('/product/{$slug}', 'ProductsController#view')->name('product.view');
/*Route::get('/users', 'UsersController');*/
Route::get('/contact', 'PagesController#contact');
Route::get('/about', 'PagesController#about');
Try changing
Route::get('/product/{$slug}', 'ProductsController#view')->name('product.view');
to
Route::get('/product/{slug}', 'ProductsController#view')->name('product.view');
Ref: Laravel Routing

Custom function in Controller with resource wont work

I have created my own custom function in my RoomsController
public function join($id){
return $id;
}
Then I want to pass variable to it and it says MethodNotAllowedHttpException
And my Form looks like this
{{Form::open(['action'=> ['RoomsController#join', $room->id], 'method' => 'POST' ])}}
{{Form::submit('Join', ['class' => 'btn btn-danger'])}}
{{Form::close()}}
Also have these routes
Route::get('/','PagesController#index');
Route::get('/about', 'PagesController#about');
Route::get('/services', 'PagesController#services');
Route::get('/register', 'PagesController#register');
Route::get('/logout', 'PagesController#logout');
Route::get('/rooms/join', 'RoomsController#join');
Route::resource('posts','PostsController');
Route::resource('rooms','RoomsController');
Auth::routes();
Route::get('/dashboard', 'DashboardController#index');
I have tried in many different ways i dont know why it is not working. All update edit destroy resource functions are working. Thank's for helping :)
You're submitting a POST request but the route is expecting a GET request. If you change your route to Route::post('/rooms/join', 'RoomsController#join'); it should work
change the method to post and put the route below the resource route
Route::resource('rooms','RoomsController');
Route::post('/rooms/join', 'RoomsController#join');

Laravel - passing route parameter to blade

I am using Laravel 5.6, with the default make:auth mechanism.
In the routes/web.php, I would like to add a language middleware as follow:-
Route::prefix('{lang}')->group(function () {
Route::get('password/reset', 'Auth\ForgotPasswordController#showLinkRequestForm')->name('password.request');
});
Now I wish to apply this in blade:-
<a href="{{ route('password.request') }}">
But debugger just say:-
Missing required parameters for [Route: password.request] [URI: {lang}/password/reset].
I believe the blade cannot not get the {lang} from route. How can it be achieved?
Try this
<a href="{{ url('en') }}/password/reset">
When you pass a prefix in the route it means that it is going to be included in the route, so when you say
Route::prefix('{lang}')->group(function () {
Route::get('password/reset' .......
Route::get('foo/bar' .......
it means the route will be like this "http://yourwebsite.com/{lang}/password/reset"
"http://yourwebsite.com/{lang}/foo/bar"
so each time you pass any route that falls in that group will need to have a variable called $lang so the link can be initialized.
so in your case
<a href="{{ route('password.request', ['lang' => 'en']) }}"> //you can change en to your preferred language.
if you want to add a middleware to specific route or route group, it is passed in this way
Route::get('/', function () {
//
})->middleware('first', 'second');
or
Route::group(['middleware' => ['first']], function () {
//
});
if you want to create another language, you can check the below links:
Laravel localization
spatie/laravel-translation-loader

NotFoundHttpException Laravel

I am very new in learning Laravel. I want to fetch data from a database and show it. I can do it. But I want to use the title (fetched from the database) as a link. but then I get a NotFoundHttpException.
Routes
Route::get('articles', 'ArticleController#index');
Route::get('articles/{id}', 'ArticleController#show');
Controller
class ArticleController extends Controller
{
public function index()
{
$articles = Article::all();
return view('articles.index', compact('articles'));
}
public function show($id){
$article = Article::find($id);
return view('articles.show', compact('article'));
}
}
View
#extends('new_welcome')
#section('content')
<h1>Articles</h1>
#foreach($articles as $article)
<article>
<h2>
{{$article->title}}
</h2>
<div class="body">{{ $article->body}}</div>
</article>
#endforeach
#stop
Can someone help me in this case?
Your problem is because of You've "eat" one curly brace (blade engine skips it):
was:
href="{url ('/articles',$article->id)}"
have to be:
href="{{url ('/articles',$article->id)}}"
as You said:
if I click on any single article title then it can not show me the
specific article. But, if I give the URL "homestead.app/articles/2";
so You can see that when You click on link Your browser's address bar becomes:
homestead.app/{url ('/articles',$article->id)}
Because You're beginner so I'll give You advice to not to set direct url in views using url() helper.
Named routes are better if You want to have app that will work properly if in future You decide to change url from: articles to artcls. In this named routes will save You from bulk changing urls in view files.
set name to Your route using 'as' directive that makes Your routing flexible for changes (when You need to change URL so You change only path and keep views unchanged):
Route::get('articles/{id}', ['as' => 'article', 'uses' => 'ArticleController#show']);
Route::get('articles', ['as' => 'articles', 'uses' => 'ArticleController#index']);
change Your view file (find route helper in href):
#extends('new_welcome')
#section('content')
<h1> Articles </h1>
#foreach($articles as $article)
<article>
<h2>
{{$article->title}}
</h2>
<div class="body">{{ $article->body}}</div>
</article>
#endforeach
#stop

laravel 5.1 : how can i use my route in blade?

I want to link to my route in blade. How can i do this?
<a href="roue" > go first</a>
if you define Route name you can use that in your blade :
define Route Name :
Route::get('/admin/transfer/forms-list', [
'as' => 'transfer.formsList',
'uses' => 'Website\TransferController#IndexTransferForms'
]);
now you can use that in your blade like this :
<a href="{{URL::route('transfer.formsList')}}" type="submit">
go first</a>
Of course if you use form collective you can use this :
{!! link_to_route('route.name', 'go first') !!}
There are few ways to use routes in a view.
The simplest one is using route() helper:
go first
If you're using Laravel Collective HTML & Forms, it will create full link, not only path to the route:
{!! link_to_route('route.name', 'go first') !!}
Or:
{!! HTML::linkRoute('mainpage', 'hey') !!}
Using URL facade. Wouldn't recommend, because you're getting redundant code:
go first

Resources