I want to nest divs in a loop like this
<div>
Content
<div>
Content
<div>
Content
</div>
</div>
</div>
I am thinking of recursion but I am not sure how to implement it in blade.
You can use include to call the nested content in Blade:
example.blade.php:
<div>
{{ $content->data }}
#if ($content->hasNestedData())
#include('viewfolder.example', $content->nestedContent)
#endif
</div>
To check for nested content, you could implement a function in your model like this:
public function hasNestedData()
{
return $this->nestedContent !== null;
}
And set the nested content as an attribute, for example:
public function nestedContent()
{
return $this->belongsTo(Content::class, 'nested_content_id');
}
As you didn't post the code you already have, use this as the main idea.
Related
I'm making a "teacher's" app, and I want to make a log-in page which changes depending if there's registered users in the database or not.
I want to make a redirection button to a create user page if there aren't auth users in database, and to make a select user view if the database have one or more users.
The problem is that I don't know how to exactly do this, 'cause the view always shows me the first statement (what I've got in the if), also if in the database are registered users. Can anyone help me with this please?
This is the blade file:
#if (empty(Auth::user()->id))
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Welcome</h1>
<p>We see there aren't users</p>
</div>
<div id="loginForm">
<button type="button" onclick="window.location='{{ url("/newUser") }}'">Button</button>
</div>
</div>
#else
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Select an user</h1>
</div>
<div id="loginForm"></div>
</div>
#endif
Here you have the controller index method:
public function index()
{
$users = User::all();
return view('/', compact('users'));
}
And finally here you have the page:
The following code is the sample for it, kindly replace code accordingly
#if(!$user)
//show button
#else
//dont show button
#endif
I think your question is you want to check if there is user in database.
So no need to check if the user authenticated but to check if there is user on the database.
In your controller
public function index() {
return view('/', ['users' => User::all()]);
}
and in your blade file
#if(!$users)
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Welcome</h1>
<p>We see there aren't users</p>
</div>
<div id="loginForm">
<button type="button" onclick="window.location='{{ url("/newUser") }}'">Button</button>
</div>
</div>
#else
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Select an user</h1>
</div>
<div id="loginForm"></div>
</div>
#endif
This function will get the current authenticated user: Auth::user(). I guess what you are trying to achieve is #if(empty($users)) where $users is the variable you are passing on controller.
If you want to verify if the user that accessed to that view is authenticated you can simply use #auth and #guest.
Also i would suggest you to change your button to an <a> tag and your href would be <a href="{{ route('route.name') }}" where route.name would be defined in your routes file.
in your controller:
you can create a folder inside views called users and then the index.blade.php (views/users/index.blade.php)
public function index()
{
$users = Users::all();
return view('users.index')->with('users', $users);
}
in your view:
#if(count($users) < 1)
...
#else
...
#endif
count is validating if the $users array length is less then 1 (in other words if the array is empty).
Alternative you can you isEmpty()
#if($users->isEmpty())
...
#else
...
#endif
What is the correct way to call the "Links" function after this "Foreach"?
I don't know how to handle the variable to put in function.
#inject('usuarios', 'App\User')
#foreach($usuarios->getIndicados() as $user)
#endforeach
<div class="row">
<div class="col-12 text-center">
{{ $usuarios->getIndicados()->links() }}
</div>
</div>
Maybe it's just an editing error, but in your output the -tags don't seem to be closed again. Also, there should be no space like < a> at the beginning of the tag. And < a hr_ef= ... is obviously wrong.
In order to style them, you can add a class attribute to the tags while building the string and do the style-stuff in css.
This is what laravel document provides. You need to add links in the collection.
<div class="container">
#foreach ($users as $user)
{{ $user->name }}
#endforeach
{{ $users->links() }}
I'm using Blade templates in Laravel 5.3. I want to render two lists - one of 'friends' and one of 'acquaintances'. The header and footer of the list is the same in both cases, but items rendered in the friends list have different formatting and fields from those rendered in the acquaintances list.
Here are two methods in my controller:
public function showFriends() {
return view('reports.friends', ['profiles' => $friends]);
}
public function showAcquaintances() {
return view('reports.acquaintances', ['profiles' => $acquaintances']);
}
Here are my blade templates:
// reports/acquaintances.blade.php
<div>Some generic header HTML</div>
<div class="container">
#each('reports.acquaintance', $profiles, 'profile')
</div>
<div>Some generic footer HTML</div>
// reports/acquaintance.blade.php
<div class="media">
<div>Some HTML formatting specific to acquaintance item</div>
{{ $profile->name }}
{{ $profile->job }}
</div>
// reports/friends.blade.php
<div>Some generic header HTML</div>
<div class="container">
#each('reports.friend', $profile, 'profile')
</div>
<div>Some generic footer HTML</div>
// reports/friend.blade.php
<div class="media">
<div>Some HTML formatting specific to friend item</div>
{{ $profile->name }}
{{ $profile->birthday }}
</div>
This doesn't seem to be a very efficient way to achieve what I want because the I've had to create two identical parent templates for my lists: friends.blade.php and acquaintances.blade.php. What I really need is the ability to have a generic parent template and then somehow to specify in my controller which template I want to use to render the list items. Is this possible? Is there another, more elegant way to implement this? I'm just starting to get my head around Blade and any pointers would be very appreciated.
You could break it into a generic persons_list and two custom items. Then use a conditional inside of the list:
public function showFriends() {
return view('reports.persons_list', ['profiles' => $friends, 'type' => 'friends']);
}
public function showAcquaintances() {
return view('reports.persons_list', ['profiles' => $acquaintances, 'type' => 'acquaintances']);
}
And blade:
// reports/persons_list.blade.php
<div>Some generic header HTML</div>
<div class="container">
#if ($type == 'friends')
#each('reports.friend', $profiles, 'profile')
#else
#each('reports.acquaintance', $profiles, 'profile')
#endif
</div>
<div>Some generic footer HTML</div>
I have 2 actions functions in my controller and I want to pass variable to an other action function
This is my first function in my controller
public function newUserAction(Request $request)
{ .........
$url = $this->generateUrl('userBundle_new_user_reasonCodeAjaxView',
array('id' => $newUser->getCode(),
'countCode' => $countCode,));
return $this->redirect($url);
This my second funtion in my controller
public function userCodeAjaxViewAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$pc = $em->getRepository('UserBundle:Code')->find($id);
if($pc != null)
{
return $this->render('UserBundle:userCodeView.html.twig', array(
'pc' => $pc,
));
}
And my twif looks like this
<div class="">
<div class="panel panel-default step3-textarea top-arrow top-arrow">
<div class="panel-body">
<fieldset>
<div>
{{ pc.name|trans }}
{{countCode}}
</div>
</fieldset>
</div>
</div>
</div>
I am getting an error Variable "countCode" does not exist in...
Is there any idea how I can use a variable from a controller in a other controller?
You can not get a variable from another controller.
Every request creates only one controller instance.
What you can do is create a Service that you can call in the controller.
Something like that:
class CountUserService{
public function count(){
return 1; // count users here
}
}
And the in the controller do this:
$service = new CountUserService();
$data=['countCode' => $service->count()];
return $this->render('UserBundle:userCodeView.html.twig', $data);
You are using the same template scope for the two controllers, and this is not possible. If you are using two controllers to render two simple informations, why not simply returning a JSON and displaying it in a more global template using AJAX ?
Else a pure symfony solution would be to have a main template view.html.twig where you would put
<div class="">
<div class="panel panel-default step3-textarea top-arrow top-arrow">
<div class="panel-body">
<fieldset>
<div>
{{ render(controller("AppBundle:UserController:newUserAction")) }}
{{ render(controller("AppBundle:UserController:userCodeAjaxViewAction")) }}
</div>
</fieldset>
</div>
</div>
Given that then your two controller action templates would be simple {{ pc.name|trans }} and {{countCode}}.
Hope it helps you out !
I am doing project in laravel. In my project there are two tables categories and subcategories. subcategories table has categoryid as foreign key.
subcategoryModel.php has following function
public function category(){
return $this->belongsTo('App\Category');
}
In my blade file I am displaying my subcategories using category object as follows,
blade.php
<div class="col-sm-6 col-align-left">
#foreach($category as $c)
<div class="form-group clearfix">
#if($c->subcategories->count() > 0)
<h4><b>Subcategories: </b></h4>
<div class="form-group clearfix">
#foreach($c->subcategories as $subcategory)
<h4>{{$subcategory->subcategoryname}}</h4>
#endforeach
</div>
#endif
<h4>Add Subcategories</h4>
</div>
#endforeach
</div>
This display correct data,but now I want to display data in the order they are updated/created. Is there any way to display data as per the requirement?
I want to something into the blade file itself. How to do this? Please give suggestions.
You shouldn't do something like that in a blade. Blade should be kept as dumb as possible. Main things should be done in the controller and then passed to the blade. you can do this in your controller:
$categories = Category::with(array('subcategories' => function($query) {
$query->orderBy('created_at', 'DESC');
}))->get();
now the subcategories in the category object will be ordered.
#foreach($c->subcategories()->orderBy('updated_at','asc')->get() as $subcategory)
<h4>{{$subcategory->subcategoryname}}</h4>
#endforeach
Edit: I do like the other answer however OP asked for within the blade file itself