Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RobHowdle's avatar

Laravel Query not returning correct information

Hi all, hopefully somebody can tell me what I've done wrong. Just an FYI before I start, I know this is messy and there are better ways to do it, I'm just not at the "better way" level just get I'm still learning!

I have a parts table with different parts in it. In one of my questions I am telling the query to show the parts based of a matching ID (this part works fine) as well as checking to see whether the version of the parts in the table matches what the user has selected in a form. Here is the controller. The problem is at the $domeVersion/ $domeSelections variable.


namespace App\Http\Controllers\Droids;

use Gate;
use App\User;
use App\Role;
use App\Droid;
use App\DroidUser;
use App\DroidDetail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class DroidsUsersController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user = auth()->user();

        $my_droids = DB::table('droid_user')
            ->join('droids', 'droid_id', '=', 'droids.id')
            ->select('droid_user.id', 'droids.class', 'droids.image')
            ->where('droid_user.user_id', '=', $user->id)
            ->get();


        return view('droids.user.index', ['my_droids' => $my_droids]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('droids.user.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //Assigns a Droid to a user
        $newDroidBuild = $request->input('droidIdentification');
        $newBuild = new DroidUser();
        $newBuild->user_id = auth()->user()->id;
        $newBuild->droid_id = $newDroidBuild;
        $newBuild->save();

        //Sends a blank Droid Detail form into the database
        $user = auth()->user();
        $droidDetailForm = $newBuild->id;
        $newDroidDetails = new DroidDetail();
        $newDroidDetails->droid_user_id = $droidDetailForm;
        $newDroidDetails->save();

        return redirect()->route('droid.user.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request, $id)
    {
        //Returns checklist for that droid
        $currentBuilds = DB::table('droid_user')
            ->join('droids', 'droid_id', '=', 'droids.id')
            ->select('droids.class', 'droids.id')
            ->where('droid_user.id', '=', $id)
            ->get();

        //Displays parts for current droid
        $Parts = DB::table('parts')
            ->join('droid_user', 'droid_user.droid_id', '=', 'parts.droids_id')
            ->where('droid_user.id', '=', $id)
            ->join('parts_group', 'parts_group.id', '=', 'parts.group_id')
            // ->where('parts.group_id', '=', '')
            ->select('parts.id', 'droid_section', 'droid_version', 'sub_section', 'part_name', 'parts_group.group_name')
            // ->orderBy('droid_section', 'DESC')
            // ->orderBy('sub_section')
            ->get();


        $versionParts = DB::table('parts')
            ->select('parts.droid_version', 'parts.droid_section', 'parts.sub_section', 'parts.part_name', 'parts.id', 'droid_details.dome_version')
            ->join('droid_details', 'droid_details.droids_id', '=', 'parts.droids_id', )
            ->where('droid_user_id', '=', $id)
            ->groupBy('droid_version')
            ->orderBy('droid_section', 'DESC')
            ->orderBy('sub_section')
            ->get();

            // dd($versionParts);
        //Filters Droid Details table by current droid_user_id
        $droidDetails = DB::table('droid_details')
            ->where('droid_user_id', '=', $id)
            ->get();

        //Get Users Selected Versions to return parts
        $domeVersion = DB::table('droid_details')->select('dome_version')->get();
        $domeSelections = DB::table('parts')
            ->select('parts.droid_version', 'parts.droid_section', 'parts.sub_section', 'parts.part_name', 'parts.id', 'droid_details.dome_version')
            ->join('droid_details', 'droid_details.droids_id', '=', 'parts.droids_id')
            ->where([
                ['droid_user_id', '=', $id],
                ['parts.droid_version', '=', 'MK2'],
            ])
            ->get();
            // dd($domeSelections);

        return view('droids.user.edit', [
            'currentBuilds' => $currentBuilds,
            'Parts' => $Parts,
            'droidDetails' => $droidDetails,
            'versionParts' => $versionParts,
            'domeSelections' => $domeSelections,
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //Update users Droid Information
        $droidInfo = DB::table('droid_details')
            ->where('droid_user_id', '=', $id)
            ->update([
                'droid_designation' => $request->input('droid_designation'),
                'builder_name' => $request->input('builder_name'),
                'description' => $request->input('description'),
                'droid_version' => $request->input('droid_version'),
                'colors' => $request->input('colors'),
                'mobility' => $request->input('mobility'),
                'electronics' => $request->input('electronics'),
                'control_system' => $request->input('control_system'),
                'drive_system' => $request->input('drive_system'),
                'power' => $request->input('power'),
            ]
        );


        return back();
    }

    public function updateDome(Request $request, $id)
    {
                //Versions of each section
                $selectDomeVersion = DB::table('droid_details')
                ->where('droid_user_id', '=', $id)
                ->update([
                    'dome_version' => $request->input('selectDomeVersion'),
            ]);
            // dd($selectDomeVersion);
            return back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request, $id)
    {
        //Deletes Droid from droid_user table
        $my_droids = DB::table('droid_user')->where('droid_user.id', '=', $id);
        $my_droids->delete();

        return redirect()->route('droid.user.index');

    }
}

So, just to save time and repeated answers here is what I have tried so far:

        $domeVersion = DB::table('droid_details')->select('dome_version')->get();
        $domeSelections = DB::table('parts')
            ->select('parts.droid_version', 'parts.droid_section', 'parts.sub_section', 'parts.part_name', 'parts.id', 'droid_details.dome_version')
            ->join('droid_details', 'droid_details.droids_id', '=', 'parts.droids_id')
            ->where([
                ['droid_user_id', '=', $id],
                ['parts.droid_version', '=', (string)$domeVersion],
            ])
            ->get();

This simply returns nothing to the blade file. If I dd($domeVersion) it gives me the following

Illuminate\Support\Collection {#514 ▼
  #items: array:2 [▼
    0 => {#515 ▼
      +"dome_version": "MK2"
    }
    1 => {#518 ▼
      +"dome_version": null
    }
  ]
}

Which is correct.

I have also tried:

        $domeSelections = DB::table('parts')
            ->select('parts.droid_version', 'parts.droid_section', 'parts.sub_section', 'parts.part_name', 'parts.id', 'droid_details.dome_version')
            ->join('droid_details', 'droid_details.droids_id', '=', 'parts.droids_id')
            ->where([
                ['droid_user_id', '=', $id],
                ['parts.droid_version', '=', 'droid_details.dome_version'],
            ])
            ->get();

Again this returns nothing and when i do a a dd($domeSelections) it returns this:

Illuminate\Support\Collection {#511 ▼
  #items: []
}

Now, if I give it a specific value in the where clause like I have in the first code exert of my controller then it returns back the row where the parts version is equal to that string and that's exactly what I want however I am trying to make the query check another table column to get that value.

I have checked my SQL query and my SQL is correct because if I do the exact same query directly into phpmyadmin it returns the exact same row with all the data I have requested.

I'm well and truely stumped on this, can anyone help? Sorry for the long message but hopefully I've provided enough information for people to understand but I'm happy to clarify anything.

Thanks Rob

0 likes
4 replies
Snapey's avatar

You would get a lot of mileage from spending a little time learning eloquent

1 like
RobHowdle's avatar

I’ll be honest I didn’t know you could haha. I’ll give it a try, the main thing was that I couldn’t return the variable within php my admin and I figured that the queries were the same written differently and worried I’d end up with the same results?

RobHowdle's avatar

I know I really should! My thought process was that if I could get it running in a way I mostly understood, once it worked I could them change it over to eloquent but perhaps I’m missing something that maybe by doing it in eloquent I would have figured out

Please or to participate in this conversation.