I have a resource route generated by Artisan command as
Route::resource('posts','PostsController');
the URI of this route is posts/{post}/edit which require a dynamic value in the middle. But because I'm using the url() method to link all of my routs I am forced to nest the template expression as:
Edit
This is giving me an error: NotFoundHttpException because it couldn't get the {{$show->id}} part. How can I fix this?
In PostsController, create a new variable that you want to show up in the template. You should be able to use the url() function within a controller. (See here) Pass this value to the template like you normally would.
Just in case: documentation on how to pass values to a template
Use Edit with out the use of {{}} notation.
You need double quote and remove one bracket
Do you looking for something like this?
#foreach($posts as $post)
Edit
#endforeach
Related
After reading the documentation, I still only have a vague idea of what Named Routes are in Laravel.
Could you help me understand?
Route::get('user/profile', function () {
//
})->name('profile');
Route::get('user/profile', 'UserProfileController#show')->name('profile');
It says:
Once you have assigned a name to a given route, you may use the route's name when generating URLs or redirects via the global route function
I don't understand what the second part of the sentence means, about generating URLs or redirects.
What would be a generated URL in the case of profile from the above example? How would I use it?
The best resource is right here : https://laravel.com/docs/5.8/routing#named-routes
One of the common use case is in your views. Say your post request goes to a particular route, basically without named routes you can simply go like this to store a task
action="/task"
but say for example you need to update the route to /task/store , you will need to update it everywhere you use the route.
But consider you used a named route
Route::post('/task', 'TaskController#store')->name('task.store');
With named routes you can use the route like this in your view:
action="{{route('task.store')}}"
Now if you choose to update your route, you only need to make the change in the routes file and update it to whatever you need.
Route::post('/task/now/go/here', 'TaskController#store')->name('task.store');
If you need to pass arguments to your routes, you pass it as arguments to route helper like this:
route('task.edit', 1), // in resource specific example it will output /task/1/edit
All of the view examples are given you use blade templating.
After adding a name to a route, you can use the route() helper to create urls.
This can now be used in your application.
For instance, in your blade templates this may look like:
{{ route('profile') }}
This will use the application url and the route path to create a url.
this is how it looks it:
named route sample name('store');:
Route::get('/store-record','YourController#function')->name('store');
store is the named route here. to call it use route('store')
defining another type of route. this is not named route:
Route::get('/store-record','YourController#function')
you can access this route using {{ url('/store-record') }}
hope this helps
I have never understood how to create proper URLs. Every time I end up with trying to figure out if I should do ?var=value or &var=value and then if ?var=value already exists then I end up with ?var=value&var=value.
P.S. I am working with Laravel. (So maybe there is a built-in function?)
For example:
I have pagination and my URL could look like this
www.example.com OR
www.example.com?name=John
Then my pagination link is href="?page=2" and I end up with
www.example.com?name=John?page=2
Then I want to navigate to the next page with href="?page=3" and I end up with this. Because it keeps on adding.
www.example.com?name=John?page=2?page=3
What a mess.... is there a function for PHP or Laravel that would create proper URLS? (knowing when to use ? or & and not add existing values all the time but replace them if they exist already.
If you're using Laravel I think you should use some helper functions:
route('user.profile', ['id' => 1]);
It will create url by route name and parameters, It will look like:
http://sitename/user/profile?id=1
And you can find some useful helper functions here
There should be one and only ? in URL's which separates URI and parameters, rest of the key-value pairs are separated by &, and if you need to pass & or ? as a parameter, then you need to encode them.
you could pass an array to http_build_query and it will build the url for you
and in laravel there are url helper functions you can use them with ease.
Laravel come with a route helper which allow you to generage url quickly by passing the name with which you register your route.
Route::get('/', 'HomeController#index')->name('home.route')
Here you can see I pass home.route to the name method which is the name which I use for this route. And when I want to generate the URL for that route in my view I will juste do
{{ route('home.route') }}
If ny route take some parameters like this
Route::get('/person/{id}', 'HomeController#index')->name('home.route')
In view I will generate the url like this
{{ route('home.route', ['id' =>$id]) }}
Because you want juste to make pagination, Laravel comme with a buil in pagination. When you want to paginate something in your views, you must just call the paginate function on your model and laravel will handle all the route for that. For example I you have a Person Model you can do that like this in your controller
$persons = Person::paginate(25)
And in your views to generate pagination for that you will have to do
{{ $persons->links() }}
And that's all
I am new to VueJS.
I have a link using Laravel Helpers:
{{ $tournament->id }}
As it will be inside a VueJS v-for, now I need to refer to Vue variables that I load with AJAX.
So, I tried to write it like that:
#{{ tournament.id }}
But I get
Parse error: syntax error, unexpected '{' (View: /home/vagrant/Code/resources/views/tournaments/index.blade.php)
Any Idea how to mix Laravel Helper and VueJS?
First of all it looks like you're missing quotes around #{{ tournament.id }}.
But after playing around with it myself it doesn't seem possible at the moment due to routes using the same brackets as placeholders.
A workaround would be to create a simple function somewhere that generates the url with a unique placeholder and afterwards you do a str_replace call on the generated url.
That's the thing I dislike in the helpers and I have completely stopped using them. IMHO the best way to go is to create a route for that (or a Route::controller) and map those urls to a controller. and then use something like:
#{{ tournament.id }}
Your routes could be something like:
Route::controller('tournament','TournamentController');
and your function inside the controller:
function getEdit($tournament_id){
// something something
}
And something like this would work
I used to use smarty a lot and now moved on to Laravel but I'm missing something that was really useful. The modification in the template of you're variable.
Let say I have a variable assign as {{$var}}. Is there a way in Laravel to set it to upper case ? Something like: {{$var|upper}}
I sadly haven't found any documentation on it.
Only first character :
You could use UCFirst, a PHP function
{{ucfirst(trans('text.blabla'))}}
For the doc : http://php.net/manual/en/function.ucfirst.php
Whole word
Str::upper($value)
Also this page might have handy things : http://cheats.jesse-obrien.ca/
PHP Native functions can be used here
{{ strtoupper($currency) }}
{{ ucfirst($interval) }}
Tested OK
You can also create a custom Blade directive within the AppServiceProvider.php
Example:
public function boot() {
Blade::directive('lang_u', function ($s) {
return "<?php echo ucfirst(trans($s)); ?>";
});
}
Don't forget to import Blade at the top of the AppServiceProvider
use Illuminate\Support\Facades\Blade;
Then use it like this within your blade template:
#lang_u('index.section_h2')
Source:
How to capitalize first letter in Laravel Blade
For further information:
https://laravel.com/docs/5.8/blade#extending-blade
works double quoted:
{{ucfirst(trans("$var"))}}
So in one of my views I am generating a redirect()->back()->with() command to send the user back to a previous page.
<button>Go Back</button>
the issue I run into is that it does not provide me with the url. This is what I get.
http://localhost:8888/pp/public/HTTP/1.0%20302%20FoundCache-Control:%20no-cacheDate:%20%20%20%20%20%20%20%20
%20%20Tue,%2024%20Nov%202015%2022:23:12%20GMTLocation:%20%20%20%20%20%20http://localhost:8888/pp/public/task/matching/response/house%3C!DOCTYPE%20html%3E%3Chtml%3E%20%20%20%20%3Chead%3E%20%20%20%20%20%20%20%20%3Cmeta%20charset=%22UTF-8%22%20/%3E%20%20%20%20%20%20%20%20%3Cmeta%20http-equiv=%22refresh%22%20content=%221;url=http://localhost:8888/pp/public/task/matching/response/house%22%20/%3
E%20%20%20%20%20%20%20%20%3Ctitle%3ERedirecting%20to%20http://localhost:8888/pp/public/task/matching/response/house%3C/title%3E%20%20%20%20%3C/head%3E%20%20%20%20%3Cbody%3E%20%20%20%20%20%20%20%20Redirecting%20to%20%3Ca%20href=%22http://localhost:8888/pp/public/task/matching/response/house%22%3Ehttp://localhost:8888/pp/public/task/matching/response/house%3C/a%3E.%20%20%20%20%3C/body%3E%3C/html%3E
So I'm thinking I am using the function wrong but how can I simply get a back() url to work?
What you probably want to use is URL::previous().
Go Back
The redirect()->back() method actually calls the URL::previous() method to create the RedirectResponse.
redirect() does generate a URL. It returns an instance of Illuminate\Routing\Redirector. It's meant for use within a Controller Method or a Route Closure.
You want the route() helper method
$url = route('routeName');
Actually redirect() cannot be used after printing any output, it will not work.
If you want the link to go back. Just use simple JS script, as described here.
Try using
{{ Request::referrer() }}
you can achieve the same by using just PHP (but I prefer the Laravel's syntax since it's more readable and clean :
$_SERVER["HTTP_REFERER"]