Asked  7 Months ago    Answers:  5   Viewed   37 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?

 Answers

39

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:

<?php
/**
 * <code>
 * {? $old_section = "whatever" ?}
 * </code>
 */
Blade::extend(function($value) {
    return preg_replace('/{?(.+)?}/', '<?php ${1} ?>', $value);
});
Wednesday, March 31, 2021
 
BetaRide
answered 7 Months ago
70

The official Laravel 5.1 documentation states:

Note: Renaming columns in a table with a enum column is not currently supported.

It doesn't matter if you're trying to change another column, if the table contains a enum anywhere it won't work. It's a Doctrine DBAL issue.

As a workaround you could either drop the column and add a new one (column data will be lost):

public function up()
{
    Schema::table('users', function(Blueprint $table)
    {
        $table->dropColumn('name');
    });

    Schema::table('users', function(Blueprint $table)
    {
        $table->text('username');
    });
}

or use a DB statement:

public function up()
{
    DB::statement('ALTER TABLE projects CHANGE slug url VARCHAR(200)');
}

public function down()
{
    DB::statement('ALTER TABLE projects CHANGE url slug VARCHAR(200)');
}

Source: https://github.com/laravel/framework/issues/1186

Wednesday, March 31, 2021
 
RemiX
answered 7 Months ago
92

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.

HttpKernel.php

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

HttpMiddlewareRole.php

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();

    if($user->isAdmin())
        return $next($request);

    foreach($roles as $role) {
        // Check if user has the role This check will depend on how your roles are set up
        if($user->hasRole($role))
            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');
Wednesday, March 31, 2021
 
astaykov
answered 7 Months ago
62

In your other PHP file you would do:

<?php
  $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.

So

File1.php:

<?php
  $fname = "david";
?>

File2.php

<?php
require_once 'File1.php';
echo $fname;

Would result in david being printed to screen.

Wednesday, March 31, 2021
 
Valdas
answered 7 Months ago
32

The best way would to just have two definitions, publishers for all publishers, and active_publishers for those with status = 1:

public function publisher()
{
    return $this->belongsTo("AppUser", "user_id", "id");
}

public function active_publisher()
{
    return $this->publisher()->where('status', 1);
}

Use with $object->active_publisher()->get();

Thursday, August 19, 2021
 
dnoeth
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :