Viewed   94 times

I'm reading the Laravel Blade documentation and I can't figure out how to assign variables inside a template for use later. I can't do {{ $old_section = "whatever" }} because that will echo "whatever" and I don't want that.

I understand that I can do <?php $old_section = "whatever"; ?>, but that's not elegant.

Is there a better, elegant way to do that in a Blade template?



It is discouraged to do in a view so there is no blade tag for it. If you do want to do this in your blade view, you can either just open a php tag as you wrote it or register a new blade tag. Just an example:

 * <code>
 * {? $old_section = "whatever" ?}
 * </code>
Blade::extend(function($value) {
    return preg_replace('/{?(.+)?}/', '<?php ${1} ?>', $value);
Friday, September 2, 2022

For more complex variable types like arrays your best bet is to convert it into JSON, echo that in your template and decode it in JavaScript. Like this:

var jobs = JSON.parse("{{ json_encode($jobs) }}");

Note that PHP has to run over this code to make it work. In this case you'd have to put it inside your Blade template. If you have your JavaScript code in one or more separate files (which is good!) you can just add an inline script tag to your template where you pass your variables. (Just make sure that it runs before the rest of your JavaScript code. Usually document.ready is the answer to that)

    var jobs = JSON.parse("{{ json_encode($jobs) }}");

If you don't like the idea of doing it like this I suggest you fetch the data in a separate ajax request.

Wednesday, December 7, 2022

You should't have a separate middleware for each role. It will get very messy very fast. It would be better to have a single role checking middleware that can check against any role passed to it.


protected $routeMiddleware = [
    'role' => AppHttpMiddlewareRole::class,


public function handle($request, Closure $next, ... $roles)
    if (!Auth::check()) // I included this check because you have it, but it really should be part of your 'auth' middleware, most likely added as part of a route group.
        return redirect('login');

    $user = Auth::user();

        return $next($request);

    foreach($roles as $role) {
        // Check if user has the role This check will depend on how your roles are set up
            return $next($request);

    return redirect('login');

Finally in your web routes

Route::get('admin/scholen/overzicht', 'SchoolsController@overview')->middleware('role:editor,approver');
Route::get('admin/scholen/{id}/bewerken', 'SchoolsController@edit')->middleware('role:admin');
Sunday, October 30, 2022

In your other PHP file you would do:

  $fname = "david";

This answers your question directly, but I would hazard a guess that you actually want to have access to variables that are set in a file that is included into your current file or something along those lines.



  $fname = "david";


require_once 'File1.php';
echo $fname;

Would result in david being printed to screen.

Tuesday, November 22, 2022

You don't have to pass anything to blade. If you define your function, you can use it from blade.

  1. Create a new app/helpers.php file.
  2. Add your trim_characters function to it.
  3. Add that file to your composer.json file.
  4. Run composer dump-autoload.

Now just use the function directly in blade:

{{ trim_characters($string) }}
Saturday, August 6, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :