Trying to get property 'rute_asal' of non-object on Laravel 8 - php

i just get error "Trying to get property 'rute_asal' of non-object". before i ask this queston, i tried so many things but not worked (i tried the similiar question by others too).
so, this my my code
Controller
$biaya_pengantaran = BiayaPengantaran::latest()->get();
$biaya_pelayaran = BiayaPelayaran::latest()->get();
$detail_transaksi = DetailTransaksiPengiriman::where('noinvoice', $noinvoice)->get();
return view('pengiriman.create', compact('biaya_pengantaran', 'biaya_pelayaran', 'detail_transaksi'));
View:
#foreach ($detail_transaksi as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>
<small>Nama Penerima: {{ $item->nama_penerima }}</small><br>
<small>Nama Penerima: {{ $item->telepon_penerima }}</small><br>
<small>Nama Penerima: {{ $item->alamat_penerima }}</small>
<br>
<small>Rute Pengantaran:
{{ $item->biaya_pengantaran->rute_asal }} -
{{ $item->biaya_pengantaran->rute_tujuan }}
</small><br>
<br>
<small>Merk/Tipe: {{ $item->merk_type }}</small><br>
<small>Warna: {{ $item->warna }}</small><br>
<small>No. Polisi: {{ $item->no_polisi }}</small>
<br>
<small>Biaya Pengantaran
{{ number_format($item->biaya_pengantaran) }}</small><br>
<small>Biaya Koordinasi {{ number_format($item->biaya_koordinasi) }}</small>
</td>
<td><b>{{ number_format($item->total_biaya_per_unit) }}</b></td>
</tr>
#endforeach
Model:
public function biaya_pengantaran()
{
return $this->belongsTo(BiayaPengantaran::class);
}

Related

How to add two numbers in the blade Laravel

I need to add two numbers/amount displayed in my blade view, as shown in the image below
I need to display as 666.68 / 1000 AUD
Here's my blade codes
<td>
#foreach ($team->competitionPayments as $payment)
{{ $payment->amount }}
#endforeach
<br>
{{ $team->competitionPayments }}
/
#foreach ($team->competitions as $total)
{{ $total->pivot->total_fee .' AUD'}}
#endforeach
</td>
Here's my controller function
public function detailedRegistrations($competition)
{
$competitions = $this->competitionsRepo->findOrFail($competition);
$teams = $competitions->teams()->get();
$payments = $competitions->payments()->get();
return view('competitions.detailed-registrations',
[
'teams'=>$teams,
'payments'=>$payments,
'competitions'=>$competitions,
]
);
}
Here's the competitionPayments relationship in the Team model
public function competitionPayments()
{
return $this->hasMany(CompetitionPayment::class, 'team_id');
}
Please give me an idea of how to make this work
here competitionPayments is a collection so you can apply sum() function on it like
<td>
{{ $payment->competitionPayments->sum('amount') }}
<br>
ref link https://laravel.com/docs/8.x/collections#method-sum
This works
<td>
{{ $team->competitionPayments->sum('amount') }}
/
#foreach ($team->competitions as $total)
{{ $total->pivot->total_fee .' AUD'}}
#endforeach
</td>

Laravel array loop

