Using trim on blade - Laravel - laravel

Good, in php there is a trim function to remove the white spaces, is there a function for blade?
In php it would be like this:
trim($title)
I think I understood your question, what I want is to go from this test text to testtext

You can use #php ... #endphp to call any PHP functions inside blade file. Just to use trim function example is given below.
// #php is similar to <?php ?>
#php
$mytitle = trim($title)
#endphp
// Echo my title
{{ $mytitle }}

Related

When rendering a model, how to use a blade template instead of json as default?

Is it possible to assign a blade template to a model?
Instead of doing this:
#php $contact = Contact::find(1); #endphp
#include('contact', ['contact' => $contact])
I'd like to just do:
#php $contact = Contact::find(1); #endphp
{{ $contact }}
But latter obviously just spits out the model in json.
It is possible with PHP's __toString() magic method: https://www.php.net/manual/en/language.oop5.magic.php#object.tostring
Let's make an example for default User.php model.
First, create a blade file for that model, lets create that as /resources/views/model/user.blade.php and a dummy component;
<h1>{{ $user->name }}</h1>
<p>{{ $user->created_at->diffForHumans() }}</p>
Now make this default __toString() for User model.
Add this to app/Models/User.php ;
/**
* #return string
*/
public function __toString(): string
{
return view('model.user', ['user' => $this])->render();
}
Now you can test it directly in your routes/web.php ;
Route::get('test', function () {
echo \App\Models\User::first();
});
Or try to echo it in any view as;
{!! $user !!}
You can't use {{ $user }} because you need that HTML tags, so you have to use it as {!! $user !!}

How to write post response in Laravel?

How to convert this php code in to laravel?
While add this code in laravel blade getting error?
<?php
$status=$_POST["status"];
echo "<h3>Thank You. Your order status is ". $status .".</h3>";
?>
Laravel is an MVC framework, so the business logic remains in controller function like:
$status = Input::get('status');
and pass it to view like:
return view('view_name', ['status' => $status]);
and you can use this variable on view like:
<h3>Thank You. Your order status is {{ $status }}</h3>

Laravel 5.4 Chaining to display Auth data

I have this in my master blade
<?php $user=auth()->user() ?>
{{ $user->id }}
Im using this code to display the ID of my user detail,
Can you suggest a way to remove <?php ?> or atleast a cleaner approach on this auth ?
Just use id() method:
{{ auth()->id() }}
If you need some other property or related data to display, use user() to get current user object:
{{ auth()->user()->name }}
Maybe you can do something like :
{{ auth()->user()->id }}
or if you have Laravel Auth
use Illuminate\Support\Facades\Auth;
Auth::id();
You can try above suggested answered as cleaner approach, but if you want to remove <?php ?>, you can try below code:
{{--*/ $user=auth()->user() /*--}}
I recommend to use above answered cleaner approach.
if you want to store user to variable initialize it on #php. For example
#php($user = auth()->user())
{{ $user->id }}

Laravel - How to a try/catch in blade view?

I would like to create a way to compile in a view, the try/catch.
How can I do this in Laravel?
Example:
#try
<div class="laravel test">
{{ $user->name }}
</div>
#catch(Exception $e)
{{ $e->getMessage() }}
#endtry
You should not have try/catch blocks in your view. A view is exactly that: a representation of some data. That means you should not be doing any logic (such as exception handling). That belongs in the controller, once you’ve fetched data from model(s).
If you’re just wanting to display a default value in case a variable is undefined, you can use a standard PHP null coalescing operator to display a default value:
{{ $user->name ?? 'Name not set' }}
You are probably doing something wrong if you need a try..catch in your view. But that does not mean you should never do it. There are exceptions to every rule.
So anyway, here is the answer:
Put raw PHP in your view by using <?php ?> or #php #endphp tags. Then put your try..catch in the raw PHP.
#php
try {
// Try something
} catch (\Exception $e) {
// Do something exceptional
}
#endphp
while i agree with #areed that something is wrong if you have to use a try...catch, there are weird cases where a wrong approach can lead you down that path. One instance is using the same blade to house a paginate() and a get() response. Well, this might help a little.
<?php try{ ?>
//try to show something
{{ $products->links() }}
<?php }catch(\Exception $e){ ?>
// show something else
<?php } ?>

Laravel blade #include view using variable

I have a few blade template files which I want to include in my view dynamically based on the permissions of current user stored in session. Below is the code I've written:
#foreach (Config::get('constants.tiles') as $tile)
#if (Session::get('currentUser')->get('permissions')[$tile]['read'] == 1)
#include('dashboard.tiles.' . $tile)
#endif
#endforeach
Blade is not allowing me to concatenate the constant string with the value of variable $tile. But I want to achieve this functionality. Any help on this would be highly appreciated.
You can not concatenate string inside blade template command. So you can do assigning the included file name into a php variable and then pass it to blade template command.
#foreach (Config::get('constants.tiles') as $tile)
#if (Session::get('currentUser')->get('permissions')[$tile]['read'] == 1)
<?php $file_name = 'dashboard.tiles.' . $tile; ?>
#include($file_name)
#endif
#endforeach
Laravel 5.4 - the dynamic includes with string concatenation works in blade templates
#foreach (Config::get('constants.tiles') as $tile)
#if (Session::get('currentUser')->get('permissions')[$tile]['read'] == 1)
#include('dashboard.tiles.' . $tile)
#endif
#endforeach

Resources