How to ignore html if data is empty in Laravel? - laravel

I am trying to retrieve data from database and check if the data is empty or not. What problem I am facing is that html is showing even if the data is empty. I want to ignore the html tag like example ul li. Here how i tried is like
#if(!empty($jobseekers->skill_1))
<li> My Skill is : {{ \App\skill::where('id',$jobseekers->skill_1)->pluck('name')->first() }}</li><br/>
#endif
I want to ignore "My Skill is " if the data is empty. I don't want to show anything.

When using ->get() you can't simply use any of the below:
if (empty($jobseekers->skill_1)) { }
if (!$jobseekers->skill_1) { }
if ($jobseekers->skill_1) { }
But, When you are getting data with first() method, You can simply use all above methods.
Because if you dd($jobseekers->skill_1); you'll notice an instance of Illuminate\Support\Collection is always returned, even when there are no results.
I think you are using !empty() on data with ->get() method which will always return true even data is empty. You need to use other way.
To determine if there are any results you can do any of the following:
if (!$jobseekers->skill_1->isEmpty()) { }
if ($jobseekers->skill_1->count()) { }
if (count($jobseekers->skill_1)) { }

If you get $jobseekers with get() method you can not use empty($jobseekers )
instead of empty you can use other conditions :
#if($jobseekers->skill_1 != '')
in this condition you check skill_1 as empty string
also
#if($jobseekers->skill_1)
and etc
replace your code with below code and check it:
#if($jobseekers->skill_1 != '')
<li> My Skill is : {{ \App\skill::where('id',$jobseekers->skill_1)-pluck('name')->first() }}</li><br/>
#endif

you should use isset()
#if(isset($jobseekers->skill_1))
<li> My Skill is : {{ \App\skill::where('id',$jobseekers->skill_1)->pluck('name')->first() }}</li><br/>
#endif

you can use count method
#if(count($jobseekers->skill_1)>0)
<li> My Skill is : {{ \App\skill::where('id',$jobseekers->skill_1)-pluck('name')->first() }}</li><br/>
#endif

#if(count($data)>0)
//write your code which you want to show if data is available
#endif

Related

Vue.js Change text color based on 3 conditions

I have a table with 3 possible values: Firenze, Roma and Milano.
These 3 values need to be associated to 3 different colours.
On page load, every value should render with its associated colour.
Inline conditional rendering can't be used as it only can hold 2 values.
I've tried using functions such as this one below
($missing_item is a v-for single prop got from $missing_items which is an external array prop)
function getColour($missing_item){
switch($missing_item.Deposito){
case $missing_item.Deposito == 'Firenze':
return 'text-violet-400';
case $missing_item.Deposito == 'Roma':
return 'text-orange-400';
case $missing_item.Deposito == 'Milano':
return 'text-green-400';
}
}
I'm calling the function like this: :class="changeColour()"
but I keep getting the same error:
"Uncaught (in promise) TypeError: Cannot read properties of undefined
(reading 'Deposito')"
.
My main issue at this point is how to access the renderd "$missing_item.Deposito" inside a function
EDIT:
As I understand you try to call your method from a v-for. In this case just add the parameter from the loop:
<div v-for="(missing_item,index) in missing_items" :key="`Item${index}`">
<div :class="changeColour(missing_item)">
I change color depending on which city I am.
</div>
</div>
ORIGINAL ANSWER
There is no need for an additional function, Vue can handle this inside the class binding (see docs).
In your example it could look like this:
<div :class="$missing_item.Deposito == 'Firenze' || $missing_item.Deposito == 'Milano' ? 'text-green-400' : 'text-orange-400'">
{{ $missing_item.Deposito }}
</div>
While this works, the "Vue way" would be to use it in combination with a computed property like this:
<div :class="depositoStyle">
{{ $missing_item.Deposito }}
</div>
computed: {
depositoStyle() {
return this.$missing_item.Deposito == 'Firenze' || this.$missing_item.Deposito == 'Milano' ? 'text-green-400' : 'text-orange-400';
}
}
If you'd rather use a switch case than the short form, you can insert your switch case function into the computed property and return the end result.

Cannot output laravel model getter property containing array

I want to have model property containing array of files from specific directory
public function getGalleryAttribute($value) {
return Storage::disk('storage')->files($value);
}
then I output on the page
#foreach ($item->gallery as $img)
<img src="/storage/{{ $img }}" >
#endforeach
and I see error
Attempt to read property "gallery" on array (View: /var/www/site/resources/views/components/postpartials/gallery.blade.php)
I cannot understand what is the problem
if ($item->gallery->isNotEmpty()) {
}
try checking if the gallery property exists in the array and if there is some sort of data stored in that
just use dd($item);
it seems that it was mistype. Issue is closed.

how construct route pattern for an unknown number of tags - Laravel & Conner/Taggable

