0

How to List all rows from a table (agendas) in my DB where records are saved by the connected user.I'm using default Auth from Laravel.

public function index ($id = null)

    {
        $agendas = Agenda::where('id', Auth::user()->id)->get();
        $users = User::all();
        return view('admin.agendas.index', compact('agendas','users','id'));
    }

My controller here. Need help

4
  • 2
    Does agendas table have a user_id column linking it to a record on users table? What's the relationship defined between User and Agenda Model for eg: User hasMany Agenda and Agenda belongsTo User? Commented Dec 21, 2020 at 10:29
  • Use Relationships. Commented Dec 21, 2020 at 10:34
  • Agenda::where('id', Auth::user()->id) is getting an Agenda where the id is the user's id - which is wrong. Please post your relations Commented Dec 21, 2020 at 10:34
  • Hello Donkarnash, The Model Agenda and User haven't relationship. Let me try this. Thanks Commented Dec 21, 2020 at 10:43

3 Answers 3

1

Assuming

  • agendas table (containing records for Agenda model) has a column user_id which references the id column on users table
  • User hasMany Agenda
  • Agenda belongTo User

class User extends Model
{
    public function agendas()
    {
        return $this->hasMany(Agenda::class);
    }

    //... rest of class code
}


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

    //... rest of class code
}


public function index($id = null)
{

    $user = User::with('agendas')->findOrFail(auth()->id());

    return view('admin.agendas.index', [
        'user' => $user,
        'agendas' => $user->agendas,
        'id' => $id
    ]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Let me try this and return you as soon as possible. Many Thanks
Hello Donkarnash, Thanks you very much. It's working
0

Your User model should have a relationship:

public function agendas()
{
    return $this->hasMany(Agenda::class);
}

In your controller, you could then simplify the use as such:

public function index (Request $request, $id = null)
{
    $agendas = $request->user()->agendas;
    $users = User::all();
    return view('admin.agendas.index', compact('agendas','users','id'));
}

Comments

0

if you wanna get data related to another data , you have to join those tables together by a field. in this case i guess you have a column in Agenda table named 'user_id'.

you have two way :

  1. join tables in your Model.
  2. search in Agenda table in your controller

if you want to use joining your tables from model :

// in App\User.php
...
class User ...{
...

    public function Agenda(){
        return $this->hasMany(Agenda::class);
    }
...
}
...

then you can access to all of "Agenda" from everywhere like this : Auth()->user()->agenda

if you want to search in table from your controller you can do : Agenda::where('id', Auth::user()->id)

you can read more about eloquent-relationships in : https://laravel.com/docs/8.x/eloquent-relationships

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.