0

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);
    }
2
  • Is there a reason that you are using the query builder instead of eloquent to fetch the search results? and please add the code from the relationship that your are talking about to the question. Commented Oct 28, 2019 at 15:57
  • How can I use eloquent there and I have updated the relatioshionships Commented Oct 28, 2019 at 16:40

2 Answers 2

2

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]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

So for the name_of_relation am I supposed to use the class or which name am I to use
0

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 }}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.