I have two table's one user and another classrooms.
Here is my user model:
public function classroom()
{
return $this->hasMany('App\classroom');
}
And here is my classroom's model:
public function user()
{
return $this->hasMany('App\User');
}
When user create classroom then i store in classroom table id, user_id, class_name, section. I already view class data in my blade page. But i can't view user name who created the classroom data.
In my view page
#foreach($classrooms as $classroom)
<li><b>Class code--><i>{{$classroom->class_code}}</i></b></li>
<li><b>Subject--><i>{{$classroom->class_name}}</i></b></li>
<li><b>Section--><i>{{$classroom->section}}</i></b></li>
<li><b>created by--><i> ???? </i></b></li>
#endforeach
My controller
$classrooms=classroomModel::where('id',$id)->get();
$posts = Status::where('class_id' , $id)->orderBy('id', 'desc')->get();
return view::make('class',compact('posts','classrooms'));
If i want to view user name who was created classroom data then what will be the right code and where should i change?
in your controller:
$classrooms=classroomModel::where('id',$id)->with('user')->first();
in your view:
$classrooms->user->name;
Related
I want to show data from a database(sql) that is from a specific user.
I have 3 tables:
Users
Stores
Areas
These are the following relationship between tables:
User hasOne Area
Area hasMany Stores
Basically what I wanted to show is that every user has their own area that has many stores.
User model
function area() {
return $this->hasOne('App\Area');
}
Area model
function user() {
return $this->belongsTo('App\User');
}
function stores() {
return $this->hasMany('App\Store');
}
Store Model
function area() {
return $this->belongsTo('App\Area');
}
My database looks like this:
user table
id name role_id area_id
area table
id name user_id
store table
id name area_id
How can I access user->area->store?
This is what I got so far
function show() {
$id= Auth::user()->id;
$user = User::find($id);
echo($user->area->stores);
}
Thank you
you can use the hasManyThrough for get sotres of an area:
define stores function in User model
public function stores()
{
return $this->hasManyThrough(Store::class, Area::class);
}
and use it :
$stores = auth()->user()->stores;
I hope be useful.
i have two tables,
Student table
which have column (id, family_id,name,class,section)
Family table
which have column (family_id, mobile_no, profession)
--
i have created two models.
student model:
class student extends Model
{
}
and
family model
class family extends Model
{
public function student()
{
return $this->hasMany('App\student');
}
}
--
i am able to show all data from student table,
my controller is:
public function index()
{
$finddata = student::orderBy('id', 'asc')->get();
return view('students.index')->with('finddata', $finddata); }
--
what i tried
in family model:
return $this->hasMany('App\students');
what i want;
i want to connect family model with student model..
index page will have all students name only. which i have already done
when i click on student name it should show, all information about particular student, and his family information. right now it's only showing information from student table.
In the student model:
class student extends Model
{
public function family(){
return $this->belongsTo(Family::class,'family_id');
}
}
Then you can access family information in Blade by:
{{$student->family->mobile_no}}
{{$student->family->profession}}
You should better use artisan commands to generate models and controllers, so that you get automatic templates that follow naming conventions, such as uppercase Class names.
You should try this
class student extends Model
{
public function familyId(){
return $this->belongsTo(Family::class,'family_id');
}
}
when you click on student name it will redirect to student/{id}
route
Route::get('student/{id}', 'StudentController#show');
student controller
public function show(Request $request, $id)
{
$student = student::find($id);
return view('student.show')->with('student', $student);
}
student.show.blade file
Mobile no: {{ $student->familyId->mobile_no }}
Profession: {{ $student->familyId->profession }}
I have 3 tables users, companies and pivot table with user_id, company_id.
I can't get users, which belongs to my company inside User model.
Tried like
belongsToMany('App\User','companies_users','company_id','user_id' );
but I get relation with wrong users.
Since you are having a belongsToMany relationship between the User and Company, the User belongs to more than one Company. To get users of the companies of a particular User will not be straight forward. If you are sure that is exactly what you want, then do this:
//inside the User model
public function companies()
{
return $this->belongsToMany('Company');
}
//inside the User model
public function companiesusers()
{
$users= new Illuminate\Database\Eloquent\Collection;
foreach($this->companies as $company)
{
$users = $users->merge($company->users->get());
}
return $users->unique();
}
//inside the Company model
public function users()
{
return $this->belongsToMany('User');
}
Then you can get a user's companiesusers like so:
User::first()->companiesusers();
I have two tables/models. User and Advert.
When a user posts an advert their userid is a column in the database row. I want to use the id to get the user details and show it in a blade template.
These are the models
Advert belongs to a user.
class Advert extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
}
User has many adverts.
class User extends Authenticatable
{
public function advert(){
return $this->hasMany('App\Advert);
}
}
On the advert show page I want the users name and details printed out beside it. Currently I can only access the ID as that is stored as a attirbute in the advert column in the DB. How can I use this relatonship to access the users data.
This is the show controller
public function show($id){
//Shows add user clicked on based on id
$user = User::where('id', $id)->first();
$Advert = Advert::where('id', $id)->first();
return view('pages/advert/show', compact('Advert', 'user'));
}
In the show page view I can access the user id like so {{$user->id}}
You are using the $id passed into your show function for retrieving both the user and the advert.
public function show($id){
// Get the avert with the passed in id
$advert = Advert::find($id);
// From the advert get the user related to it.
$user = $advert->user;
return view('pages/advert/show', compact('advert', 'user'));
}
Take a look at https://laravel.com/docs/5.6/eloquent-relationships#querying-relations to get a better understanding of how eloquent works with relationships.
I'm doing a project for one of my University courses. But I'm finding it difficult to make relationship between User to User model.
Table: users
id
name
email
type
password
On this table the column type is used for classifying user types.
For example: User can be either distributor or dealer type.
And a distributor may have many dealer
But a dealer may have only one distributor.
To implement this I've created another table and named as dealers_of_distributor.
Table: dealers_of_distributor
id
distributor_id (id of users table)
dealer_id (id of users table)
Although I've used an additional model DelaersOfDistributor, the exact relationship should be between user to user.
How can I make this relationship. I'm confused!
I've tried so far:
Model: User
public function dealersOfDistributor(){
$this->hasMany('App\DealersOfDistributor');
}
Model: DealersOfDistributor
public function user(){
return $this->belongsTo('App\User');
}
The DistributorsController
public function index()
{
$distributors=User::where('type', 'distributor')->get();
$dealers=User::where('type', 'dealer')->get();
$assignedDealers=DealersOfDistributor::all();
return view('distributors.index')->withDistributors($distributors)->withDealers($dealers)->with('assignedDealers', $assignedDealers);
}
In blade file: I've tried $assignedDealer->dealer_id->user->name. But It's showing error.
Model DealersOfDistributor:
public function distributorUser()
{
return $this->belongsTo('App\User', 'distributor_id', 'id');
}
public function dealerUser()
{
return $this->belongsTo('App\User', 'dealer_id', 'id');
}
than you just get the user's data from DealersOfDistributor model, call function dealerUser if you want get user's data from dealer_id, and call function distributionUser if you want get user's data from distribution_id
Controller:
public function index()
{
$assignedDealers=DealersOfDistributor::all();
return view('distributors.index',compact('assignedDealers'));
}
Blade get username:
#foreach($assignedDealers as $assign)
{{$assign->distributorUser->name}}
{{$assign->dealerUser->name}}
#endforeach