Does a Laravel has an included function to convert a string to an url? Or should I use some standart PHP libraries? But with standard library there is a problem with duplicating url... So there should be a script for every model..?
Example:
'Apple Cake'=>'apple-cake'
If you’re wanting to convert a title-case word to a URL-friendly string, then you can use the Str::slug() method:
use Illuminate\Support\Str;
$slug = Str::slug('Apple Cake'); // apple-cake
I also have found https://github.com/cviebrock/eloquent-sluggable slug maker for Laravel, if someone will have the same problem
Related
Hello to everyone who is seeing this! I am a Laravel beginner, using Laravel for API-s calls. I cannot find an appropriate solution how to pick up parameters from GET method that looks like this:
http://localhost:8000/api/products/searchv2/cat=category1&cat=category2
Now I want to receive in my Laravel controller this array
$categories = ["category1", "category2"]
I think the solution is very easy, but due to minimal experience, I cannot find the right answer on the internet or Laravel documentation. Thanks to everyone who tried to help me!
According to this answer, you can use the [] syntax like you would with form fields to create an array. So in your case, the URL would be:
http://localhost:8000/api/products/searchv2/?cat[]=category1&cat[]=category2
And $_GET['cat'] should return an array, so in theory $request->get('cat') would also return an array.
Using Commas
Another way would be to make use of commas, e.g.:
?cat=category1,category2,category3
You could then use explode:
$categories = explode($request->get('cat'));
We can use json language files like ja.json from Laravel 5.4.
But i have still php language files like validation.php.
Is there a way to put inside json language files to share front end?
I suggest in your controller you do something like:
https://laravel.com/docs/5.4/localization#retrieving-translation-strings
public function convertLangToJson(){
$lang = __('languagefilename');
return response()->json($lang);
}
Is there a way to detect if my code is running from a Middleware. I have a helper that get's called from everywhere for date conversions. In that helper, I check for route names \Route::current()->getName() or if running from console \App::runningInConsole().
When my middleware calls the helper, I get an error with \Route::current()->getName() since \Route::current() is null
Is there a way of knowing if code executed from my middleware?
Thanks
Problem solved
I had problems integrating Cashier and middleware and got that fixed, and to prevent going to my timezonehelper to set dates, I used the protected $dates = ['trial_ends_at', 'ends_at'];
Thanks for your help
You can get full url in laravel using the code below
$fullUrl = Request::capture()->fullUrl();
And You can get current url in laravel using the code below
$currentUrl = Routes::current()->getName();
In this case, you have to use this namespace given below:
use Illuminate\Support\Facades\Route as Routes;
I use $.getJSON() to retrieve some data for a couple of cascading dropdowns in my form. $.getJSON() automatically appends the parameter at the end of the URL like domain.com/controller/method/?parent=5
So, I've declared my method like public function method($parent) which works file, but the same method will be used from other parts of the website that will call it like domain.com/controller/method/5
I tried to create a route in routes.php like the one below:
$route['business/regions/?parent=(:num)'] = 'business/regions/$1';
but it doesn't seem to work. Am I doing something wrong? Maybe ? is confusing the regex parser of the router? Do I have to escape it somehow to make it a 'literal' ? ?
Or is it that router is not used to 'rewrite' get parameters at all? I'm very confused, as it should work but it doesn't and I'm wondering what's wrong with it...
Codeigniter route parameters are for url parameters. It is particularly useful when trying to create a REST styled url pattern.
What you're trying to do is get url query string from the url which is not supported via the Codeigniter router. For you to get what you want you can do the following:
In your routes.php:
$route['business/regions'] = 'business/regions';
and in your controller Business.php:
public function regions() {
//the numeric id you're looking for
$parent = $this->input->get('parent');
}
Can anyone tell me why I get an error saying Trailing Data, whenever I Override this method in my model and alter it in any way?
protected function getDateFormat() {
return 'Y-m-d H:i:s';
}
Any change to the above code breaks my page.
I am following the official documentation here: http://laravel.com/docs/eloquent#timestamps
getDateFormat is purely meant for how date/time fields are stored in the database. You're not meant to modify it for display purposes.
EDIT: Here are some suggestions for custom formatting of your datetime fields if you don't want to call format() everywhere.
Make a presenter class
Add accessor methods like getDateYearAttribute and getDateMonthAttribute
Use Carbon's setToStringFormat method as documented here
Add a HTML macro for formatting dates
Add a blade extension for the same thing
This may not be the same but I would get this error when working with timestamps and carbon but using strtotime() on the data i was passing resolved my issue, may help you.