Laravel Blade echo and variables - laravel

I'm trying to understand Laravel, but i'm stuck
When I try this code:
#foreach($nerds as $key =>$value)
<tr>
<td>{{ $value->id }}</td>
<td>{{ $value->name }}</td>
<td>{{ $value->email }}</td>
<td>{{ $value->nerd_level }}</td>
</tr>
#endforeach
it displays litterally "{{ $value->id }}" etc in a list on the site.
Anyone?

Related

Api Pagination Call to undefined method stdClass::links() on laravel 8

hello can you help me? so i am paginating on server side, but when i try to display on client side i get error like this...
i can say I can't use firstItem(), Thanks for your response, I hope this gets the answer..
In Controller
$response = Http::withHeaders([
'X-Authorization' => 'NkKeAs7Og8O6WcNste9OsAjkui2pSZnjZr6WOcDf1EjU1UDPuz88CATNqFI2rkSN'
])->get('http://www.Api.net/api/get-produk', [
'type_product' => $filter,
'limit' => $request->length,
'offset' => $request->start,
]);
$data = json_decode($response);
In Rest API
$produk = Daftarproduk::select('cat_number', 'brand', 'nama_produk', 'host', 'reactivity', 'clone_type', 'application', 'pack_size', 'type_product', 'pack_category', 'price')
->where('type_product', $request->get('type_product'))
->offset($request->offset)
->limit($request->limit)
->paginate();
In View
<tbody>
#foreach ($produk->data as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $item->cat_number }}</td>
<td>{{ $item->brand }}</td>
<td>{{ $item->nama_produk }}</td>
<td>{{ $item->host }}</td>
<td>{{ $item->reactivity }}</td>
<td>{{ $item->clone_type }}</td>
<td>{{ $item->application }}</td>
<td>{{ $item->pack_size }}</td>
<td>{{ $item->type_product }}</td>
<td>{{ $item->price }}</td>
</tr>
#endforeach
</tbody>
</table>
<div class="d-flex justify-content-start">
Showing
{{ $produk->firstItem() }}
to
{{ $produk->lastItem() }}
of
{{ $produk->total() }}
Entries
</div>
<div class="d-flex justify-content-end">
{{ $produk->links() }}
</div>

Laravel 5.8 - Invalid argument supplied for foreach()?

