Load Blade assets with https in Laravel

I had a problem with asset function when it’s loaded resources through HTTP protocol when the website was using HTTPS, which is caused the “Mixed content” problem. To fix that you need to add \URL::forceScheme(‘https’) into your AppServiceProvider file. So mine looks like this (Laravel 5.4): <?php namespace App\Providers; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider … Read more

Calculate difference between two dates using Carbon and Blade

You are not following the example from the Carbon Documentation. The method Carbon::createFromDate() expects 4 parameters: year, month, day and timezone. And you are trying to pass a formatted date string. If you want to create a Carbon object from a formatted date string you can use the constructor of the class just like this: … Read more

Switch in Laravel 5 – Blade

Updated 2020 Answer Since Laravel 5.5 the @switch is built into the Blade. Use it as shown below. @switch($login_error) @case(1) <span> `E-mail` input is empty!</span> @break @case(2) <span>`Password` input is empty!</span> @break @default <span>Something went wrong, please try again</span> @endswitch Older Answer Unfortunately Laravel Blade does not have switch statement. You can use Laravel if … Read more

Displaying the Error Messages in Laravel after being Redirected from controller

Laravel 4 When the validation fails return back with the validation errors. if($validator->fails()) { return Redirect::back()->withErrors($validator); } You can catch the error on your view using @if($errors->any()) {{ implode(”, $errors->all(‘<div>:message</div>’)) }} @endif UPDATE To display error under each field you can do like this. <input type=”text” name=”firstname”> @if($errors->has(‘firstname’)) <div class=”error”>{{ $errors->first(‘firstname’) }}</div> @endif For better … Read more

Laravel Blade passing variable with string through @include causes error

It’s not a bug but a limitation of blade syntax due to regex. Solution came from github: The problem is using multi-line. You can only use a single line to [pass variables] in Blade, since syntax is limited [by regular expressions] Try the code below and you should be good to go: @include(‘layouts.article’, [‘mainTitle’ => … Read more

Laravel stylesheets and javascript don’t load for non-base routes

For Laravel 4 & 5: <link rel=”stylesheet” href=”https://stackoverflow.com/questions/15232600/{{ URL::asset(“assets/css/bootstrap.min.css’) }}”> URL::asset will link to your project/public/ folder, so chuck your scripts in there. Note: For this, you need to use the “Blade templating engine”. Blade files use the .blade.php extension.

How do I pass a variable to the layout using Laravel’ Blade templating?

If you’re using @extends in your content layout you can use this: @extends(‘master’, [‘title’ => $title]) Note that same as above works with children, like: @include(‘views.subView’, [‘my_variable’ => ‘my-value’]) Usage Then where variable is passed to, use it like: <title>{{ $title ?? ‘Default Title’ }}</title>

Truncate string in Laravel blade templates

In Laravel 4 & 5 (up to 5.7), you can use str_limit, which limits the number of characters in a string. While in Laravel 5.8 up, you can use the Str::limit helper. //For Laravel 4 to Laravel 5.5 {{ str_limit($string, $limit = 150, $end = ‘…’) }} //For Laravel 5.5 upwards {{ \Illuminate\Support\Str::limit($string, 150, $end=’…’) … Read more

Laravel Pagination links not including other GET parameters

I think you should use this code in Laravel version 5+. Also this will work not only with parameter page but also with any other parameter(s): $users->appends(request()->input())->links(); Personally, I try to avoid using Facades as much as I can. Using global helper functions is less code and much elegant. UPDATE: Do not use Input Facade … Read more