Eloquent relacje

0

Hej,

inicjalnie mam coś takiego

$document = $this->documentRepository->getOrFail($documentId);

wyświetlam w widoku tak

@forelse($document->details as $e)

    <tr data-documentelements="{{ $e->toJson() }}">
        <td> {{ $e->race->name }} </td>
        <td> {{ $e->quantity }} </td>
    </tr>
@empty
    <tr>
        <td colspan="2" style="text-align: center;">brak danych</td>
    </tr>
@endforelse

czyli mam encję Dokument oraz Detail

teraz potrzebuję zmienić na coś takiego

$document =  Document::join('details AS det', 'documents.id', '=', 'det.document_id')
                    ->leftJoin('details as detbis', 'detbis.detail_id', '=', 'det.id')
                    ->where('documents.id', $documentId)
                    ->whereNull('detbis.id')
                    ->get();

bez zmiany wyświetlania otrzymuję
Property [details] does not exist on this collection instance

jak posłużyć się query builderem aby używać eloquent relations?

0

dodam jeszcze moje migracje i modele i może się uda to zrobić w sposób bardziej Eloquent :)

    public function up()
    {
        Schema::create('documents', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('customer_id');
            $table->integer('location_id');
            $table->integer('user_id');
            $table->integer('type');
            $table->date('date');
            $table->text('description')->nullable();
            $table->timestamps();
        });
    }

    public function up()
    {
        Schema::create('details', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('race_id');
            $table->integer('quantity');
            $table->integer('herd_age');
            $table->integer('duration');
            $table->integer('document_id');
            $table->integer('detail_id')->nullable();
            $table->timestamps();
        });
    }

modele

class Document extends Model
{
    protected $fillable = [
        'customer_id', 'location_id', 'user_id', 'type', 'date', 'description'
    ];

    /**
     * Get the documents for the customer.
     */
    public function customer()
    {
        return $this->belongsTo('App\Models\Customer');
    }

    /**
     * Get the documents for the user.
     */
    public function user()
    {
        return $this->belongsTo('App\User');
    }

    /**
     * Get the documents for the user.
     */
    public function location()
    {
        return $this->belongsTo('App\Models\Location');
    }

    /**
     * Get the documents elements
     */
    public function details()
    {
        return $this->hasMany('App\Models\Detail');
    }

class Detail extends Model
{
    protected $fillable = [
        'race_id', 'quantity', 'herd_age', 'duration', 'document_id', 'detail_id',
    ];

    public function race()
    {
        return $this->belongsTo('App\Models\Race');
    }

    public function connectedPositions()
    {
        return $this->hasOne('App\Models\Detail', 'detail_id');
    }

}

chodzi o to że pozycja z dokumentu może być skopiowana do innego dokumentu. Jeśli tak się stanie to zapisuję sobie id skopiowanej pozycji w polu detail_id.
Celem jest zwócenie z tego 1szego dokumentu ( tego z którego kopiowałem ) tych pozycji, które nie zostały skopiowane.

0

Zamiast takich gołych joinów lepiej gdybyś użył eloquentowych relacji. Czyli zamiast takich:

Document::join('details AS det', 'documents.id', '=', 'det.document_id')

miałbyś

Document::with('details')

Oczywiście musiałbyś dodać relację do modelu document.

A co do Twojego problemu, dobrze gdybyś sobie użył jakiejś funkcji dd() albo var_dump() i zobaczył czy to co masz w $document jest tym czego się spodziewasz. Ewentualnie możesz użyć debuggera, jeśli umiesz :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1