Q:

Laravel relations are not working?

I have relation with Deposit model but relations are not working.

error:Trying to get property of non-object (View: C:\xamp\htdocs\assets\core\resources\views\user\deposit-history.blade.php)

View code
<tbody>
        @php $i = 0;@endphp
        @foreach($deposit as $p)
            @php $i++;@endphp
            <tr>
                <td>{{ $i }}</td>
                <td width="10%">{{ date('d-F-Y h:s:i A',strtotime($p->created_at)) }}</td>
                <td><span class="aaaa"><strong>{{ $p->plan->name }}</strong></span></td>
                <td>#{{ $p->deposit_number }}</td>
                <td>{{ $p->amount }} - {{ $basic->currency }}</td>
                <td width="13%">{{ $p->percent }} %</td>
                <td>{{ $p->time }} - times</td>
                <td><span class="aaaa"><strong>{{ $p->compound->name }}</strong></span></td>
                <td>
                    @if($p->status == 0)
                        <span class="label label-secondary"><i class="fa fa-spinner"></i> Running</span>
                    @else
                        <span class="label label-success"><i class="fa fa-check" aria-hidden="true"></i> Completed</span>
                    @endif

                </td>
            </tr>
        @endforeach
</tbody>

Model code
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Deposit extends Model
{
    protected $table = 'deposits';

    protected $fillable = ['deposit_number','user_id','plan_id','amount','status','percent','time','compound_id'];

    public function plan()
    {
        return $this->belongsTo(Plan::class,'plan_id');
    }
    public function compound()
    {
        return $this->belongsTo(Compound::class,'compound_id');
    }
    public function user()
    {
        return $this->belongsTo(User::class,'user_id');
    }
}

A:

In your view you have: 
<td><span class="aaaa"><strong>{{ $p->plan->name }}</strong></span></td>
And this: 
<td><span class="aaaa"><strong>{{ $p->compound->name }}</strong></span></td>
If two line will work if you always have $p->plan and $p->compound. But if you don't have(e.g. null) $p->plan or $p->compound it will throw an exception(Trying to get property of non-object). 
So, when will these property be null? 
Firstly, you need to make sure that you have plan_id and compound_id in your deposits table. Otherwise, the relational property will return null. 
Secondly, It can be null if the plan_id or compound_id is empty/null or a value that doesn't exist in plans or compounds table. 
What is the solution even if it's null?
If you need to show other field even if your plan or compound is null then you can change the above to line into the following lines: 
<td><span class="aaaa"><strong>{{ isset($p->plan) ? $p->plan->name : '' }}</strong></span></td>
and
<td><span class="aaaa"><strong>{{ isset($p->compound) ? $p->compound->name : '' }}</strong></span></td>

