Asked  7 Months ago    Answers:  5   Viewed   29 times

Maybe this is a simple thing that I'm missing, but in my laravel blade template I have something like:

{{ Form::model(....) }}
    ... my fields ...
{{ Form::close() }}

This results with escaped HTML so the form tag is actually printed to the screen. However, if I do:

{!! Form::model(....) !!}
    ... my fields ...
{!! Form::close() !!}

it works as expected. Do I always need to use the {!! ... !!} when outputting html? All the tutorials I've read up on just show using the normal convention of {{ Form::model(...) }} to open the form. Thanks for any advice! Using Laravel 5 fwiw.

 Answers

11

That is correct.

Laravel 4

{{ ... }} for raw html
{{{ ... }}} for escaping with htmlentities()

Laravel 5

{!! ... !!} for raw html
{{{ ... }}} for explicitly escaped content
{{ ... }} for the default behavior (which is escaped as well)


If you don't like it you can change all 3 of those tags with these methods:

Blade::setRawTags($openTag, $closeTag);
Blade::setContentTags($openTag, $closeTag);
Blade::setEscapedContentTags($openTag, $closeTag);

To restore the way how Laravel 4 handled things, you can do this:

Blade::setRawTags('{{', '}}');
Blade::setEscapedContentTags('{{{', '}}}');
Wednesday, March 31, 2021
 
Sendy
answered 7 Months ago
33

Recommended approach if you put here only methods (not classes):

  1. Create file anywhere you want
  2. In composer.json make sure you add this file to files key inside autoload like this:

    "autoload": {
        // here other autoload things
    
        "files": ["app/Helpers/AnythingHelper.php"]
    },
    
  3. Run composerdump-autoload`

For classes obviously you should use standard PSR-4 autoloading

Wednesday, March 31, 2021
 
zhartaunik
answered 7 Months ago
85

After re-reading the docs, it is clear that this is intended behaviour. The docs say

As an alternative to mocking, you may use the Event facade's fake method to prevent all event listeners from executing.

As described in a comment, I have to create two tests. One for making sure that the event has been fired and contains the expected data and a second one for making sure that the event listeners get fired.

So, first Test:

Event::fake();

$order = factory(Order::class)->make();

event(new PaymentWasCompleted($order));

Event::assertDispatched(PaymentWasCompleted::class, function ($e) use ($order) {
    return $e->order->id === $order->id;
});

Second test:

Queue::fake();

$order = factory(Order::class)->make();

event(new PaymentWasCompleted($order));

Queue::assertPushed(GenerateInvoiceJob::class, function ($job) use ($order) {
    return $job->order->id === $order->id;
});

As expected, this tests pass.

Saturday, May 29, 2021
 
Amber
answered 5 Months ago
94

Laravel 5 has made this a little harder than before but here's the way to do it. All you will need to do after this is change a value of your .env file and the environment will change

The steps to do this are as follows

  1. Look at your local .env installed by Laravel and change its contents to local or production or whatever else you need

  2. Create 2 files .local.env and .production.env

  3. Add default environment value:

    • In .local.env : APP_ENV=local
    • In .production.env : APP_ENV=production
  4. Create new php file and named it, environment.php, save it into this folder: app/bootstrap/environment.php

    $env = $app->detectEnvironment(function(){
      $environmentPath = __DIR__.'/../.env';
       $setEnv = trim(file_get_contents($environmentPath));
       if (file_exists($environmentPath)){
              putenv("APP_ENV=$setEnv");
              if (getenv('APP_ENV') && file_exists(__DIR__.'/../.' .getenv('APP_ENV') .'.env')) {
                   Dotenv::load(__DIR__ . '/../', '.' . getenv('APP_ENV') . '.env');
              } 
       }
    });
    
  5. Include your environment.php file in bootstrap file. Paste it inside your bootstrap/app.php file.

    require __DIR__.'/environment.php';
    

Yay! You're done.

NOTE: If Laravel can't find a .env file it automatically uses .production.env which makes it awesome for deployments

Credit to http://developers.ph/laravel-framework/laravel-5/how-to-setup-multiple-environment-for-laravel-5-developers-way/

Saturday, May 29, 2021
 
SheppardDigital
answered 5 Months ago
100

If I understand correctly, you'd like to fetch a list of Content objects together with their children Content objects, correct?

Easiest way to do that is to create a parent-child relation in your Eloquent Content model and then use that to load parents with children:

<?php
class Content extends Model {
  public function children() {
    //this defines a relation one-to-many using parent_id field as the foreign key
    return $this->hasMany(Content::class, 'parent_id'); 
  }

  public function parent() {
    return $this->belongsTo(Content::class, 'parent_id'); 
  }

  public function section() {
    return $this->belongsTo(Section::class);
  }
}

Then, if you want to list Content objects their Section together with with their children and their sections, you can fetch the data like that:

$contents = Content::with(['children', 'section', 'children.section'])->whereNull('parent_id')->get();

$contents will contain a collection of all Content objects that have no parent. Each of the objects will have a $content->children attribute that holds a collection of all children Content objects. All children objects will also hold a reference to their parent in $childContent->parent. Both parents and children will have their corresponding section in ->section attribute.

If you wanted to display some Content hierarchy now in your Blade template, you can pass the $contents variable to the view and do the following:

<ul>
@foreach($contents as $content)
  <li>{{$content->title}}</li>
  @if($content->children->count() > 0)
    <ul>
      @foreach($content->children as $childContent)
        <li>{{$childContent->title}}</li>
      @endforeach
   </ul>
  @endif
@endforeach
</ul>  

I noticed that you have a sequence field in your model. I assue that you want content to be sorted by that field. In this case you'll need modify the way you fetch the data:

$contents = Content::with(['children' => function($builder) {
  $builder->orderBy('sequence', 'desc');
}, 'section', 'children.section'])->whereNull('parent_id')->get();
Saturday, May 29, 2021
 
toesslab
answered 5 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 :