I'm new at Laravel and I want to know what best practices are for that :
I have a bunch of item I got with my query. It returns me something like that :
[{"id_equipement":24,"id_typeequipement":5, "imgequipement":xxxx},
{"id_equipement":30,"id_typeequipement":8, "imgequipement":xxxx}]
I have now 15 div to fill (see image below). One div represents one type and I want to know how to check if my first record goes to my second div background image, my second record goes to my third div...
I don't really know how to split in order to check...
Here is my html code:
<div class="item-frame-vertical" id="item-hat">
<div class="item-inner-frame"
style="background-image: url({{ asset('images/item_display/headgear.png') }})"></div>
</div>
<div class="item-frame-vertical" id="item-necklace" data-target="lol">
<div class="item-inner-frame"
style="background-image: url({{ asset('images/item_display/amulet.png') }})"></div>
</div>
<div class="item-frame-vertical" id="item-chest">
<div class="item-inner-frame"
style="background-image: url({{ asset('images/item_display/breastplate.png') }})"></div>
</div>
How can I check :
If the first record is type "5", it belongs to my 8th div
I was intending to do 15 query and give my controller 15 array and check for each div if there is a single record but I'm not sure this is the best way to do it because I must make 15 queries...
The way I'm doing it, I feel that I have to go through my result 15 times with a foreach and then check the type like :
#foreach ($entries as $entry)
if($entry->type_equipement == 5)
{
$img = $entry->img_equipement;
}
else
{
$img = images/item_display/breastplate.png
}
#endforeach
<div class="item-inner-frame" style="background-image:
url({{ asset('$img') }})"></div>
</div>
EDIT :
To be more precise... If I have the :
record with 'id_typeequipement' 5 I must change the background of the div 'item-hat' by 'img_equipement' from my record
record with 'id_typeequipement' 7 I must change the background of the div 'item-chest' by 'img_equipement' from my record
and this, for 15 div
Related
i have two tables which one has room and other has bed when room table enter no of bed 4 then it will other bed table bed four time entry for 4 beds
then i want make every bed own modal so how to do it
i m little confuse for how to do it
here is my code
$room = new Room(array(
'floor_id'=>$request->get('floor_id'),
'room'=>$request->get('room'),
'bed_type'=>$request->get('bed_type'),
'no_of_beds'=>$request->get('no_of_beds'),
'status'=>'assign'
));
$room->save();
$id = $room->no_of_beds;
for($i=1; $i<=$id; $i++){
\DB::table('beds')->insert([
'floor_id'=>$request->get('floor_id'),
'room_id'=>$room->id,
'beds'=>$i]);
}
return back();
when i want in blade file then everytime pass first id so how to beds wise id pass in blade file to use modal popup
beds add propertly as i want but when i in blade file to open each modal for bed then everytime first id get so how to do it as beds wise id when i click modal popup
This is the blade
here is my blade file
#for($i=1; $i<=$lists->no_of_beds; $i++)
<div class="col-lg-3">
<div class="kt-widget24__info">
<span data-toggle="modal" data-target="#modal_assign-{{$lists->id}}">+
</span>
</div>
</div>
#endfor
sorry its very hard to understand what you are trying to do.
Unable to understand your english.
please elaborate your question so that we can answer.
**Questions regarding your code snippet are ** :
1. can you please tell if it is savinf number of beds in DB. e.g if i entered number of beds as 4 does it enter them in DB ? if it does(looks like it do) then what is the problem here ? What do you want to achieve in your Blade file.
Here is my blade file:
#for($i=1; $i<=$lists->no_of_beds; $i++)
<div class="col-lg-3">
<div class="kt-widget24__info">
<span data-toggle="modal" data-target="#modal_assign-{{$lists->id}}">+
</span>
</div>
</div>
#endfor
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'm trying to get pagination into my view. I don't know where is the problem, the code doesn't show any errors.
Here is my controller function
public function apakskategorijas($id)
{
$apakskat = apakskategorijas::with('prece')->where('id',$id)->paginate(2);
return view ('kategorijas.apakskategorijas',compact('apakskat'));
}
View
#section('content')
#foreach($apakskat as $apk)
#foreach($apk->prece as $prec)
<div class="col-md-4 kat">
<a href="{{ url('kategorija/apakskategorija/preces/'.$prec->id) }}">
<div>
<img src="{{ URL::to($prec->path) }}">
</div>
<div class="nos">
<p>{{$prec->nosaukums}}</p>
</div></a>
<div class="price-box-new discount">
<div class="label">Cena</div>
<div class="price">{{ $prec->cena }} €</div>
</div>
<div><span>Ielikt grozā</span></div>
</div>
#endforeach
#endforeach
<center>{{$apakskat->links()}}</center> <--pagination
#endsection
dd($apakskat)
UPDATE
when i changed code in my controller to $apakskat = apakskategorijas::paginate(1); then it showed me pagination, but this doesn't work for me since i need to display items in each subcategory, with this code it just displays every item i have,it doesn't filter which subcategory is selected.
This is why i need this $apakskat = apakskategorijas::with('prece')->where('id',$id)->paginate(1); with is a function that i call which creates a relation between tables, so that it would display every item with its related subcategory.
That's the behavior of paginator in current Laravel version. When you have just one page, pagination links are not displayed.
To test this, just change the code to something like this to get more pages:
$apakskat = apakskategorijas::paginate(1);
If you want to show the first page if there is only one page, you need to customize pagination views. Just publish pagination views and remove this #if/#endif pair from the default.blade.php but keep the rest of the code as is:
#if ($paginator->hasPages())
....
#endif
I would like to put another code of div in every 5th row. Something like:
#foreach ($vip_ads as $key=>$ad)
#if($key%30==0) {
<div></div>
}
#include('front.ad.ad_template.view')
#endforeach
#foreach ($ads as $key=>$ad)
#if($key%30==0) {
<div></div>
}
#include('front.ad.ad_template.view')
#endforeach
My laravel version is 4. So I can't use new loop->iteration function. The problem is that it doesn't give a new block to a div. Everything in one line, meanwhile I need to close 5th row (6 columns total 30 elements) and put a new div, after that continue with the given $key value until it gets for example 60. And the next problem is that I could have less than 10 values in $vip_ads, but in total it has to be 30 for both $vip_ads and $ads. Sorry, for my english. Example of this can seen at http://zaza.iknobel.kz/catalog-ad/index/17
I use only laravel 5.x but your code is not bad, be carefull your key have to be numeric, or add $i=0; before with php and $i++ in the loop
#foreach ($vip_ads as $key=>$ad)
#if($key%5==0)
<div></div>
#endif
#include('front.ad.ad_template.view')
#endforeach
or
<?php $id=0;?>
#foreach ($vip_ads as $ad)
<?php $i++;?>
#if($i%5==0)
<div></div>
#endif
#include('front.ad.ad_template.view')
#endforeach
Edit :
Use something like bootstrap with col-md-3 for ad_template (A) and col-md-12 for div (-----) it will look like this :
A A A A
-------
A A A A
I want to change the CSS class of every second element within an included partial. Here is a code example of the output I want:
<p class="rec"></p>
<p class="sent"></p>
<p class="rec"></p>
<p class="sent"></p>
This is what I tried it so far:
<?php $derp = (isset($derp) && $derp == 'rec') ? 'sent' : 'rec'; ?>
<p class='<?php echo $derp; ?>'>
//CONTENT
</p>
I need help to get this working. I don't want to use the nth-child selector or JavaScript to change the class.
//update:
#forelse($statuses as $status)
#include('statuses.partials.status')
#empty
<div class="notif blue">
<span>This user hasn't yet posted a status.</span>
<span class="icon icon-info-sign"></span>
</div>
#endforelse
The partial get looped like this.
You can use pure CSS for this, using the nth-child selector and the even and odd parameter. In your CSS file simply add:
p:nth-child(odd) {
// This is the odd element so 1, 3, 5, .. element
}
p:nth-child(even) {
// This is the even element so 2, 4, 6, .. element
}
It's working now. I had to keep the code outside of the included partial and not in the partial itself. This made the difference! Anyways, still appreciate the given help!