I have a blog and a quotationfamous sayings repository on one site.
The quotations are tagged and the entries are tagged too.
I use this rtconner/laravel-tagging package.
Now, what I want to do is to display ALL Quotation models which share the same tags as article.
The Eloquent syntax is simple, as the original docs provide an example:
Article::withAnyTag(['Gardening','Cooking'])->get();
possible solution
Optional routing parameters. The asker-picked answer in this question gives a solution:
//in routes.php
Route::get('/{book?}/{chapter?}/{topic?}/{article?}', 'controller#func');
//in your controller
public function func($book = null, $chapter = null, $topic = null, $article = null) {
...
}
my problem
In my app the shared tags might count more than 3 or 5. I will soon get an example with even 10 tags. Possibly more
My question
Does it mean that I have to construct an URL with 10 optional routing parameters? Do I really need sth like this:
Route::get('quotations/tags/{tag1?}/{tag2?}/{tag3?}/{tag4?}/{tag5?}/{tag6?}/{tag7?}', 'controller#func');
my question rephrased
I could create a form with only a button visible, and in a hidden select field I could put all the tags. The route would be a POST type then and it would work. But this solution is not URL-based.
I think you could process the slashes, as data:
Route::get('quotations/tags/{tagsData?}', 'controller#func')
->where('tagsData', '(.*)');
Controller:
public function controller($tagsData = null)
{
if($tagsData)
{
//process
}
}
Ok, this is my solution. As I have a tagged model, I dont't need to iterate through tags in url to get the whole list of tags.
The enough is this:
// Routes file:
Route::get('quotations/all-tags-in/{itemtype}/{modelid}', 'QuotationsController#all_tagged_in_model');
Then in my controller:
public function all_tagged_in_topic($itemtype, $id) {
if($itemtype == 'topic') {
$tags = Topic::find($id)->tags->pluck('name')->all();
$topic = Topic::find($id);
}
if($itemtype == 'quotation') {
$tags = Quotation::find($id)->tags->pluck('name')->all();
$quotation = Quotation::find($id);
}
// dd($tags);
$object = Quotation::withAnyTag($tags)->paginate(100);;
And it is done.
Now, the last issue is to show tags in the URL.
TO do that, the URL should have an extra OPTIONAL parameter tags:
// Routes file:
Route::get('quotations/all-tags-in/{itemtype}/{modelid}/{tags?}', 'QuotationsController#all_tagged_in_model');
And in the {url?} part you can just write anything which won't break the pattern accepted by route definition.
In your view you might generate an URL like this:
// A button to show quotes with the same set of tags as the article
// generated by iteration through `$o->tags`
<?php
$manual_slug = 'tag1-tag2-tag3-tag4`;
?>
<a href="{{ URL::to('quotations/all-tags-in/article/'.$o->id.'/'.$manual_slug) }}" class="btn btn-danger btn-sm" target="_blank">
<i class="fa fa-tags icon"></i> Tagi:
</a>

laravel-4 IF loop when relation is set

I have this relation in my Post model
public function mainItem()
{
return $this->belongsTo('Item', 'item_id');
}
Some of the Posts are connected to Item, some are not.
I can print the binding status with this line:
{{ $post->mainItem->id or 'not yet binded!'}}
I can even print image delivered through relation
<img src="{{ $post->mainItem->pic_filename or ''}}">
...and hide empty IMG with no SRC with the use of CSS3.
But how I can print a link or communique depending of the binding status?
I want to print a button which binds a post to an item - but the button should be visible only when the relation is not yet set.
#if($post->mainItem->count() > 0)
... here comes the button ...
#endif
The code works in case of a hasMany relation, but not in case belongsTo.
The return of a belongTo would be a single model. So to check this would be to check if the object is_null, as if there is no related model it will return null.
So this as you said for a hasMany;
#if($post->mainItem->count() > 0)
... here comes the button ...
#endif
And this for a belongsTo;
#if(is_null($post->mainItem))
.. Button
#endif
To find out more of the return types use the API found here

model relationship and routes get model id

i have the following route:
Route::get('notes/main', function(){
$destinations = Destination::where('show', '=','1')->get();
$notes = Destination::find($destination->id)->notes()->get();
return View::make('notes.main')
->with('destinations', $destinations);
});
//the relationship models:
<?php
class Destination extends Eloquent {
public function notes()
{
return $this->has_many('Note');
}
}
<?php
class Note extends Eloquent
{
public function destination()
{
return $this->belongs_to('Destination');
}
}
//View:
#foreach( $destinations as $destination)
{{ $destination->name}}<br>
{ $notes->destination->text }} // this isn't echoed
#endforeach
what's the correct way to filter this and define $destination->id
thanks
How would i Filter the notes in an if statement inside the loop ?
#if (isset($note->title) != 'shorttext')
#else
<p> {{ $note->text }} </p>
#endif
#endforeach
You use $destination->id in your Route but it seems to be not defined yet. The question is, what do you want to achieve with your code?
With:
$destinations = Destination::where('show', '=','1')->get();
$notes = Destination::find($destination->id)->notes()->get();
you are getting only the Notes of one specific destination (so far, $destination is not defined. You could use Destination::all()->first() to get the first, or Destination::find(id), with ID being replaced with the primary key value of the destination you need).
But I guess you don't want it that way. From your Output it seems like you want to have an output with each destination and below each destination the corresponding Notes.
Controller:
//gets all the destinations
$destinations = Destination::where('show', '=','1')->get();
return View::make('notes.main')
->with('destinations', $destinations);
View:
#foreach( $destinations as $destination)
{{ $destination->name}}<br>
#foreach($destination->notes as $note)
{{ $note->text }} <br>
#endforeach
<hr>
#endforeach
Didn't test this, but this way your View would show you all your Destinations and for each Destination all the Notes.
In your route, your not sending $notes variable to the view.
How I would do it:
$destinations = Destination::where('show', '=','1')->get();
$destinations->notes = Destination::find($destination->id)->notes()->get();
Then in view:
#foreach( $destinations as $destination)
{{ $destination->name}}<br>
{{ $destination->notes->text }}
#endforeach
So first you asked a question, I gave you a correct answer.
You marked my answer as accepted, but later on you edit your initial question to add another question (for which you should create a new thread instead).
Then you create your own answer which is only answering part of your initial question and mark that as the good solution?
I could tell you why your isset and empty did not work, but why should I if you don't value my help at all?
OK
for the last part this worked out at the end
function isset and empty didn't work , strange:
#if ($note->title == 'shorttext')

Resources