Reputation: 1104
I want to display data from multiple table to one view, first table is Transaction_in
and second table is Transaction_in_detail
but beside those two other table are involved.
This is Transcation_in Controller
class Transactions_inController extends Controller
{
public function show($id)
{
$supplierList = Supplier::where('id', 'nama')->first();
$transactionin = Transaction_in::where('id', $id)->first();
$deviceTypeList = DeviceType::where('id', 'nama_tipe_device')->first();
$deviceBrandList = DeviceBrand::where('id', 'nama_brand_device')->first();
$transactionindetail = Transaction_in_detail::where('id', 'Transansaction_in_id')->first();
//return view('transactionsin.show', compact('supplierList', 'transactionsin', 'deviceTypeList', 'deviceBrandList', 'transactionindetail'));
return view('transactionsin.show')->with('transactionsin', $transactionin);
return view('transactionsin.show')->with('transactionsindetail', $transactionindetail);
}
}
Transaction_in Model
class Transaction_in extends Model
{
protected $guarded = [];
public function get_suppliers()
{
return $this->belongsTo(Supplier::class, 'Supplier_id');
}
public function get_devicetypes()
{
return $this->belongsToMany(DeviceType::class, 'DeviceType_id');
}
public function get_devicebrands()
{
return $this->belongsToMany(DeviceBrand::class, 'DeviceBrand_id');
}
public function get_transactionindetail()
{
return $this->belongsToMany(Transaction_in_detail::class, 'Transaction_in_id');
}
}
Transaction_in_detail Model
class Transaction_in_detail extends Model
{
protected $guarded = [];
public function get_transction_in_id()
{
return $this->belongsTo(Transaction_in::class, 'Transaction_in_id');
}
public function get_devicetypes()
{
return $this->belongsToMany(DeviceType::class, 'DeviceType_id');
}
public function get_devicebrands()
{
return $this->belongsToMany(DeviceBrand::class, 'DeviceBrand_id');
}
}
I want to display data from Transaction_in_detail
table to Transaction_in
Controller, but i have this error
count(): Parameter must be an array or an object that implements Countable (View: C:\xampp\htdocs\inventory\resources\views\transactionsin\show.blade.php)
and this is transactionsin.show
code https://hastebin.com/ilewesucej.xml
Upvotes: 1
Views: 2348
Reputation: 1156
if you want to make object countable you have to use $object->count()
in your case $transactionin->count()
Upvotes: 1
Reputation: 15296
You don't need to return view two times. use compact for set multiple variable.
public function show($id)
{
$supplierList = Supplier::where('id', 'nama')->first();
$transactionin = Transaction_in::where('id', $id)->first();
$deviceTypeList = DeviceType::where('id', 'nama_tipe_device')->first();
$deviceBrandList = DeviceBrand::where('id', 'nama_brand_device')->first();
$transactionindetail = Transaction_in_detail::where('id', 'Transansaction_in_id')->first();
//return view('transactionsin.show', compact('supplierList', 'transactionsin', 'deviceTypeList', 'deviceBrandList', 'transactionindetail'));
return view('transactionsin.show',compact('transactionsin','transactionsindetail'));
}
In blade file check with empty condition.
@if (!empty($transactionsin))
<div class="tale-responsive">
<table class="table table-hover table-bordered">
<thead align="center">
<tr class="table-primary">
<th>Tipe Perangkat</th>
<th>Brand Perangkat</th>
<th>Spesifikasi</th>
<th>Jumlah</th>
<th>Harga</th>
<th>Total Harga</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ $transactionin->get_devicetypes->nama_tipe_device }}</td>
<td>{{ $transactionin->get_devicebrands->nama_brand_device }}</td>
<td>{{ $transactionin->get_transactionindetail->spek_device }}</td>
<td>{{ $transactionin->get_transactionindetail->harga_device }}</td>
<td>{{ $transactionin->get_transactionindetail->jumlah_device }}</td>
<td>{{ $transactionin->get_transactionindetail->total_harga_device }}</td>
</tr>
</tbody>
</table>
</div>
@else
<h6>No Data Found</h6>
@endif
Upvotes: 1
Reputation: 5825
What does your table setup look like, specifically what is the relationship between Transaction_in
and Transaction_in_detail
? You have a One To Many relationship given in Transaction_in_detail::get_transaction_in_id
and a Many to Many relationship given in Transaction_in::get_transactionindetail
.
The belongsToMany
relationship is for Many to Many relationships with a pivot table. Maybe this is supposed to be hasMany
instead?
Start by clarifying that relationship correctly. See the docs on relationships in Laravel. Then you can pull the correct data.
Are you trying to get ONE Transaction_in
instance with the id $id
? In that case, no, you can't iterate over it. Maybe you're trying for something like this?
$transactionin = Transaction_in::find($id);
$transactionindetail = $transactionin->get_transactionindetail;
// $transactionindetail will now be a `Collection` which implements `Countable`.
Also note that you're (and some of the other answers) are mixing up transactionsin
and transactionin
(without the 's').
Upvotes: 2
Reputation: 101
1.
You are using first()
method which returns a single object , not array. Instead of returning a collection of models, first()
method returns a single model instance. Rewrite your code as following -
$transactionin = Transaction_in::where('id', $id)->get();
For reference, Laravel Docs
2. You don't have to return view and variables twice. Return once as following -
return view('transactionsin.show',compact('transactionsin','transactionsindetail'));
Upvotes: 1