V
V
Vanik Khachatryan2018-02-18 16:55:18
Laravel
Vanik Khachatryan, 2018-02-18 16:55:18

Why can't link tables in Laravel?

controller:

public function getMyInvest() {
        $myInvest = Invest::->where('user_id', Session::get('id_user'))->get();

        return view('invest.my-invest', ['invests' => $myInvest]);
    }

Output in view:
@foreach($invests as $invest)
        <tr>
            <th scope="row">{{$invest->id}}</th>
            <td>{{$invest->tarif_id->name}}</td>
            <td>{{$invest->created_at}} - 16.03.18</td>
            <td>{{$invest->sum}}руб.</td>
            <td><a class="glyphicon icon-remove" href="" > </a></td>
        </tr>
        @endforeach

And the model itself:
class Invest extends Model
{
    public $timestamps = false;

    public function tarif_id() {
        return $this->hasOne('App\Models\Invest_tarif', 'id');
    }
}

What is the reason for the error
5a8985a6ba955738975104.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Wells, 2018-02-18
@Alex_Wells

The error is obvious if you even try to read the documentation: follow the codestyle
(tarif_id -> tariff, Invest_tarif -> InvestTariff) and everything will work for you.
It's also interesting how you try to use the created_at field if timestamps are disabled ..
PS: is there a new operator in php ::->?))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question