I have shipping method where will calculate price, this codes/prices etc. comes from third-party website (nothing stored in my database).
my problem is i don't know how to get objects that i need!
here is screen shot of my data on dd
my function
public function index() {
$province = RajaOngkir::Provinsi()->all();
// this part will return dd image above
$cost = RajaOngkir::Cost([
'origin' => 501, // id kota asal
'destination' => 114, // id kota tujuan
'weight' => 1000, // berat satuan gram
'courier' => 'jne', // kode kurir pengantar ( jne / tiki / pos )
])->get();
return view('welcome', compact('province', 'cost'));
}
my view codes blade
#foreach($cost as $option)
{{$option['code']}} <br>
{{$option['name']}}
#endforeach
PS: I got my dd with {{dd($option)}} in my loop(#foreach)
So far my loop return this (compare to dd to):
You can do something like:
#if(!empty($cost))
#foreach($cost as $option)
{{$option['code']} <br>
{{$option['name']}} <br>
#if(!empty($option['costs']))
#foreach($option['costs'] as $cost)
{{$cost['service']}} <br>
{{$cost['description']}} <br>
#if(!empty($cost['cost'])
#foreach($cost['cost'] as $c)
{{ $c['value'] }} <br>
{{ $c['etd'] }} <br>
{{ $c['note'] }} <br>
#endforeach
#endif
#endforeach
#endif
#endforeach
#endif
try this :
#foreach($cost as $option)
{{$option['code']}} <br>
{{$option['name'] }} <br>
#foreach($option['costs'] as $cost)
#foreach($cost as $arr)
{{$arr['service'] }} <br>
{{$arr['description']}} <br>
#foreach($arr['cost'] as $c)
{{ $c['value'] }} <br>
{{ $c['etd'] }} <br>
{{ $c['note'] }} <br>
#endforeach
#endforeach
#endforeach
#endforeach

Laravel Error: "Trying to get property of non-object"

I have a table what populates from database:
#extends('layouts.master')
#section('main')
#parent
<table border=1 align=center;>
<tr><td rowspan=10><img src="media/productimg/question.jpg" width=250px></td>
<th>Márka</th><td colspan=3>{{ $productdetails->brand }}</td></tr>
<tr><th>Beszállító</th><td colspan=3>{{ $productdetails->supplier }}</td></tr>
<tr><th>{{ $productdetails->type }}</th>
<th colspan=3>{{ $productdetails->name }}</th></tr>
<tr><th>Nettó beszerzési ár</th><td>{{ $productdetails->wholeprice }} Ft</td>
<th rowspan=2>Ár</th><td rowspan=2>{{ $productdetails->price }} Ft</td></tr>
<tr><th>Bruttó beszerzési ár</th><td>{{ $productdetails->wholeprice }} Ft</td>
<tr><th>Vonalkód</th><td>{{ $productdetails->barcode }}</td><th>Raktáron</th><td>{{ $productdetails->count }} {{ $productdetails->dimension }}</td></tr>
<tr><th>Elhelyezkedés</th><td>{{ $productdetails->whereis }} {{ $productdetails->whereis2 }}</td><th>Küszöb</th><td>{{ $productdetails->threshold }} {{ $productdetails->dimension }}</td></tr>
<tr><th>Utolsó rendelés</th><td> NA </td>
<th>Utolsó vásárlás</th><td> NA </td></tr>
<tr><td colspan=5><button class="button">Kép szerkesztése</button>
<button class="button">Termék szerkesztése</button>
<button class="button">Mennyiség szabályozása</button></td></tr>
</table>
#endsection
And I got this error:
ErrorException in 20d205ed160f36c734b8252e44ac81bfa0977988.php line 6:
Trying to get property of non-object
If I replace the table with
<?php print_r($productdetails);?>
I got the the array with the good values.
What going wrong?
You are saying that the array will show as it should be.
You could try for example:
{{ $productdetails['type'] }} instead of {{ $productdetails->type }}
But please post the result of the print_r($productdetails) function so that we can see whats wrong with the code.
The solution was: {{ $productdetails[0]->type }}
Thanks for #rahul_m for his answer:
Once check the structure of array it is having 0th index first,
You should get it like,
{{$product[0]->brand}}
OR
$product = DB::table("inventory")->where("barcode", $id)->first(); //
and get data as echo $product->brand;
Give it a try, it should work.

Undefined property: Illuminate\Database\Eloquent\Collection::$id

I don't know exactly why I'm getting this error as indicated by title. I am trying to write the code fro deleting records. This is the code below.
<TABLE >
#foreach($users as $user)
<TR><TD>{{ $user->id }}</TD><TD>{{ $user->firstname }}</TD><TD><div id="divContainer"><div class="theDiv"><form action="/users/{{ $users->id }}" method="POST"> {{ csrf_field() }} {{ method_field('DELETE') }}<button class="css-deletebutton">DELETE</button></form></div></div></TD><TD>{!! Form::submit('DEACTIVATE', ['class'=>'css-statusbutton']) !!}</TD></TR>
#endforeach
<!--{!! $users->render() !!}-->
</TABLE>
This is the route:
Route::resource('users', 'UserController');
This is the controller method:
public function show($id)
{
$users = User::find($id);
return view('userpages.show')->with('users', $users);
}
This is the view to be displayed for deletion of any record.
#section('body')
{!! Form::open([ 'method' => 'delete', 'route' => ['users.destroy', $users->id]]) !!}
<TABLE>
<TR><TD>{{ $users->firstname }}</TD><TD>{{ $users->lastname }}</TD><TD>{{ $users->email }}</TD><TD>{{ $users->username }}</TD><TD>{!! Form::submit('DELETE', ['class'=>'css-deletebutton']) !!}</TD></TR>
</TABLE>
{!! Form::close() !!}
#stop
This is the error message I'm getting:
ErrorException in acf1a7ad2174bd2b743200b1a50b4c9f line 14:
Undefined property: Illuminate\Database\Eloquent\Collection::$id (View: C:\Users\ken4ward\Documents\xampp\htdocs\schoolproject\resources\views\userpages\index.blade.php)
I can see you are referring to $users->id within the foreach loop in the first code snippet - that's what's causing the error you're getting. $users is the variable that holds the collection and $user is the variable that you should use to access user's data, including user's ID.

Laravel Form Looping Not Rendering Completely

My View
<tbody>
#foreach($categories as $category)
<tr>
<td>{{ $category->name }}</td>
<td>{{ $category->slug }}</td>
<td>{{ ($category->TermTaxonomy ? $category->TermTaxonomy->description : '') }}</td>
<td>
{{ Form::open(['method' => 'DELETE', 'route' => ['admin_posts_categories_destroy', $category->term_id]]) }}
{{ Form::submit('Delete'); }}
{{ Form::close() }}
</td>
</tr>
#endforeach
</tbody>
then result in inspect element
so i can't delete the first row , but i can delete the other , why this things happen ? and how to fix it. already try in the other browser and still same.
Never mind , in have 2 form in my view the first is static form to add term, the second is loop form for restful delete, and missing {{ Form::close() }} in the first form (static) , so just put {{ Form::close() }} in the first form (static) and both form static and looping work like a charm. thanks for all.

Categories