anybodys guess!
Wrong output in query for id
Hi everyone,
I have problem with one query. They give me wrong output of cell. Instead column of "id" ,they put me column "name"? what am I doing wrong? thanks!
$task = Task::find($id);
$samples = Sample::find($id);
$methods = DB::table('methods')
->join('analysis','methods.method','=','analysis.id')
->join('technicians','methods.name','=','technicians.id')
->where('methods.task_id','=',$url)
->orderBy('methods.id', 'ASC')
->get();
foreach($methods as $method)
<tr>
<td>{{$method->name_of_analyze}}</td>
<td>{{$method->name}}</td>
<td style="text-align:center">{{$method->nmbr_of_analysis}}</td>
<td>
<a href="{{url('methods')}}/{{$method->id}}/edit" class="btn btn-default center-block">Uredi</a>
</td>
</tr>
@endforeach
</table>
When all else fail https://laracasts.com/series/laravel-from-scratch-2018
Controller function use as like below, findOrFail instead find and where are you getting $url in your query?,
$task = Task::findOrFail($id);
$samples = Sample::findOrFail($id);
$methods = DB::table('methods')
->join('analysis','methods.method','=','analysis.id')
->join('technicians','methods.name','=','technicians.id')
->where('methods.task_id','=',$url)
->orderBy('methods.id', 'ASC')
->get();
I don't understand your query?
You have a column on methods table called method which contains the key for the analysis table?
You have a column on methods table called name which contains the key for the technicians table?
and you have something in $url that needs to match task_id ?
Your structure and naming seem all off - making it very difficult to understand what you are trying to do.
One thing to be aware of, when you join tables, if there are conflicting column names then only one table can win
Hi @munazzil and @snapey $url it' $task->id, i know that's wrong approach. I corrected it. I did not want to put the whole controller because it is big but here it may be clearer. Everything it's ok but $method->id on last foreach it's wrong. They put the $method->name istead $method->id ?
This is my Controller of Report:
public function report($id)
{
$task = Task::find($id);
$samples= DB::table('tasks')
->join('samples','tasks.id','=','samples.task_id')
->where('samples.task_id','=',$task->id )
->get();
$methodss = DB::table('methods')
->join('analysis','methods.method','=','analysis.id')
->join('technicians','methods.name','=','technicians.id')
->where('methods.task_id','=',$task->id )
//->orderBy('methods.id', 'ASC')
->get();
$technicians = Technician::all()->pluck('name');
$technicians->prepend('Izaberite');
$methods = Analysis::all()->pluck('name_of_analyze');
$methods->prepend('Izaberite');
$plocica_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('plocica');
$pl_busenje_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('pl_busenje');
$preparata_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('preparata');
$pr_sep_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('pr_sep');
$prep_sep_90_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('prep_sep_90');
$prep_sep_125_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('prep_sep_125');
$prerez_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('prerez');
$nabrusak_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('nabrusak');
$poliranje_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('poliranje');
$gran_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('gran');
$priprema2_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('priprema2');
$t_l_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('t_l');
$priprema3_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('priprema3');
$net_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('net');
$priprema4_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('priprema4');
$slem_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('slem');
$m_slem_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('m_slem');
$palino_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('palino');
$nano_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('nano');
$radiolarija_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('radiolarija');
$priprema5_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('priprema5');
$sfz_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('sfz');
$pr_kem_inoz_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('pr_kem_inoz');
$pr_kem_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('pr_kem');
$kalc_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('kalc');
$cao_mgo_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('cao_mgo');
$skr_sil_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('skr_sil');
$komp_sil_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('komp_sil');
$aas_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('aas');
$rtg_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('rtg');
$ph_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('ph');
$naparivanje_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('naparivanje');
$sem_eds_sum = DB::table('samples')->where('task_id','=', $task->id)->sum('sem_eds');
$countrep = DB::table('methods')->where('task_id','=',$task->id)->sum('nmbr_of_analysis');
$count = $plocica_sum+$pl_busenje_sum+$preparata_sum+$pr_sep_sum+$prep_sep_90_sum+$prep_sep_125_sum+$prerez_sum+$nabrusak_sum+$poliranje_sum+$gran_sum+$priprema2_sum+
$t_l_sum+$priprema3_sum+$net_sum+$priprema4_sum+$slem_sum+$m_slem_sum+$palino_sum+$nano_sum+$radiolarija_sum+$priprema5_sum+$sfz_sum+$pr_kem_inoz_sum+$pr_kem_sum+
$kalc_sum+$cao_mgo_sum+$skr_sil_sum+$komp_sil_sum+$aas_sum+$rtg_sum+$ph_sum+$naparivanje_sum+$sem_eds_sum;
return view('tasks.report')->with('task', $task)
->with('samples', $task->samples)
->with('methodss',$methodss)
->with('technicians',$technicians)
->with('methods',$methods)
->with('plocica_sum',$plocica_sum)
->with('pl_busenje_sum',$pl_busenje_sum)
->with('preparata_sum',$preparata_sum)
->with('pr_sep_sum',$pr_sep_sum)
->with('prep_sep_90_sum',$prep_sep_90_sum)
->with('prep_sep_125_sum',$prep_sep_125_sum)
->with('prerez_sum',$prerez_sum)
->with('nabrusak_sum',$nabrusak_sum)
->with('poliranje_sum',$poliranje_sum)
->with('gran_sum',$gran_sum)
->with('priprema2_sum',$priprema2_sum)
->with('t_l_sum',$t_l_sum)
->with('priprema3_sum',$priprema3_sum)
->with('net_sum',$net_sum)
->with('priprema4_sum',$priprema4_sum)
->with('slem_sum',$slem_sum)
->with('m_slem_sum',$m_slem_sum)
->with('palino_sum',$palino_sum)
->with('nano_sum',$nano_sum)
->with('radiolarija_sum',$radiolarija_sum)
->with('priprema5_sum',$priprema5_sum)
->with('sfz_sum',$sfz_sum)
->with('pr_kem_inoz_sum',$pr_kem_inoz_sum)
->with('pr_kem_sum',$pr_kem_sum)
->with('kalc_sum',$kalc_sum)
->with('cao_mgo_sum',$cao_mgo_sum)
->with('skr_sil_sum',$skr_sil_sum)
->with('komp_sil_sum',$komp_sil_sum)
->with('aas_sum',$aas_sum)
->with('rtg_sum',$rtg_sum)
->with('ph_sum',$ph_sum)
->with('naparivanje_sum',$naparivanje_sum)
->with('sem_eds_sum',$sem_eds_sum)
->with('countrep',$countrep)
->with('count',$count)
;
}
}
and this is my view report.blade.php
@extends('layouts.app3')
@section('content')
<?php
use App\Task;
use App\Method;
use App\Analysis;
use App\Sample;
use App\User;
use App\Technician;
use App\Http\Controllers\TasksController;
use Illuminate\Support\Facades\Input;
?>
@if(Auth::User()->type_of_user == 1 or Auth::User()->type_of_user == 2 or Auth::User()->type_of_user == 3)
<a href="{{url('tasks')}}/{{$task->id}} " class="btn btn-default" style="margin-left:10%">Natrag</a> <br><br>
<h3 style="margin-left:10%">Zadatak: {{$task->naziv_zadatka}}</h3>
<h3 style="margin-left:10%">Nositelj zadatka: {{$task->nositelj_zadatka}}</h3>
<hr>
<table class="table-bordered table-hover" style="table-layout:fixed ;
width: 90%;margin-left:5%;
">
<thead>
<tr>
<th></th>
<th style="width: 20%" colspan="3" ><small >Ukupno (zbroj svih stranica)</small></th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('plocica')}}</th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('pl_busenje')}}</th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('preparata')}}</th>
@if(Auth::User()->type_of_user != 2)
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('pr_sep')}}</th>
@endif
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('prep_sep_90')}}</th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('prep_sep_125')}} </th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('prerez')}} </th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('nabrusak')}} </th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('poliranje')}} </th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('gran')}}</th>
@if(Auth::User()->type_of_user != 2)
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('priprema2')}}</th>
@endif
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('t_l')}}</th>
@if(Auth::User()->type_of_user != 2)
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('priprema3')}}</th>
@endif
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('net')}}</th>
@if(Auth::User()->type_of_user != 2)
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('priprema4')}}</th>
@endif
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('slem')}} </th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('m_slem')}} </th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('palino')}}</th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('nano')}}</th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('radiolarija')}}</th>
@if(Auth::User()->type_of_user != 2)
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('priprema5')}}</th>
@endif
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('sfz')}}</th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('pr_kem_inoz')}}</th>
@if(Auth::User()->type_of_user != 2)
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('pr_kem')}}</th>
@endif
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('kalc')}}</th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('cao_mgo')}}</th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('skr_sil')}}</th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('komp_sil')}}</th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('aas')}}</th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('rtg')}}</th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('ph')}}</th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('naparivanje')}}</th>
<th>{{DB::table('samples')->where('task_id','=', $task->id)->sum('sem_eds')}}</th>
<th>{{$count}} </th>
</tr>
<!-- ako je korisnik 2 (nositelj zadatka) ne može vidjeti tko je pripremao metode -->
<tr>
<th hidden >Id uzorka</th>
<th style="text-align:center">Red.<br>br.</th>
<th style="text-align:center">Oznaka uzorka</th>
<th style= "text-align:center">Koordinata x</th>
<th style= "text-align:center">Koordinata y</th>
<!-- priprema-->
<th class="thsample">Pločice</th>
<th class="thsample">Pločica + brušenje</th>
<th class="thsample">Preparat</th>
@if(Auth::User()->type_of_user != 2)
<!-- priprema 2 -->
<th class="thsample">Prip.separ.</th>
@endif
<th class="thsample">Preparat sep(0,90)</th>
<th class="thsample">Preparat sep(1,25)</th>
<th class="thsample">Prerez</th>
<th class="thsample">Nabrusak</th>
<th class="thsample">Poliranje</th>
<th class="thsample">Gran.</th>
@if(Auth::User()->type_of_user != 2)
<th class="thsample">Priprema T+L</th>
@endif
<th class="thsample">T+L</th>
@if(Auth::User()->type_of_user != 2)
<th class="thsample">Priprema+Net</th>
@endif
<th class="thsample">Net.</th>
@if(Auth::User()->type_of_user != 2)
<th class="thsample">Priprema Šlem</th>
@endif
<th class="thsample">Šlem</th>
<th class="thsample">Makro šlem</th>
<th class="thsample">Palino</th>
<th class="thsample">Nano</th>
<th class="thsample">Radiolarije</th>
@if(Auth::User()->type_of_user != 2)
<th class="thsample">Priprema SFZ</th>
@endif
<th class="thsample">SFZ</th>
<th class="thsample">Kem. analiza (inoz.)</th>
@if(Auth::User()->type_of_user != 2)
<th class="thsample">Prip.kem.</th>
@endif
<th class="thsample">Kalc.</th>
<th class="thsample">Cao MgO</th>
<th class="thsample">Skr.sil.</th>
<th class="thsample">Komp.sil.</th>
<th class="thsample">AAS</th>
<th class="thsample">Rtg</th>
<th class="thsample">pH</th>
<th class="thsample">Naparivanje</th>
<th class="thsample">SEM-EDS</th>
</tr>
</thead>
<tbody>
@foreach($samples as $sample)
<tr>
<td hidden >{{$sample->id}}</td>
<td style="text-align:center" >{{$sample->rb}}</td>
@if(Auth::User()->type_of_user != 2)
<td class="tdreport">{{--<a href="{{url('samples')}}/{{$sample->id}}"</a>--}}<strong>{{$sample->oznaka_uzorka}}</strong></td>
@endif
@if(Auth::User()->type_of_user == 2)
<td class="tdreport">{{$sample->oznaka_uzorka}}</a></td>
@endif
<td class="tdreport">{{$sample->koordinata_x}}</td>
<td class="tdreport" >{{$sample->koordinata_y}}</td>
<td class="tdreport" >{{$sample->plocica}}</td>
<td class="tdreport" >{{$sample->pl_busenje}}</td>
<td class="tdreport" >{{$sample->preparata}}</td>
@if(Auth::User()->type_of_user != 2)
<td class="tdreport">{{$sample->pr_sep}}</td>
@endif
<td class="tdreport" >{{$sample->prep_sep_90}}</td>
<td class="tdreport" >{{$sample->prep_sep_125}}</td>
<td class="tdreport" >{{$sample->prerez}}</td>
<td class="tdreport" >{{$sample->nabrusak}}</td>
<td class="tdreport" >{{$sample->poliranje}}</td>
<td class="tdreport" >{{$sample->gran}}</td>
@if(Auth::User()->type_of_user != 2)
<td class="tdreport" >{{$sample->priprema2}} </td>
@endif
<td class="tdreport" >{{$sample->t_l}}</td>
@if(Auth::User()->type_of_user != 2)
<td class="tdreport" >{{$sample->priprema3}}</td>
@endif
<td class="tdreport" >{{$sample->net}}</td>
@if(Auth::User()->type_of_user != 2)
<td class="tdreport" >{{$sample->priprema4}}</td>
@endif
<td class="tdreport" >{{$sample->slem}}</td>
<td class="tdreport" >{{$sample->m_slem}}</td>
<td class="tdreport" >{{$sample->palino}}</td>
<td class="tdreport" >{{$sample->nano}}</td>
<td class="tdreport" >{{$sample->radiolarija}}</td>
@if(Auth::User()->type_of_user != 2)
<td class="tdreport" >{{$sample->priprema5}}</td>
@endif
<td class="tdreport" >{{$sample->sfz}}</td>
<td class="tdreport" >{{$sample->pr_kem_inoz}}</td>
@if(Auth::User()->type_of_user != 2)
<td class="tdreport" >{{$sample->pr_kem}}</td>
@endif
<td class="tdreport" >{{$sample->kalc}}</td>
<td class="tdreport" >{{$sample->cao_mgo}}</td>
<td class="tdreport" >{{$sample->skr_sil}}
<td class="tdreport" >{{$sample->komp_sil}}
<td class="tdreport" >{{$sample->aas}}</td>
<td class="tdreport" >{{$sample->rtg}}</td>
<td class="tdreport" >{{$sample->ph}}</td>
<td class="tdreport" >{{$sample->naparivanje}}</td>
<td class="tdreport" >{{$sample->sem_eds}}</td>
</tr>
@endforeach
</tbody>
</table>
<br>
<hr>
@endif
@if(Auth::User()->type_of_user == 1 or Auth::User()->type_of_user == 3 && $task->status_gotovo == null)
{!! Form::open(['action'=>'MethodsController@store','method'=>'POST']) !!}
<table border="3" width="30%" align="center">
<tr>
<th hidden style="width:2%;text-align:center;padding: 10px;"> Id zadatka </th>
<th style="width:2%;text-align:center;padding: 10px;">Metode pripreme/analize</th>
<th nowrap style="width:2%;text-align:center;padding: 10px;">Ime i prezime</th>
<th style="width:1%;text-align:center;padding: 0px;">Broj uzoraka</th> </tr>
<tr>
<td hidden> {{Form::hidden('task_id',$task->id)}}</td>
<td style="text-align:center">{{Form::select('method',$methods,['class'=>'form-control'])}}</td>
<td style="text-align:center">{{Form::select('name',$technicians,['class'=>'form-control', ])}}</td>
<td>{{Form::text('nmbr_of_analysis','',['class'=>'form-control',"input type"=>"number"])}}</td>
</tr>
</table>
<br>
{{Form::submit('Unesi', ['class' => 'btn btn-primary','style'=>'margin-left:61.6%',])}}
{!! Form::close() !!}
<br> <br>
@endif
<h4 style="margin-left:42%">Izvještaj rada djelatnika laboratorija </h4>
<table border="3" width="30%" align="center">
<tr>
<th style="width:2%;text-align:center;padding: 10px;">Metode pripreme,analize</th>
<th nowrap style="width:2%;text-align:center;padding: 10px;">Ime i prezime</th>
<th style="width:1%;text-align:center;padding: 0px;text-">Broj uzoraka</th>
<th style="width:1%;text-align:center;padding: 0px;text-">Uredi</th>
</tr>
@foreach($methodss as $method)
<tr>
<td>{{$method->name_of_analyze}}</td>
<td>{{$method->name}}</td>
<td style="text-align:center">{{$method->nmbr_of_analysis}}</td>
<td><a href="{{url('methods')}}/{{$method->id}}/edit" class="btn btn-default center-block">Uredi</a></td>
</tr>
@endforeach
</table>
<br>
<a href="{{url('methods')}}/{{$task->id}}/print" class="btn btn-success"style="margin-left:62.1%;margin-top:0%">Ispis</a>
<hr>
<hr>
@endsection
Wow
You need to seriously have a word with yourself (assuming its your code). All those repeated queries, each one summing a different value!
In view is wrong, this is old code.
view: report.blade.php
<table class="table-bordered table-hover" style="table-layout:fixed ;
width: 90%;margin-left:5%;
">
<thead>
<tr>
<th></th>
<th style="width: 20%" colspan="3" ><small >Ukupno (zbroj svih stranica)</small></th>
<th>{{$plocica_sum}}</th>
<th>{{$pl_busenje_sum}} </th>
<th>{{$preparata_sum}}</th>
@if(Auth::User()->type_of_user != 2)
<th>{{$pr_sep_sum}}</th>
@endif
<th>{{$prep_sep_90_sum}}</th>
<th>{{$prep_sep_125_sum}}</th>
<th>{{$prerez_sum }} </th>
<th>{{$nabrusak_sum}} </th>
<th>{{$poliranje_sum }} </th>
<th>{{$gran_sum}}</th>
@if(Auth::User()->type_of_user != 2) etc.....
I need a lot of summing because I need to know for each item what its sum is... picture of table: https://ibb.co/HqvrC2P .... sorry for my very bad code. I'm very new in this. I know it's bad for you to see it all :)
You can create a single query that sums all the columns and returns a single result set that you can pass to the view.
Please or to participate in this conversation.