Jimmyjbk
Jimmyjbk

Reputation: 401

Get objects of related tables from a collection laravel

I want to pass the result of a searching action but I am facing a problem because it is returning an array and from that all the relationships of the table are not working

result view. For this I can only access the data on my table not the related data through relationships

@extends('layouts.app')

@section('content')
    @foreach ($result as $object)
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-md-8">
                    <div class="card">
                        <div class="card-header">
                            <h3>Details for the animal</h3>

                        </div>
                        <div class="card-body">
                            <div class="col-12">
                                <p><strong>Id: </strong>{{ $object->id }}</p>


                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    @endforeach
@endsection

Here is my controller

<?php

namespace App\Http\Controllers;

use App\Animal;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class SearchController extends Controller
{
    public function index()
    {
        Animal::all();
        return view('search.index');
    }
    public function postSearch(Request $request)
    {
        $serial_number = $request->input('search');
        $this->getResult($serial_number);
        return redirect()->route('result',$serial_number);
    }
    public function getResult($serial_number){
        $result = DB::table('slaughters')->where(function ($query) use ($serial_number) {
            $query->where('id','LIKE',"%$serial_number%");
        })->latest()->get();

        return view('search.result', ['result'=>$result]);


    }
}

And my routes

Route::get('/search','SearchController@index')->name('search');
Route::post('/get','SearchController@postSearch');
Route::get('/search/{result}','SearchController@getResult')->name('result');

I would like to access data from the table related to this one too. What am to do

Slaughter model

class Slaughter extends Model
{
    protected $guarded = [];

    public function user(){
        return $this->belongsTo(User::class);
    }

    public function animal(){
        return $this->belongsTo(Animal::class);
    }

Upvotes: 0

Views: 255

Answers (2)

Remul
Remul

Reputation: 8242

In your Controller:

use App\Slaughter;

public function getResult($serial_number) {
    $result = Slaughter::where('id', 'like', "%{$serial_number}%")
        ->with('user', 'animal')
        ->latest()
        ->get();

    return view('search.result', compact('result'));
}

In your view you can then access the relationships like so:

{{ $object->user }}

{{ $object->animal }}

Upvotes: 0

Serhii Posternak
Serhii Posternak

Reputation: 504

You must create model Slaughter and define relations.

And then you can get result:

public function getResult($serial_number)
{
    $result = Slaughter::with(['name_of_relation_1', 'name_of_relation_2'])->latest()->get();

    return view('search.result', ['result'=>$result]);
}

Upvotes: 2

Related Questions