0

I have 2 tables, Users and Statements. I need to display monthly statements for the currently logged user.

User Model

public function statements()
{
return $this->hasMany('\App\Models\Statement','account_number','name');
}

Statement Model

protected $primaryKey = 'account_number';

public $incrementing = false;

protected $keyType = 'string';
public function user()
{
return $this->belongsTo('\App\Models\User','account_number','name');
}

User Tabe

id
name // (ex: 01-234567 and used as username for login)
accname
email
password

Statement Table

id
account_number
month
bill
due

How can I display the monthly data in a datatable and also the current month data in a separate div in my blade file.

I'm new to laravel so kindly guide me.

Thank you in advance

1 Answer 1

1

If your relationship is already working you just need to get the Auth user and the statement. Something like this:

$user = Auth::user();
$statements = $user->statements // this is a statements collection;
Sign up to request clarification or add additional context in comments.

7 Comments

I've updated my question sir. I'm new so i don't quite get how to display the data on the blade file or do i need to put anything on controller and or model
In this case you dont really need to use the controller more than to return your desired view. On your blade file, you can access the data from the logged user with {{ Auth::user() }}. I suppose you need to display the statements on a table, so you just need to use a @foreach(Auth::user()->statements as $statement) do whatever you want here @endforeach
I recomend you to read laravel's documentation. Its understandable and has many examples
Thank you! it worked. Now, how can i display a data for a single row? let's say for the month of december for example.
you can use the method where() from laravel to manipulate collections. Just type Auth::user()->statements->where('month', 'december'). Read more about: laravel.com/docs/8.x/collections
|

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.