Bosstone's avatar

Get Data from Two Tables

Hi, how can I get all the data from two tables:

users, projects

My tables are connected with user.id & projects.user_id

This is my Controller:

public function adminProjectShowAll() {
    
    $user = Auth::user();
    $stat = 1;
    $projects = Project::where('stat' <> $stat );

    
  return view('admin-project-show-all', compact('projects', 'user'));
    
}

my sql code would be: select * from users, projects where user.id = projects.user_id

This is my View:

@foreach($projects as $project)
                  <br>
                  <tr>
                    <td>{{ $project->name }}</td>
                    <td>{{ $project->user_id }}</td>
                    <td>{{ $project->cat_name }}</td>
                  </tr>
                  @endforeach 

Error: No Error, but nothing will be displayed...

Thank you for your help and support!

Best Regards,

Stefan

0 likes
5 replies
Snapey's avatar

This is pretty basic stuff. Suggest you follow the Laravel from scratch series

public function adminProjectShowAll() {
    
    $user = Auth::user();
    $stat = 1;
    $projects = $user->projects()->where('stat' <> $stat )->get();

    
  return view('admin-project-show-all', compact('projects', 'user'));
    
}

for this to work, you need relationships in User and Project

//User.php
public function projects()
{
    return $this->hasMany(Project::class);
}


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

Bosstone's avatar

Dear Snapey, dear Arthvrian,

thanks a lot for your help. I do not get it :-(

in my Project.php I have

public function user()
{
    return $this->belongsTo(\App\User::class, 'user_id');
}

in my User.php I have

public function projects()
  {
      return $this->hasMany(\App\Project::class, 'user_id');
  }

When I use

$results = User::with('projects')->get();

in my controller, then I get all data from User, but no data from projects.

@foreach($results as $project)
     <br>
        <tr>
            <td>{{ $project->name }}</td>
            <td>{{ $project->user_id }}</td>
            <td>{{ $project->cat_name }}</td>
         </tr>
 @endforeach

The first Data is from User, the other two from Project and those Variables will not be shown.

Thank you for your Time and support.

Stefan

arthvrian's avatar

without your data structure, is a little hard

did you try dump($results) or dump($project) in your view? to see if you get any data or what data you get

// from the previous link
$books = App\Book::with('author')->get();

foreach ($books as $book) {
    echo $book->author->name;
}

// you can do something like (set according to your view/table)

foreach ($results as $project) {
    echo $project->user->id; // get the user id of the project
}
Snapey's avatar

When you do this

$results = User::with('projects')->get();

You get EVERY user but the projects data is not loaded because you did not mention which user

This instead;

$results = User::with('projects')->find(1);

will return user 1 with the project data nested inside the User object.

Did you try what I showed?

$projects = $user->projects()->where('stat' <> $stat )->get();

Should give you all the projects related to the logged in user

Please or to participate in this conversation.