Laravel - bug with echoing var in view - php

This code outputs exactly what I want to type on page
{{ dd($users->where('id', $user_id)->first()->avatar) }}
So exactly:
It looks like:
<div class="col-lg-1 section-count text-center my-auto small-text p-margin0">
<b>{{ $topic->views }}</b>
<p>WYŚWIETLEŃ {{ dd($users->where('id', $user_id)->first()->avatar) }}
</div>
But when I remove laravel's "dd" to look it like:
<p>WYŚWIETLEŃ {{ $users->where('id', $user_id)->first()->avatar }}
Suddenly... there is crash splash bum error...
Before I tried to do it even like:
<p>WYŚWIETLEŃ {{ dd($users->where('id', $user_id)->values()[0]->avatar) }}
Works again.
On Page
Okay, so let's delete dd :)!
<p>WYŚWIETLEŃ {{ $users->where('id', $user_id)->values()[0]->avatar }}
crash splash bum error... :<
Now this;
What is this??? It's everything correct when I dump it by dd, and without errors :<.
edit
$users var is collection, looks like this

Okay the first thing you need to do, is that you have to make a check if the user actually exists, so always before calling this, make something like:
#if($user = $users->where('id', $user_id)->first())
<p>WYŚWIETLEŃ {{ $user->avatar }}</p>
#endif
Otherwise, would be good if you can check before this what is this query actually giving you ( might give you null when you use this on collection and not Builder ), so like this:
{{ dd($users->where('id', $user_id)->first()) }}

Related

Symfony twig nl2br

I have twig template and rendering some data in this template, I have two variant
$datatest = "test" . chr(13) . "test"; //nl2br work good
$datatest = "test\ntest";//nl2br dosn't work, have string in template test\ntest
and second I have query builder, with select some field
$qb
->select('
CONCAT(TRIM(s.streetAddress), \'\\n\', s.postal, s.postOffice) as company_address
')
I try
$qb
->select('
CONCAT(TRIM(s.streetAddress), CHAR(13), s.postal, s.postOffice) as company_address
')
but query builder have error, don't find function CHAR(13), how to create custom DQL for CHAR(13) ?
and in template when dump(data) have string but nl2br not work
I try another filter twig, like this
<div style="font-size: 8px;">
{{ data.company_name|upper }}<br>
{% set address = data.company_address|nl2br %}
{{ address|upper|raw }}
</div>
<br>
<div style="font-size: 9px;">
{{ datatest|nl2br }}
</div>
what need to do in select for nl2br work fine??
You shouldn't format the output inside the SQL query. You're using an MVC framework, so you should definitely stay within it's concepts - let the presentation be done in the view (a.k.a. template).
I recommend select and present tha data this way:
Model:
$qb->select('s.streetAddress, s.postal, s.postOffice')->from ...
Template:
{{ streetAddress }}<br>
{{ postal }} {{ postOffice }}

How do I use the POST of Laravel's Route::resource?

Below is my code for a Laravel 4 project.
Going to the authors/create URL and submitting the form gives me a 405 error.
However, if I prepend the routes.php file with Route::post('authors/store', 'AuthorsController#store');, basically doubling what it already should do, everything works like a charm!
Why do I need do prepend said line in my code to work? I can only assume I'm doing something wrong here.
routes.php:
Route::resource('authors', 'AuthorsController');
AuthorsController.php:
public function create() {
$view = View::make('authors.create');
return $view;
}
public function store() {
//
}
authors/create.twig:
{{ form_open({'url':'authors/store'},{"method" : "post"}) }}
<p>
{{ form_label("Name", "name") }}
{{ form_text("name") }}
</p>
<p>
{{ form_submit("Add Author") }}
</p>
{{ form_close() }}
The store action get's trigger when you POST to the resource. So just authors and not authors/store:
{{ form_open({'url':'authors'},{"method" : "post"}) }}
See this table on more information what URL corresponds to what controller action.
Also I think it should be like this:
{{ form_open({'url':'authors', 'method' : 'post'}) }}
And you can pass the route name Laravel automatically generates to make your life a bit easier:
{{ form_open({'route':'authors.store', 'method' : 'post'}) }}
Oh and one more, post is the default method so this should do as well:
{{ form_open({'route':'authors.store'}) }}

Changing a href to laravel anchor tag

Here is my regular code in the blade
{{ $Blog->BlogTitle }}
I want to do something like
{{ HTML::link('http://test.com', null, array('id' => 'linkid'))}}
I mean i want to do it in the laravel way..
How can i do this ?
Try the following code
{{ HTML::link(url().'/blog'.$Blog->id, $Blog->BlogTitle)}}

NotFoundHttpException at new route Laravel 4

i see there are similar questions but dont find any clue of me problem.
I created a basic users system, to manage groups, permissions, users, etc. The basic routes like create, edit, delete, index are working.
Now im trying to add one more function to UserController, to manage the users groups in a simple view.
Route::group(array('prefix' => 'admin'), function()
{
Route::resource('groups', 'GroupController');
Route::resource('users', 'UserController');
});
The function in controller:
public function groups($id)
{
$user = Sentry::findUserByID($id);
$groups = $user->getGroups();
return View::make('users.show')
->with('groups', $groups);
}
And the users/groups.blade.php:
#extends('layouts.admin')
#section('content')
<header id="page-title">
<h1>User Groups</h1>
</header>
<!-- if there are creation errors, they will show here -->
{{ HTML::ul($errors->all()) }}
{{ Form::open(array('url' => 'admin/users/save_groups')) }}
<div class="form-group">
</div>
{{ Form::submit('Create!', array('class' => 'btn btn-primary')) }}
{{ Form::button('Cancel', array('class' => 'btn btn-danger')) }}
{{ Form::close() }}
#stop
I go to url "mysite/admin/users/2/groups", and im getting the NotFoundHttpException, i try many ways to make it works and dont know what is happening.
I assume it will works like "mysite/admin/users/2/edit", but if i test the show function, it only is "mysite/admin/users/2", dont need the show action to know is that function, maybe i missed something.
You have declared a route for "GroupsController". As per the documentation, this will only handle actions as defined in the table: "Actions Handled By Resource Controller"
Just by adding one more action it won't simply be extended by Laravel.
You should instead type:
Route::get('users/{id}/groups', 'UserController#groups');

Laravel language handler in templates blade

How can i use something like {{#something}} and it will run a controller that checks for "something" so i can return it to translteable text?
My current blade template looks like following:
#layout("layouts.default")
#section("inner")
<h1>Velkommen til pornobiksen</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection
I mean, how can i change the "Velkommen til pornobiksen" tekst? I know i can make something like
View::make("template")->with("h1_text","Velkommen til pornobiksen");
But is there not a module/plugin to make it easier? By making like {{#h1_text}} and it will takes from my database or something?
What is the easists way to make this?
You need to use {{ $h1_text }} to put the variable into your blade template.
#layout("layouts.default")
#section("inner")
<h1>{{ $h1_text }}</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection
EDIT
I think I misunderstood you, it seems you are looking for localization
#layout("layouts.default")
#section("inner")
<h1>{{ Lang::get('messages.welcome') }}</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection
For localization , you can use helper function : trans
home.php
return [
'welcome' => 'Velkommen til pornobiksen'
];
View
#layout("layouts.default")
#section("inner")
<h1>{{trans('home.welcome')}}</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection

Categories