Hello how do i check if database row insert exist in laravel? i want to display something entries if > 0 and i want to display else if entries = 0. but i don t know how to do that. i tried with forelse, if else and i got same error. Undefined variable $istProj.
<div class="card-body">
#if ($istoric->isEmpty())
#forelse ($istoric as $istProj)
<div class="mb-3">
<table class='table'>
<tr class="table-row-heads">
<th>Id Proiect</th>
<th>Tip actiune </th>
<th>Colaborator </th>
<th>Suma </th>
<th>Data </th>
</tr>
<tr class="table-row-data">
<td>{{ $istProj->id_proiect }}</td>
<td>{{ $istProj->action_type }}</td>
<td>{{ $istProj->colaborator_id }}</td>
<td>{{ $istProj->suma }}</td>
<td>{{ $istProj->data }}</td>
</tr>
</table>
</div>
#empty
<div class="card-body">
<h1>Nu au fost gasite inregistrari</h1>
</div>
#endforelse
#endif
</div>
<form action="{{ url('/') }}" method="POST">
#csrf
#method('PUT')
<div class="mb-3">
<label class="form-label">Id proiect</label>
<input type="text" class='form-control' value="{{ $proiecte->id }}" name='id_proiect' id='id_proiect' placeholder="Id proiect">
</div>
<div class="mb-3">
<label class="form-label">Tip actiune</label>
<select class="form-select" aria-label="Default select example" name='Status_Tranzactii'>
<option selected>Alege tipul actiunii (0 = cheltuiala, 1 = plata, 2 = incasare)</option>
<option value="cheltuiala">0</option>
<option value="plata">1</option>
<option value="incasare">2</option>
</select>
</div>
<div class="mb-3">
<label class="form-label">Colaborator</label>
<select class="form-select" aria-label="Default select example" name="Colab_id">
<option selected>Alege colaboratorul (daca este cazul)</option>
#foreach ($colaborator as $colaboratori)
<option value="{{ $colaboratori->id }}">{{ $colaboratori->id }} </option>
#endforeach
</select>
</div>
<div class="mb-3">
<label class="form-label">Suma</label>
<input type="text" class='form-control' value="{{ $istProj->suma }}" name='suma' placeholder="Introduceti suma">
</div>
<div class="mb-3">
<label class="form-label">Data</label>
<input type="text" class='form-control' value="{{ $istProj->data }}" name='data' placeholder="Introduceti data">
</div>
<button type='submit' class='btn btn-primary' style="float: right;">Adauga</button>
</form>
How can i make that to work?
Error on line with "Suma"
If I understand well, you already have a Collection result (From Eloquent or anywhere) and you need to do different action based on that result.
I'm,right? If yes....
To check the amount or entries you can use $result->count(), so in Blade you can:
#if($result->count() > 0)
Do something
#else
Do anotherthing
#endif
Related
This code from product_listing.blade.php and here the dropdown filter. I need all the item name in ascending orde. Right now the list is in order how I can see it in a database table..
<div class="box-title text-center">
{{ translate('Filter by CarType')}}
</div>
<div class="box-content">
<div class="filter-checkbox">
<select class="form-control" data-placeholder="{{ translate('All CarType')}}" name="cartype" onchange="filter()">
<option value="">Select CarType</option>
#foreach (\App\CarType::all() as $cartype)
<option value="{{ $cartype->id }}" #isset($cartype_id) #if ($cartype_id == $cartype->id) selected #endif #endisset>{{ $cartype->name }}</option>
#endforeach
</select>
</div>
</div>
You can use collection's sortBy() method:
<div class="box-title text-center">
{{ translate('Filter by CarType')}}
</div>
<div class="box-content">
<div class="filter-checkbox">
<select class="form-control" data-placeholder="{{ translate('All CarType')}}" name="cartype" onchange="filter()">
<option value="">Select CarType</option>
#foreach (\App\CarType::all()->sortBy('name') as $cartype)
<option value="{{ $cartype->id }}" #isset($cartype_id) #if ($cartype_id == $cartype->id) selected #endif #endisset>{{ $cartype->name }}</option>
#endforeach
</select>
</div>
</div>
<form action="/group/{id}/save/group" method="POST">
#csrf
<table id="modal-table">
#foreach($user_list->get() as $f)
<tr>
<td>
<div class="image">
#if($f->follower->avatar_location)
<img src="{{asset('storage/'.$f->follower->avatar_location)}}" class="img-circle" style="max-height:50px;" />
#else
<img src="{{url('/')}}/assets/media/icons/socialbuttons/user.png" class="img-circle" style="max-height:50px;"/>
#endif
</div>
<div class="detail">
#if($f->follower->verified == 1)
{{ $f->follower->name }}<img id="verified_img_sm_mess_list" src="{{url('/')}}/assets/media/icons/socialbuttons/octagonal_star.png" alt="Recensioni">
#else
{{ $f->follower->name }}</h3>
#endif
<small id="mess_list_uname">{{ '#'.$f->follower->username }}</small>
</div>
<input name="group" value="{{ $group->hobby->id }}">
<input name="person" value="{{ $f->follower->id }}">
<button type="submit" class="btn-link">Go</button>
</td>
</tr>
#endforeach
</table>
</form>
Hey a small problem! in the Foreach loop and every member in the list, I tried some bare-bones to get each users id and hobby id (to add to database, but that's not important). For some reason, when I take the input 'name' and click the Go button, All "id's" of everyone in the list are executed, Not the individual id of each user. Meaning.. each user displays their individual id and a go button, but click go and all id's are collected, not just one. It's probably really obvious but any ideas why this is happening?? Thanks!
Like said Tim Lewis in the comment, you need to put your form inside your foreach, so when the "Go" button is clicked, only the corresponding form will send the data, like this :
<table id="modal-table">
#foreach($user_list->get() as $f)
<form action="/group/{id}/save/group" method="POST">
#csrf
<tr>
<td>
<div class="image">
#if($f->follower->avatar_location)
<img src="{{asset('storage/'.$f->follower->avatar_location)}}" class="img-circle" style="max-height:50px;" />
#else
<img src="{{url('/')}}/assets/media/icons/socialbuttons/user.png" class="img-circle" style="max-height:50px;"/>
#endif
</div>
<div class="detail">
#if($f->follower->verified == 1)
{{ $f->follower->name }}<img id="verified_img_sm_mess_list" src="{{url('/')}}/assets/media/icons/socialbuttons/octagonal_star.png" alt="Recensioni">
#else
{{ $f->follower->name }}</h3>
#endif
<small id="mess_list_uname">{{ '#'.$f->follower->username }}</small>
</div>
<input name="group" value="{{ $group->hobby->id }}">
<input name="person" value="{{ $f->follower->id }}">
<button type="submit" class="btn-link">Go</button>
</td>
</tr>
</form>
#endforeach
</table>
<table id="modal-table">
#foreach($user_list->get() as $f)
<form action="/group/{id}/save/group" method="POST">
#csrf
<tr>
<td>
<div class="image">
#if($f->follower->avatar_location)
<img src="{{asset('storage/'.$f->follower->avatar_location)}}" class="img-circle" style="max-height:50px;" />
#else
<img src="{{url('/')}}/assets/media/icons/socialbuttons/user.png" class="img-circle" style="max-height:50px;"/>
#endif
</div>
<div class="detail">
#if($f->follower->verified == 1)
{{ $f->follower->name }}<img id="verified_img_sm_mess_list" src="{{url('/')}}/assets/media/icons/socialbuttons/octagonal_star.png" alt="Recensioni">
#else
{{ $f->follower->name }}</h3>
#endif
<small id="mess_list_uname">{{ '#'.$f->follower->username }}</small>
</div>
<input name="group" value="{{ $group->hobby->id }}">
<input name="person" value="{{ $f->follower->id }}">
<button type="submit" class="btn-link">Go</button>
</td>
</tr>
</form>
#endforeach
</table>
check this part of Jérôme W post
<form action="/group/{id}/save/group" method="POST">
Change to this
<form action="/group/{{$group->id}}/save/group" method="POST">
i have 3 tables , arts(code,designation,prix,qte,gamme_id)
gammes(type)
factures(art_id,prix_total,quantité)
i want to search all gammes between 2 dates in facture table but the problem i dont have relation between factures and gammes , i just have relation between factures and arts
controller
public function store(Request $request)
{
$prix_totale=0;
$Gammes = Gamme::all();
$gamme_id=$request->get('gamme_id');
$date_debut=$request->get('date_debut');
$date_fin=$request->get('date_fin');
$articles=Art::where('gamme_id','LIKE',$gamme_id)->paginate(1000);
$data =Facture::where('art_id','LIKE',$articles)->whereBetween('created_at',[$date_debut,$date_fin])->orderByDesc('created_at')->paginate(1000);
foreach ($data as $p) {
$prix_totale+=$p->prix;
}
return view('affairegamme.resultat', compact('data','arts','prix_totale','articles'));
}
index.blade.php
<form method="post" action="{{ route('affairegamme.store') }}" enctype="multipart/form-data" width="20" height="20">
#csrf
<div class="form-group">
<label class="col-md-4">Gamme:</label>
<div class="col-md-8">
<select name="gamme_id" id="gamme_id" class="form-control" >
<option value="">Select Gamme</option>
#foreach($gammes as $gamme)
<option value="{{ $gamme->id}}">{{ $gamme->type }}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4">Date debut :</label>
<div class="col-md-8">
<input type="date" name="date_debut" value="{{ old('date', date('Y-m-d')) }}" class="form-control input-lg" />
</div>
</div>
<div class="form-group">
<label class="col-md-4">Date fin</label>
<div class="col-md-8">
<input type="date" name="date_fin" value="{{ old('date', date('Y-m-d')) }}" class="form-control input-lg" />
</div>
</div>
<br/>
<div class="form-group text-center">
<input type="submit" name="add" class="btn btn-primary input-lg" value="Chercher !" />
</div>
</form>
resultat.blade.php
<table class="table table-bordered table-striped">
<td><h6>ID de facture</h6></td>
<td><h6>date</h6></td>
<td><h6>Operation_id</h4></td>
<td><h6>Article</h4></td>
<td><h6>Prix Unitaire d Article</h4></td>
<td><h6>Quantite</h6></td>
<td><h6>Prix Totale</h6></td>
</tr>
#foreach($data as $row)
<tr>
<td>{{ $row->id}}</td>
<td>{{ $row->created_at}}</td>
<td>{{ $row->operation_id }}</td>
<td> {{$row->art->designation}}</td>
<td> {{$row->art->prix}}</td>
<td> {{$row->qte}}</td>
<td> {{$row->prix}}</td>
#endforeach
</table>
<br> </br>
<h1 style="font-size:200% ;text-align:center">Le chiffre d affaire pour la gamme est : {{$prix_totale}} dt</h1>
The below provided eloquent query will search for the fractures between the provided dates in the fracture table.
$grammesBetweenTwoDates = Gramme::whereHas('arts',function($query) use ($date_debut,$date_fin) {
// Any query to apply filter in arts
return $query->whereHas('fractures', function($fractureQuery) use ($date_debut,$date_fin){
// Any query to apply filter in fractures
return $fractureQuery->whereBetween('created_at', [$date_debut,$date_fin]);
});
})->paginate(1000);
Since you seem to have problem understanding the answer I will try my best to explain. The above query instructs the database to
Find all the grammes that have arts (from grames_id present in arts table ).
Find all the arts that have fractures (from arts_id present in fractures table )
Find all the fractures that are in between two dates ( from the date provided in eloquent query )
When above all cases match the query return the result.
In the above query you need to have arts relation in Grammes model.
public function arts()
{
return $this->hasMany(Arts::class);
}
And you need to gave fractures relation in Art model
public function fractures()
{
return $this->hasMany(Fracture::class);
}
I am trying to remove the refresh time after I click the end button to submit the form into the database.
I am not that experienced when it comes to jquery and I rely only to video tutorials and forums at this current time but I am on my learning curve. as you can see I already converted the start button into jquery to remove the refresh time when I click start button.
<tbody class="table table-bordered table-striped">
#foreach($user as $task)
<tr class="form-group">
{{-- <form action="{{ route('amber.timestone.home.start') }}" method="POST" class="align-center"> --}}
{{csrf_field()}}
<td class="d-flex">
<input type="hidden" name="id" id="hidden-id_{{$task->id}}" value="{{ $task->id }}">
<button type="button" id="str-btn" data-id="{{$task->id}}" class="btn btn-primary"
#if(!empty($task->start))
disabled
#elseif(!empty($task->duration))
readonly
#endif
>Start</button>
{{-- <span class="btn btn-primary" onclick="myAlert({{$task->id}})">Test</span> --}}
</td>
{{-- </form> --}}
<form action="{{ route('amber.timestone.home.end', $task->id) }}" method="POST">
{{csrf_field()}}
<td>
<input class="my-2" type="text" id="ref_{{$task->id}}" name="ref" value="{{ $task->ref }}"
#if(empty($task->start))
readonly
#elseif(!empty($task->duration))
readonly
#endif
>
{{-- <input class="my-2" type="hidden" name="ref" value="{{ $task->ref }}"> --}}
</td>
<td>
<textarea name="remarks" id="remarks_{{$task->id}}" cols="30" rows="4"
#if(empty($task->start))
readonly
#elseif(!empty($task->duration))
readonly
#endif
>{{ $task->remarks }}</textarea>
</td>
<td>
<select id="campaign_{{$task->id}}" class="custom-select custom-select-lg mb-3" name="campaign"
#if(empty($task->start))
disabled
#elseif(!empty($task->duration))
disabled
#endif
>
<option #if($task->campaign == 'cert') selected #endif value="cert">Cert Focus</option>
<option #if($task->campaign == 'omni') selected #endif value="omni">Omni</option>
<option #if($task->campaign == 'textblast') selected #endif value="textblast">TextBlast</option>
</select>
</td>
<td>
<select id="type_{{$task->id}}" class="custom-select custom-select-lg mb-3" name="type"
#if(empty($task->start))
disabled
#elseif(!empty($task->duration))
disabled
#endif
>
<option #if($task->type == 'cert') selected #endif value="cert">Cert</option>
<option #if($task->type == 'email') selected #endif value="email">Email Support</option>
<option #if($task->type == 'chat') selected #endif value="chat">Chat Support</option>
</select>
</td>
<td>
<p id="startTime"></p>
{{ $task->start }}
</td>
<td>
{{ $task->end }}
</td>
<td>
{{ $task->duration }}
</td>
<td class="d-flex">
<input type="hidden" name="id" value="{{ $task->id }}">
<button type="submit" id="end-btn_{{$task->id}}" class="btn btn-danger float-right ml-3"
#if(!empty($task->end))
disabled
#elseif(empty($task->start))
disabled
#endif
>End</button>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
#endsection
#section('reporter')
<script>
$(document).ready(function() {
$('#report').DataTable( {
"bLengthChange": false,
"lengthMenu": [ 3, 10 ],
searching: false
});
$('#str-btn').click(function(e){
var id = $(this).data('id');
$.ajax({
url: "{{ url('amber/timestone/start') }}"+'/'+id,
type: "GET",
success: function(data){
console.log(data)
$("#startTime").text(data.start)
$("#end-btn_"+id).removeAttr("disabled");
$("#ref_"+id).removeAttr("readonly");
$("#remarks_"+id).removeAttr("readonly");
$("#campaign_"+id).removeAttr("disabled");
$("#type_"+id).removeAttr("disabled");
$("#str-btn").Attr("disabled");
}
})
$(this).prop("disabled", true);
});
});
You can get your form and add listener on submit, then you can call to event.preventDefault() to avoid refreshing.
Would be something like this:
<form action="{{ route('amber.timestone.home.start') }}" method="POST" class=" form-data-table align-center">
...
Javascript part:
$('.form-data-table').on('submit', function(event){
event.preventDefault();
... remaining code
})
I am trying to create an update page and i've completed most of the page, with exception to the checkbox section. For some reason that i've yet to figure out, the checkboxes are duplicated. I am using laravel.
This is the code for that particular section of the form.
<div class="form-group">
<label>Focus Area</label>
<br>
#foreach(FocusArea::all() as $focusArea)
#if(isset($project))
<div class="checkbox material checkbox-success">
<label>
#foreach($project->getIdsOfFocusAreas() as $selectedFocusArea)
#if($selectedFocusArea == $focusArea->focus_area_id)
<input type="checkbox" name="focus-area[]" value="{{ $selectedFocusArea }}" checked>
#else
<input type="checkbox" name="focus-area[]" value="{{ $selectedFocusArea }}">
#endif
#endforeach
{{ $focusArea->name }}
</label>
</div>
<br>
#endif
#endforeach
</div>
Some extra information:
The number of elements in the array generated by FocusArea::all() is 5.
The number of elements in the array generated by getIdsOfFocusAreas() is 2.
I know that it is duplicating twice because of point number 2, im just not exactly sure why it is duplicating in the first place.
Try this:
<div class="form-group">
<label>Focus Area</label>
<br>
<?php $selectedFlug = 0; ?>
#foreach(FocusArea::all() as $focusArea)
#if(isset($project))
<div class="checkbox material checkbox-success">
<label>
#foreach($project->getIdsOfFocusAreas() as $selectedFocusArea)
#if($selectedFocusArea == $focusArea->focus_area_id)
<input type="checkbox" name="focus-area[]" value="{{ $selectedFocusArea }}" {{ ($selectedFocusArea == $focusArea->focus_area_id) ? 'checked' : '' }} >
<?php $selectedFlug = 1; ?>
#break
#else
<?php $selectedFlug = 0; ?>
#endif
#endforeach
#if($selectedFlug == 0)
<input type="checkbox" name="focus-area[]" value="{{ $focusArea->focus_area_id }}">
#endif
{{ $focusArea->name }}
</label>
</div>
<br>
#endif
#endforeach
</div>