I tried to display data inside a table for admin.
Here is my admin.blade.php:
<form method="post" action="post_namaadmin">
<input type="text" name="txtnamaadmin" id="txtnamadmin" value="1">
<input type="submit" name="btnnamadmin" id="btnamaadmin">
</form>
<div class="card-body">
<table border="1">
#foreach($datalama as $rows)
<tr>
<td>ID : </td>
<td>{{ $rows->userid }}</td>
<td>Nama : </td>
<td>{{ $rows->nama }}</td>
</tr>
<tr>
<td>Section</td>
<td>IM</td>
<td>CO</td>
<td>SH</td>
<td>PL</td>
<td>RI</td>
<td>ME</td>
<td>TB</td>
<td>CF</td>
</tr>
<tr>
<!-- //1 -->
<td>{{ $rows->a1 }}</td>
<td>{{ $rows->b1 }}</td>
<td>{{ $rows->c1 }}</td>
<td>{{ $rows->d1 }}</td>
<td>{{ $rows->f1 }}</td>
<td>{{ $rows->g1 }}</td>
<td>{{ $rows->h1 }}</td>
<!-- //1 -->
<td>{{ $rows->a2 }}</td>
<td>{{ $rows->b2 }}</td>
<td>{{ $rows->c2 }}</td>
<td>{{ $rows->d2 }}</td>
<td>{{ $rows->f2 }}</td>
<td>{{ $rows->g2 }}</td>
<td>{{ $rows->h2 }}</td>
<!-- //3 -->
<td>{{ $rows->a3 }}</td>
<td>{{ $rows->b3 }}</td>
<td>{{ $rows->c3 }}</td>
<td>{{ $rows->d3 }}</td>
<td>{{ $rows->f3 }}</td>
<td>{{ $rows->g3 }}</td>
<td>{{ $rows->h3 }}</td>
<!-- //4 -->
<td>{{ $rows->a4 }}</td>
<td>{{ $rows->b4 }}</td>
<td>{{ $rows->c4 }}</td>
<td>{{ $rows->d4 }}</td>
<td>{{ $rows->f4 }}</td>
<td>{{ $rows->g4 }}</td>
<td>{{ $rows->h4 }}</td>
<!-- //5 -->
<td>{{ $rows->a5 }}</td>
<td>{{ $rows->b5 }}</td>
<td>{{ $rows->c5 }}</td>
<td>{{ $rows->d5 }}</td>
<td>{{ $rows->f5 }}</td>
<td>{{ $rows->g5 }}</td>
<td>{{ $rows->h5 }}</td>
<!-- //6 -->
<td>{{ $rows->a6 }}</td>
<td>{{ $rows->b6 }}</td>
<td>{{ $rows->c6 }}</td>
<td>{{ $rows->d6 }}</td>
<td>{{ $rows->f6 }}</td>
<td>{{ $rows->g6 }}</td>
<td>{{ $rows->h6 }}</td>
<!-- //7 -->
<td>{{ $rows->a7 }}</td>
<td>{{ $rows->b7 }}</td>
<td>{{ $rows->c7 }}</td>
<td>{{ $rows->d7 }}</td>
<td>{{ $rows->f7 }}</td>
<td>{{ $rows->g7 }}</td>
<td>{{ $rows->h7 }}</td>
</tr>
#endforeach
</table>
</div>
The code is not yet completed, but it should give a rough picture of the table.
Here is my route in web.php:
Route::post('/post_namaadmin', 'DataController#admin' ) ->name ('postnamaadmin');
Function in DataController.php :
public function admin(Request $request){
$mk = new data();
$param['datalama'] = $mk->ambilData($request->txtnamaadmin);
return view('admin',$param);
}
Function in data class :
public function ambilData($nama){
$query = data::orderby('userid')->where('nama','=' ,'$nama')->first();
return $query;
}
Here is the error:
> ErrorException {#230 ▼ #message: "Invalid argument supplied for
> foreach()" #code: 0 #file:
> "C:\xampp\htdocs\kuesioner1\storage\framework\views\5c1b0932d9bc5a402e110419f0ce7e166c7a2109.php"
> #line: 22 #severity: E_WARNING } 1
I'm new in Laravel. For why I use 5.8, my superior demand me so. All help and suggestion is highly appreciated.
When you try to dd($param['datalama']), you will get null, because your query found nothing, using ->where('nama','=' ,'$nama').
You need to change data class ambilData method
public function ambilData($nama){
return data::orderby('userid')
->where('nama', $nama)
->get();
}
I think you should put $param['datalama'] in the foreach instead
and change the view function to view('admin',compact('param'))
and in the form tag you need to change the action to
<form action="{{route('postnamaadmin')}}">

Laravel Foreach Looping

I need to display the previous value of the iterated array, here is code
<tr>
<th></th>
<th>Customer ID</th>
<th>Customer Name</th>
<th>Delivery Address</th>
<th>Contact No.</th>
<th>Zip Code</th>
<th>Payment Terms</th>
<th>Credit Limit</th>
</tr>
<?php $previousValue = null; ?>
#foreach($customer_count as $key => $value)
<tr>
<td>{{$previousValue=$value->customer_name }}</td>
<td>{{ $value->id }}</td>
<td>{{ $value->customer_name }}</td>
<td>{{ $value->delivery_address }}</td>
<td>{{ $value->contact_number }}</td>
<td>{{ $value->area_id }}</td>
<td>{{ $value->payment_term_id }}</td>
<td>{{ $value->credit_limit }}</td>
</tr>
#endforeach
I need to display the previous name of Customer in the iteration?
Any idea on how to do this?
Set value at the end , I set blank for first
#foreach($customer_count as $key => $value)
<tr>
<td>{{$previousValue or ""}}</td>
.....
.....
</tr>
<?php $previousValue = $value->customer_name ?>
#endforeach
$customer_count[$key - 1]
Of course you'll need to check if that exists or use the null-coalesce operator or something, so:
#php
$previous = $customer_count[$key - 1] ?? false;
#endphp
Then use:
#if( $previous )
... some stuff ...
#endif
wherever you need to inject.
please try this
<td>{{ (!$loop->first) ? $customer_count[$key - 1]->customer_name : '' }}</td>
You can read more about foreach loop here.
At very first set the previous value as blank and then at the bottom set value in $previousValue
<?php $previousValue = '';?>
#foreach($customer_count as $key => $value)
<tr>
<td>{{ $previousValue }}</td>
<td>{{ $value->id }}</td>
<td>{{ $value->customer_name }}</td>
<td>{{ $value->delivery_address }}</td>
<td>{{ $value->contact_number }}</td>
<td>{{ $value->area_id }}</td>
<td>{{ $value->payment_term_id }}</td>
<td>{{ $value->credit_limit }}</td>
</tr>
<?php $previousValue = $value->customer_name; ?>
#endforeach

Laravel Resource controller not getting delete route on amchor click

In laravel, I have resource controller :
Route::resource('/student','StudentController');
here is my view listing code :
<tr>
<td>{{ $key + $models->firstItem() }}</td>
<td>{{ $model->name }}</td>
<td>{{ $model->email }}</td>
<td>{{ $model->roll_no }}</td>
<td>{{ $model->StudentDetail->phone }}</td>
<td>{{ $model->StudentDetail->course }}</td>
<td>{{ $model->StudentDetail->city }}</td>
<td>{{ $model->StudentDetail->state->state_name }}</td>
<td>
Edit |
Link
</td>
</tr>
conytroller function i have :
public function destroy($id)
{
echo "ok";
}
My problem is that when I click on anchor button(delete ) it not
found the resource route for the delete what I am doing wrong here can
anyone please help me elated this.
when I click on the delete anchor it calls the show function of resource but I want the destroy function to call on click.
destroy function use DELETE method
in your blade view
#foreach($students as $model)
<tr>
<td>{{ $key + $models->firstItem() }}</td>
<td>{{ $model->name }}</td>
<td>{{ $model->email }}</td>
<td>{{ $model->roll_no }}</td>
<td>{{ $model->StudentDetail->phone }}</td>
<td>{{ $model->StudentDetail->course }}</td>
<td>{{ $model->StudentDetail->city }}</td>
<td>{{ $model->StudentDetail->state->state_name }}</td>
<td>
Edit |
DELETE
<form id="delete_form" style="display:none" action="{{ route('student.destroy',$model->id)}}" method="POST" >
{{ csrf_field() }}
<input type="hidden" name="_method" value="delete" />
</form>
</td>
</tr>
#endforeach

Laravel 4 pagination - Can't get past page 1

I have created somewhat of a search feature, where a user can choose a type, pool, date and time. I can't seem to make the paginator to work, as when I click on page 2 the results disappear.
I have tried to append the $query->links() function, but none of my tries have yet succeeded.
The URL looks like this: http://localhost:8080/nih/public/bassengweb/data?time_maling=1&pool_id=1&fraDato=14%2F04%2F2014&tilDato=22%2F04%2F2014&fraTid=00%3A01&tilTid=23%3A59
The code for the view file:
#extends('default')
#section('content')
<h1>ADMIN SØK</h1>
{{ Form::open(array('url' => 'bassengweb/data', 'method' => 'GET')) }}
<table>
<tr>
<td>{{ Form::label('time_maling', 'Timemåling') }}</td>
<td>{{ Form::checkbox('time_maling', 1) }}</td>
<td>{{ Form::label('3_time_maling', '3. Timemåling') }}</td>
<td>{{ Form::checkbox('3_time_maling', 1) }}</td>
<td>{{ Form::label('oppgaver', 'Oppgaver') }}</td>
<td>{{ Form::checkbox('oppgaver', 1) }}</td>
<td>{{ Form::label('pool_id', 'Basseng') }}</td>
<td>{{ Form::select('pool_id', $pools, Input::old('pool_id')) }}</td>
</tr>
<tr>
<td>{{ Form::label('dagsoppg', 'Dagvakt') }}</td>
<td>{{ Form::checkbox('dagsoppg', 1) }}</td>
<td>{{ Form::label('kveldsoppg', 'Kveldsvakt') }}</td>
<td>{{ Form::checkbox('kveldsoppg', 1) }}</td>
<td>{{ Form::label('helgoppg', 'Helgevakt') }}</td>
<td>{{ Form::checkbox('helgsoppg', 1) }}</td>
</tr>
<tr>
<td>{{ Form::label('kontcm', 'Kontroll CM') }}</td>
<td>{{ Form::checkbox('kontcm', 1) }}</td>
<td>{{ Form::label('arduino', 'Arduino') }}</td>
<td>{{ Form::checkbox('arduino', 1) }}</td>
<td><hr/></td>
</tr>
<tr>
<td>{{ Form::label('fraDato', 'Fra dato: ') }}</td>
<td>{{ Form::text('fraDato', Input::old('fraDato'), array('placeholder' => $date)) }}</td>
<td>{{ Form::label('tilDato', 'Til dato: ') }}</td>
<td>{{ Form::text('tilDato', Input::old('tilDato'), array('placeholder' => $date)) }}</td>
</tr>
<tr>
<td>{{ Form::label('fraTid', 'Fra klokka: ') }}</td>
<td>{{ Form::text('fraTid', Input::old('fraTid'), array('placeholder' => '10:55:31')) }}</td>
<td>{{ Form::label('tilTid', 'Til klokka: ') }}</td>
<td>{{ Form::text('tilTid', Input::old('tilTid'), array('placeholder' => '10:55:31')) }}</td>
<td>{{ Form::submit('Søk') }}<td>
</tr>
</table>
{{ Form::close() }}
#if(isset($data))
<table class='table table-hover'>
<tr>
<td>ID</td>
<td>Tittel</td>
<td>Verdi</td>
<td>Ansatt ID</td>
<td>Dato</td>
<td>Tid</td>
#if($type === 'measurement')
<td>Basseng</td>
</tr>
#foreach($data as $dataItem)
<tr>
<td>{{ $dataItem->id }}</td>
<td>{{ $dataItem->measurements[0]->title }}</td>
<td>{{ $dataItem->value }}</td>
<td>{{ $dataItem->emps->user_name }}</td>
<td>{{ date('d/m/Y', strtotime($dataItem->date)) }}</td>
<td>{{ $dataItem->time }}</td>
#foreach($dataItem->measurements as $measurement)
<td>{{ $pools[$measurement->pivot->pool_id] }}</td>
#endforeach
<td>{{ HTML::linkRoute('edit_data', 'Rediger', $dataItem->id) }}</td>
<td>{{ HTML::linkRoute('delete_confirm', 'Slett', $dataItem->id) }}
</td>
</tr>
#endforeach
</table>
{{ $data->appends(array($type => 1, Input::except('page')))->links() }}
#elseif($type === 'task')
</tr>
#foreach($data as $dataItem)
<tr>
<td>{{ $dataItem->id }}</td>
<td>{{ $dataItem->tasks[0]->title }}</td>
<td>{{ $dataItem->value }}</td>
<td>{{ $dataItem->emps->user_name }}</td>
<td>{{ date('d/m/Y', strtotime($dataItem->date)) }}</td>
<td>{{ $dataItem->time }}</td>
<td>{{ HTML::linkRoute('edit_data', 'Rediger', $dataItem->id) }}</td>
<td>{{ HTML::linkRoute('delete_confirm', 'Slett', $dataItem->id) }}</td>
</tr>
#endforeach
#endif
#endif
#stop
Thanks.
You nested array withing appends method:
$data->appends(array($type => 1, Input::except('page')))->links()
// returns
array(
$type => 1,
array(
... // Input/Request params are ignored as a nested array
)
)
Change it to:
$data->appends(array_merge(array($type => 1), Input::except('page')))->links()

Resources