Laravel array loop - php

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

Related

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

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);
}

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>

How to display array data in laravel view

I have a dataset like below inside a cell named rolls.
[
"142650",
"142651",
"142603",
"142604"
]
I have an array named $rooms, where each room has cell called rolls.
#foreach($rooms as $room)
{{ $room->rolls }}
#endforeach
The code above displaying data like below in view
["142650", "142651", "142603", "142604", "142605"]
But I want to display like below...
142650, 142651, 142603, 142604, 142605
I have tried this
#foreach($rooms as $room)
#foreach($room->rolls as $roll)
{{ $roll }}
#endforeach
#endforeach
But getting error like below
Invalid argument supplied for foreach()
I think the cleanest solution, without changing controller is
#foreach($rooms as $room)
<h1>{{ $room->name }}</h1>
#foreach(json_decode($room->rolls) as $roll)
{{ $roll }}
#endforeach
#endforeach
Try not to use #php or <?php ?> in your blade templates
This is the solution I have found, I have not changed anything in my controller, this is what I have done in the view.
#foreach($rooms as $room)
<h1>{{ $room->name }}</h1>
<?php $rolls = json_decode($room->rolls); ?>
#foreach($rolls as $roll)
{{ $roll }}#if(!$loop->last), #endif
#endforeach
#endforeach

Laravel 5.3 : Error while fetch data in view using foreach

i got error while i'm trying to fetch my data in view. This is my code :
controller
public function create()
{
$categories = Category::all();
$tag = Tag::groupBy('name')->pluck('name');
$tags = json_encode($tag);
return view('backend.articles.create', compact('categories', 'tags'));
}
view
fetch data categories
<div class="panel-body">
#foreach ($categories as $category)
<div class="checkbox">
<label>
{!! Form::checkbox('category_id[]', $category->id) !!} {{ $category->name }}
</label>
</div>
#endforeach
</div>
fetch data tags
jQuery(document).ready(function($) {
$('#tags').magicSuggest({
cls: 'form-control',
data: {!!$tags!!},
#if (count(old('tags')))
value: [
#foreach (old('tags') as $tag)
'{{$tag}}',
#endforeach
]
#endif
});
});
I dont know why i got this error message : Invalid argument supplied for foreach(). Could anybody to help me to fix this problem ?
view
<div class="panel-body">
#if($categories)
#foreach ($categories as $category)
<div class="checkbox">
<label>
{!! Form::checkbox('category_id[]', $category->id) !!} {{ $category->name }}
</label>
</div>
#endforeach
#endif
script
jQuery(document).ready(function($) {
$('#tags').magicSuggest({
cls: 'form-control',
data: {!!$tags!!},
#if (is_array(old('tags')))
value: [
#foreach (old('tags') as $tag)
'{{$tag}}',
#endforeach
]
#endif
});
});
Try to use is_array to make sure old('tags') returning array, I believe the problem because the old('tags') is not returning array.

Is there a better way to send variables to blade?

I'm still learning Laravel and how the blade system all works..
I'm wondering what the better way of doing something is and that something is; I've done a query to pull a row from the database and then I want to put that query into an array so I can pick out whatever column I want.
Then I want to pass that to my home.blade.php and be able to use {{ $name }} for example. T
This is what I've got:
TO NOTE: THIS WORKS THE WAY I WANT IT TO BUT IM SURE IM DOING IT THE LONG WAY (WRONG) ROUND.
HomeController.php
<?php
class HomeController extends BaseController {
public function home() {
$hero_query = DB::table('heros')->where('owner_id', Auth::user()->id)->pluck('hero');
if($hero_query) {
$owner_id = Auth::user()->id;
$user = DB::table('heros')->where('owner_id', $owner_id)->first();
$name = $user->hero;
$level = $user->level;
$exp = $user->exp;
$str = $user->str;
$atk = $user->atk;
$def = $user->def;
$int = $user->int;
$blk = $user->blk;
return View::make('home', array(
'name' => $name,
'level' => $level,
'exp' => $exp,
'str' => $str,
'atk' => $atk,
'def' => $def,
'int' => $int,
'blk' => $blk
));
} else {
return View::make('home');
}
}
}
home.blade.php
#if($hero = DB::table('heros')->where('owner_id', Auth::user()->id)->pluck('hero'))
<form action="{{ URL::route('hero-delete') }}" method="POST">
Your hero:<br>
<b>{{ $hero; }}</b> | <input type="submit" value="Delete"><br>
<b>Stats:</b>
LvL: {{ $level }}
Exp: {{ $exp }}
Str: {{ $str }}
Atk: {{ $atk }}
Def: {{ $def }}
Int: {{ $int }}
Blk: {{ $blk }}
</form>
#else
<form action="{{ URL::route('hero-create') }}" method="POST">
Hero:<br>
You do not have a hero, create one!
<input type="text" name="hero">
<input type="submit" value="Create hero">
#if($errors->has('hero'))
{{ $errors->first('hero')}}
#endif
{{ Form::token()}}
</form>
#endif
Now I'm sure I'm doing it in some stupid moronic way as I'm just starting out.. but could someone explain where I'm going wrong?
Thanks in advance!
Just pass the entire $user to the view
$user = DB::table('heros')->where('owner_id', $owner_id)->first();
return View::make('home', array('user' => $user));
Then in your view
Str: {{ $user->str }}
Int: {{ $user->int }}
and so on.
You're doing lots of things wrong :(
I fix a little bit the code, but I didn't test it, so use this as a guide to implement what you want.
class HomeController extends BaseController {
public function home() {
$hero = Hero::where('owner_id', '=', $owner_id)->first();
if($hero)
{
return Response::make('home', array('hero' => $hero->toArray()));
}
else
{
return Response::make('home');
}
}
}
The blade template
#if(isset($hero))
{{ Form::open(array('route' => 'hero-delete', 'method' => 'DELETE')) }}
Your hero:<br>
<b>{{ $hero->hero }}</b> | <input type="submit" value="Delete"><br>
<b>Stats:</b>
<!-- Here you can implement a foreach for performance -->
LvL: {{ $hero->level }}
Exp: {{ $hero->exp }}
Str: {{ $hero->str }}
Atk: {{ $hero->atk }}
Def: {{ $hero->def }}
Int: {{ $hero->int }}
Blk: {{ $hero->blk }}
{{ Form::close() }}
#else
{{ Form::open(array('route' => 'hero-create')) }}
Hero:<br>
You do not have a hero, create one!
<input type="text" name="hero">
<input type="submit" value="Create hero">
#if($errors->has('hero'))
{{ $errors->first('hero')}}
#endif
{{ Form::close() }}
#endif

Categories