1

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

4 Answers 4

2

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').

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

7 Comments

your answer does remove the answer but i get another error Trying to get property 'get_devicetypes' of non-object (View: C:\xampp\htdocs\inventory\resources\views\transactionsin\show.blade.php) the devicetype is saved in Transaction_in_detail not Transaction_in here is the migration table hastebin.com/ocosonuyal.php
This error is from {{ $tdin->get_devicetypes->nama_tipe_device }} this line do Transaction_in have get_devicebrands function?
You can remove it as using ternary operator do that code as {{ $tdin->get_devicetypes->nama_tipe_device ?? '' }}
i change the transactionsin to transactionsindetail inside if..else now i think may be work but it gives me this error Cannot end a section without first starting one. (View: C:\xampp\htdocs\inventory\resources\views\transactionsin\show.blade.php)
Remove foreach loop you're getting only first record though. I've edited my answer please check it.
|
1

if you want to make object countable you have to use $object->count()in your case $transactionin->count()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.