I have included a couple of files.
my home.blade.php has the following:
#include('includes.header')
my header.blade.php in includes folder has the following
#include('elements.pendingTasksLi', array('tasks'=>$tasks))
and my elements/pendingTasksLi.php file has the following
#foreach ($tasks as $task)
<li>
<a class="todo-actions" href="javascript:void(0)">
<i class="fa fa-square-o"></i>
<span class="desc" style="opacity: 1; text-decoration: none;">{{{$task\['title'\]}}}</span>
<span class="label label-danger" style="opacity: 1;"> {{{$task\['deadLineTime'\]}}}</span>
</a>
</li>
#endforeach
This code in the pendingTakstsLi file is never executed as a loop but only as a simple text.
This is what the output looks like...
#foreach ($tasks as $task)
{{{$task['title']}}} {{{$task['deadLineTime']}}}
#endforeach
I am not sure what to do... Please advise...
Check a screen here
http://i.stack.imgur.com/8emZk.png
Thanks
Jyot
You mentioned that your "pendingTasksLi" view ends in ".php" and not ".blade.php" which would prevent it from being parsed as a blade view.
I had the same problem and as mentioned above you forgot the .blade before the .php
If you want to use expressions like this: {{ $variable}} and want them to be parsed you always have to add .blade in the name pefore the .php extension of the file.
Related
This is the example while looping
despite having a condition and relation many too many it seems that the case
#foreach (\App\ClientDocument::where('id_client', $clients->id)->get() as $item)
#if (\App\Document::where('id',$item->id_document)->get())
<a class="img-fluid " href="{{asset('images/'.$item->file)}}" download="{{asset('images/'.$item->file)}}" alt="" name="download" > Telecharger </a>
#endif
#endforeach
in the above snippet
#foreach (\App\ClientDocument::where('id_client', $clients->id)->get() as $item)
#if (\App\Document::where('id',$item->id_document)->get())
<a class="img-fluid " href="{{asset('images/'.$item->file)}}" download="{{asset('images/'.$item->file)}}" alt="" name="download" > Telecharger </a>
#endif
#endforeach
code is absolutely fine, but there might be the possibility, you are running with duplicate data, you can use this also.
\App\ClientDocument::where('id_client', $clients->id)->distinct('id_client')->get()
and instead of using #if (\App\Document::where('id',$item->id_document)->get())
you can use
#if (\App\Document::where('id',$item->id_document)->firstOrFail())
#if (\App\Document::where('id',$item->id_document)->count())
That's it.
first you shouldn't loading data inside blade files
do it inside controller function
then you should create relationship inside ClientDocument model name document
and in your blade you can do that
$item->document
or whatever you want
I was trying to extend my user.blade.php to my views menu.blade.php. Everything works fine with my other views that use the same user.blade.php too. But not with my menu.blade.php, I get an error saying "Undefined variable: categories (View: D:\xampp\htdocs\mieaceh\resources\views\layouts\user.blade.php)" with "Possible typo $categories
Did you mean $errors?"
Here are the codes to my user.blade.php
#foreach($categories as $category)
<a href="{{ route('menu.index', ['category' => $category->slug]) }}">
<div class="card-category" style="width: 10rem; height: 4rem;">
{{ $category->name }}
</div>
</a>
#endforeach
How do I solve it?
If you want to make a piece of view that appears in multiple places, you can use blade components https://laravel.com/docs/8.x/blade#components.
This will help encapsulating this partials behavior and required data.
I am brand new to Laravel and I'm running Version 6.
I want my view to display a button if one of my MySQL tables has rows that meet a specific condition but I'm having trouble figuring out how to code it - and even WHERE to code it - within my Laravel application.
My MySQL table is called diary_entries and various users of the system will contribute zero to n rows to it. Each row of the table contains a user id called client. When a given user goes to the Welcome view, I want the view to determine if that user currently has any rows in the diary_entries table. If he does, I want to display a button that will take him to another page where the entries can be displayed or edited or deleted.
I think I want to construct an #if that counts the number of records for that user; if the count is greater than zero, I want to display the button, otherwise the button is not displayed.
The problem is that I can't figure out how to code this. I've looked at the examples in the Eloquent section of the manual but they aren't particularly clear to me. I found a note near the top that said the count() function expects a Collection as an argument and that the result of an Eloquent statement is always a Collection so I guessed that I just have to execute an Eloquent query, then apply count() to the resulting Collection. But every variation of that idea which I've tried has thrown exceptions.
Here was the guess that seemed most logical to me:
#extends('layout');
#section('content');
<div class="content">
<img class="centered" src="/images/sleeping-cat.jpg" alt="sleeping cat" height="250">
<div class="title m-b-md">
<h1> Sleep Diary </h1>
</div>
<div>
<h3>{{Auth::user()->name }}</h3>
</div>
<div>
#if (count(App\DiaryEntry::select('*')->where('client', Auth::user()->name) > 0))
<p>
<a class="btn btn-primary"> View / edit existing sleep diary entries </a>
</p>
#endif
</div>
<div>
<p>
<a class="btn btn-primary" href="/diaryEntries"> Create a new sleep diary entry </a>
</div>
</div>
#endsection
This is obviously wrong because it throws an exception so how do I make it right? Does the building of the collection have to move into the Controller? If so, how do I invoke the method and see its result? Or can I do something like I have already done but just adjust the syntax a bit?
EDIT
I've imitated Sehdev's suggestion but I get this error:
$count is undefined
Make the variable optional in the blade template. Replace {{ $count }} with {{ $count ?? '' }}
Here is my welcome view:
#extends('layout');
#section('content');
<div class="content">
<img class="centered" src="/images/sleeping-cat.jpg" alt="sleeping cat" height="250">
<div class="title m-b-md">
<h1>Sleep Diary</h1>
</div>
<div>
<h3>{{ Auth::user()->name }}</h3>
</div>
<div>
#if ($count) > 0))
<p>
<a class="btn btn-primary">View/edit existing sleep diary entries</a>
</p>
#endif
</div>
<div>
<p><a class="btn btn-primary" href="/diaryEntries">Create a new sleep diary entry</a>
</div>
</div>
#endsection
And this is the relevant function from DiaryEntryController:
public function countEntriesOneUser()
{
$count = DiaryEntry::select('*')->where('client', Auth::user()->name)->count();
view("welcome", compact("count"));
}
Should the compact function be returning $count instead of count? I can't find the compact function in the manual with the search function so I'm not clear what it does or what the proper syntax is. I just tried changing the last line of the function to
view("welcome", $count);
but that produced the same error.
Try this,
#php
$count=\App\DiaryEntry::where('client', Auth::user()->name)->count();
#endphp
#if($count>1)
<p><a class="btn btn-primary">View/edit existing sleep diary entries</a></p>
#endif
Using App\DiaryEntry::select('*')->where('client', Auth::user()->name) directly on your blade template is a bad practise.
You can execute your question in your controllers method and then pass the result on your view file
Your function
function test(){
$count = DiaryEntry::select('*')->where('client', Auth::user()->name)->count(); // get the total no of records using count
view("index", compact("count")) // pass your count variable here
}
then you can directly use $count in your #if condition
Your blade template
<div>
#if ($count > 0)
<p><a class="btn btn-primary">View/edit existing sleep diary entries</a></p>
#endif
</div>
I have this design:
I need to say: last post that I add it put it in the head of design then other put them down.
My shut :) Html and foreach code:
#if ($user->projects->count() > 0)
<section class="latest section shadow-sm">
<h1>Projects</h1>
<div class="section-inner">
#foreach ($user->projects->sortByDesc('id')->take(1) as $project)
<div class="item featured text-center ">
// head post
</div>
#endforeach
#foreach ($projects_last->sortByDesc('id') as $project)
<div class="item row">
// other post
</div>
#endforeach
</div><!--//section-inner-->
</section><!--//section-->
#endif
Code of controller for $projects_last:
$projects_last = $user->projects;
$projects_last->pop();
return view('frontend.user_profile',compact('user','projects_last'));
I have the problem with when I say if the #if ($user->projects->count() > 0) do not show any thing but still show me the <h1>Projects</h1> even it is empty!
And if you have any suggest to making my code better pls do it with thankful :)
To iterate Collections you have to get them. So you have to use get() to get the results.
...
#foreach ($user->projects->sortByDesc('id')->take(1)->get() as $project)
...
and
...
#foreach ($projects_last->sortByDesc('id')->get() as $project)
...
You can see here the documentation: Laravel query documentation
Note: if you want to get just one element in your first foreach loop you can use first() instead of take(1). You code will be like that:
#php($first_project = $user->projects->sortByDesc('id')->first())
#if (!is_null($first_project))
// Use $first_project as $project variable
#enif
Good afternoon,
I am working on the blog where I have detail of the article and want to show few pictures.
What I am getting at the moment:
As you can see I am getting the content and after the content pictures. Here is also the code of the blade.
<div class="container">
#foreach($articles as $article)
<article>
<h1 class="title is-1">{{$article->title}}</h1>
#foreach($article->images as $image)
<figure class="image is-128x128">
<img src="{{$image->path}}" alt="{{$image->title}}">
</figure>
#endforeach
<p>{{$article->content}}</p>
</article>
#endforeach
</div>
Because I am getting a object I cannot split the pictures.
.
It was rather hard to figure out what you want, but I'm quite sure I finally got the question.
What you wanna do is something like this:
<div class="container">
#foreach($articles as $article)
<article>
<h1 class="title is-1">{{$article->title}}</h1>
#if(count($article->images) > 0)
<figure class="image is-128x128">
<img src="{{$article->images->first()->path}}" alt="{{$article->images->first()->title}}">
</figure>
#endif
<p>{{$article->content}}</p>
#if(count($article->images) > 1)
#foreach($article->images->slice(1) as $image)
<figure class="image is-128x128">
<img src="{{$image->path}}" alt="{{$image->title}}">
</figure>
#endforeach
#endif
</article>
#endforeach
</div>
This will not only give you headline > image 1 > content > remaining images, it will also make sure you only print images when there are actually some available. The $article->images->slice(1) within the second #if() will ensure we are not using the first image a second time.
Make sure $image->path is the correct to the image file (if it's an url, skip this point).
You can check by echoing it or just dd($image->path).
For the image to be accessed, it needs to be on the public folder of laravel
If you wish to have the files in the storage folder and have it accessed publicly, you'll need to create a symlink
https://laravel.com/docs/5.6/filesystem