Home > OS >  Laravel Snippets (v.1.13.0): Problems with relationships (One-To-Many, ...)
Laravel Snippets (v.1.13.0): Problems with relationships (One-To-Many, ...)

Time:09-23

Introduction I'm using this Add-on for Visual Studio Code. When creating relations with it. My views seem not working. I get an error in the browser (see below).

Laravel Snippets ID: onecentlin.laravel5-snippets Beschreibung: Laravel snippets for Visual Studio Code (Support Laravel 5 and above) Version: 1.13.0 Herausgeber: Winnie Lin Link zum Visual Studio Marketplace: https://marketplace.visualstudio.com/items?itemName=onecentlin.laravel5-snippets

My Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Person extends Model
{
    use HasFactory;
    protected $fillable = [];

    /**
     * Get the phone associated with the Person
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function phone(): HasOne
    {
        return $this->hasOne(Phone::class);
    }
}

Error shown by the browser, when calling the view

TypeError Return value of App\Models\Person::phone() must be an instance of App\Models\HasOne, instance of Illuminate\Database\Eloquent\Relations\HasOne returned (View: C:\Users\MusaSaglam\componenttest\resources\views\test\index.blade.php)

My view

<x-layout>

    <main class="main" id="top">
      <div class="container" data-layout="container">
        <script>
          var isFluid = JSON.parse(localStorage.getItem('isFluid'));
          if (isFluid) {
            var container = document.querySelector('[data-layout]');
            container.classList.remove('container');
            container.classList.add('container-fluid');
          }
        </script>
  
        <x-nav.admin-nav/>
        <div class="content">
            <x-nav.admin-nav-top/>

            <h1>People</h1>
            @foreach ($people as $person)
                <h2>{{ $person->id }}</h2>
                <h2>{{ $person->phone->number }}</h2>
            @endforeach


        </div>
      </div>
    </main>
  </x-layout>

But if I remove : HasOne after public function phone() in my model. Everything works fine.

Two questions

  • Why does the Snippet adds : HasOne. What is the benefit of this?
  • Does not make it easier for users like me using the snippet without : HasOne? I'm a beginner and I could not understand why the tutorials did not work.

CodePudding user response:

You can refer to the type declaration https://www.php.net/manual/en/language.types.declarations.php

  1. Why does the Snippet add: HasOne. What is the benefit of this?
  • It’s the return type of method and it’s standard so the VS code add-on is giving you the snippet(code) which follows the industry standards.
  1. Does not make it easier for users like me using the snippet without : HasOne? I'm a beginner and I could not understand why the tutorials did not work.
  • This snippet will work don’t worry about it. Just declare the HasOne before the class initialization like use Illuminate\Database\Eloquent\Relations\HasOne and it should work.

By the way type hinting is optional so a snippet will work if you just remove :HasOne(return type of method). which you already tried.

  • Related