0

So basic I have two tables "Accounts" and "Characters" that are linked by a single ID Each "Account" has 3 "Characters" So I want to display the 3 characters that are linked to the main "Account"

this is my HomeController

    {

       $data = DB::table('characters')
            ->join('accounts', 'accounts.cUid', '=','characters.pUniqueID')->get();
        return view('home', compact('data'));
    }

this is my Home.Blade

@foreach($data as $per)
            @if( $per->pUniqueID == Auth::user()->cUid )
....
....
         @else
       <script>window.location.href = '{{url("/characters")}}'; </script>
   @endif
@endforeach

2
  • use a var_dump($data); before your loop @foreach, and post a revelant part of what is displayed, you can use var_dump($per) inside loop to see what you got on each $per Commented Oct 25, 2019 at 14:49
  • do you have any relationships set up in the models? Commented Oct 25, 2019 at 14:49

1 Answer 1

1

Configure the relationships in your model:

Accounts.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Accounts extends Model
{
    public function characters()
    {
        return $this->hasMany('App\Characters');
    }
}

So, in your query:

$data = Accounts::with("characters")->get();

return view('home', compact('data'));

And in your template:

@foreach($data as $per)
    @if($per->pUniqueID == Auth::user()->cUid)
        @foreach($per->characters as $character)
            {{$character->id}} //or any other character attribute
        @endforeach
    @else
       <script>window.location.href = '{{url("/characters")}}'; </script>
    @endif
@endforeach
Sign up to request clarification or add additional context in comments.

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.