Shorten directive in laravel - laravel

I'm trying to use laravel component and slot.
As I'm using blade templates under components folder, my blade syntax is something like this
#component('components.button',['data'=>$data])
#endcomponent
Here, every times I call it, I have to write the word "component" multiple times.
can I shorten it as below?
#component('button',['data'=>$data])
If yes, how can I achieve it?

You must use of Aliasing Components functionality
If your Blade components are stored in a sub-directory, you may wish to alias them for easier access. For example, imagine a Blade component that is stored at resources/views/components/alert.blade.php. You may use the component method to alias the component from components.alert to alert. Typically, this should be done in the boot method of your AppServiceProvider:
Blade::component('components.alert', 'alert');
Referense

Related

Using multiple Routes and Controllers on single blade file

I have created multiple controller and routes but they are working 1 at a time, I have to disable the other and change the code of my blade file or use different blade file for them but is there an easy way to use it.
The routes are
Route::get('/students/{alphabet}', 'PostController#showByAlphabet');
Route::get('/students/{name}', 'PostController#showByName');
Route::get('/students/{class}', 'PostController#showByClass');
I do not want to create different blade files like
http://example.com/students/alphabet/a
http://example.com/students/name/nadia
http://example.com/students/class/b_com
but like this
http://example.com/students/a
http://example.com/students/nadia
http://example.com/students/b_com
is it possible?
All controllers show different data.
1. Alphabet show list of students starting with same initial.
2. Name shows profile data of the student.
3. Class shows list of students in that subject class.
Since you have a wildcard at the end of your routes the first one will always trigger. So make sure you have individual routes for the controller functions. You can still use the same blade file in the controller.
Route::get('/students/alphabet/{alphabet}', 'PostController#showByAlphabet');
Route::get('/students/name/{name}', 'PostController#showByName');
Route::get('/students/class/{class}', 'PostController#showByClass');
If you have the same route with a parameter there is no way for the router to know if the characters you're sending in are alphabet, name or class.

Is there something similar to Twig filters that I can use in Laravel Blade

Twig has this awesome feature called Twig Filters which allow you to change the variables that you send to your view, from within the view without messing with the data model.
{ variable_name | filter_name }
This makes it super-duper readable and clean. Filters can be lowercase, encoding, raw text or you can build your own.
Question is simple: I really miss this kind of functionality, what's the best way to implement something like this in Laravel using Blade?
as i know Blade don't offer this functionality of filters, but u can create a helper file that can has many functions helper and every function has her logic.
in this link u can find how to create a helper file with functions and call them inside ur blade file tutorial how to create helper function

Using laravel old function within a vuejs component

I have a vuejs component that contains a form, I want to use the laravel old function so I'm able to repopulate the input fields if the form validation fails.
My guess would be I'd need to do something like pass the old function in as a prop to the component possibly but not sure if this is the 'right' way of doing it.
I'm just looking for confirmation on the best way of doing this.
If you are not using ajax requests then yes the only way is to pass them as props. Probably send the whole array as one prop: <component :oldInputs="{{old()}}"></component>

Laravel 5 includes without blade

I would like to include a file on my index page. In php I use something like include('include/header.php');
or in larvel 4 I used include(app_path().'/views/includes/header.php');
My question is...How is it done in Laravel 5 since the whole framework has changed folder structures and what not? It seems that app_path() is not longer valid. Also, I do not want to leverage blade syntax. Does anyone have an idea on this?
Use base_path because the views are not outside off the app directory. Also you can pass the path to the function, no need for string concatenation
include(base_path('resources/views/includes/header.php'));
Try it like this:
include(app_path().'/../resources/views/includes/header.php');

Laravel 4 : Embedding blade template tags

I have saved some template tags in database, i want to process them when they are fetched from database. For instance, i have included this file in content and saved it in DB. When i fetch it just prints out in plain text.
#include('frontend.map')
Is there any way to process it? I know .blade extension is required for the engine to parse but how do we deal when the template tags and variables are saved in DB?
Blade wont support that.
What your asking is to allow Blades commands to be interpreted from supplied variables. It would actually be a massive security risk.
You'll need to have another solution that does not involve the use of Blade function calls inside your variables (database).
Perhaps have your view load a different template based upon the variable that you need?

Resources