The problem is that i have 3 database tables
1st table of users with user_id
2nd table is attribute with attribute_id and attribute_name
And 3rd one is Rating table having user_id and attribute_id
i want to get attribute_name from 2nd table using attribute_id after click on the specific user? Is it possible? If answer is Yes then please give me a suggestion how yo do this? thanks sir in advance?
Here i'm upload the image that after i click on specific user for edit display its record from user table and attribute_id from attribute table using many to many relation and problem is that we get attribute_name of specific id from Rating table using the attribute_id?? Is It Possible?
Its Edit form and below submit button get id from attribute table but we want to display attribute_name from rating table
Users model:
public function rating()
{
return $this->belongsTo(Rating::class);
}
Rating model:
public function attribute()
{
return $this->hasMany(Attribute::class, 'attribute_id');
}
Then call it in controller:
dd(Users::with('rating.attribute')->where('user_id', 1));
Query::
$records = DB::table('attributes')->join('ratings','attributes.id','=','ratings.attribute_id')->where('user_id',$id)->get();
return view('employee.edit',compact('records'));
Your EDIT VIEW PAGE
#foreach($records as $record)
<h4>Attributes ==>{{$record->attribute}}</h4>
#endforeach
Related
I have a pivot table. The name is users_banks table. The table have field id, user_id, bank_id, status, account_name and account_number
Now, the pivot have 3 records
I display contain from pivot table using code like this :
$user_id = auth()->user()->id;
$user = $this->user_repository->find($user_id);
$account = array();
foreach($user->banks as $bank) {
$account[] = $bank;
}
dd($account);
The result display all record in the form of array. There exist value of field user_id, bank_id, status etc. But I don't find value of id
How can I find id from pivot table?
If the relationship is defined correctly you can use:
$bank->pivot->id
but make sure the user belongsToMany(Bank::class) and the bank belongsToMany(User::class) or else it wont work.
I have two models: Report and User. This is what I have in my User model
public function userReport() {
return $this->hasMany('App\Report', 'user_id','id');
}
And this is in Report model
public function user() {
return $this->hasOne('App\User', 'id', 'user_id');
}
Controller
public function details( $item_id ){
$report = Item::find($item_id)->report;
return view('details', compact('report'));
}
In view
{!! $report->user->name !!}
In view I show all the users who are reported single Item .. which I query by $item_id.
The problem is that if same user is reported single item 1+ time I see this user 1+ time in the table on the page.
Is it possible to somehow grouped by user_id or count the user_id and show User1 ( 4 time reported ) ...? Where should I group them?
Update: users table
id | username | password
reports table
report_id | id | user_id | date
item table
id | user_id | category | date_added | image
Update2: Image of records in db. Here user with user_id=3 has reported item_id=14 total of 14 times. User with user_id=5 has reported same item_id=14 total of 3 times. So I should see on page user_id=3 ( 14 ) and user_id=3 ( 3 ). Instead I see 17 times user_id=3. Bellow are images
And on page
There should be several ways how to solve your problem
One way is (your controller should look like)
public function details( $item_id ){
$report = Item::find($item_id)->report->unique('user_id');
return view('details', compact('report'));
}
Second way should be to use #foreach in view and there check for unique values.
Third way should be to use foreach in controller and prepare unique data with calculated summarizes inside controller and then pass that prepared data to view.
Which soulution you want to use is just a matter of choice.
Hope it helps you
Just try this. Hope it helps
Report::where('item.id', $item_id)
->select('item.*','users.*',DB::raw('count(reports.report_id) as total'))
->join('item', 'item.id', '=', 'reports.id')
->join('users', 'users.id', '=', 'reports.user_id')
->groupBy('reports.report_id','reports.id')
->get();
I have a table which stores the user's interested topics along with the user id and topic id. The table will look like
Topic name topic id User-id
Sports 1 1
Education 2 3
Family 3 1
What i want to do is when a new user , say user-id '3' adds his interest as topic sports , then the first row should look like this
Topic name topic id User-id
Sports 1 1,3
When i try to update it overwrites the whole column like
Topic name topic id User-id
Sports 1 3
What is the right approach to do this ?
The right approach is to use Many-To-Many relationship:
Tables:
users (id, ....);
topics (id, ....);
topic_user (topic_id, user_id);
PHP:
class User extends Model {
public function topics() {
return $this->belongsToMany('App\Topic');
}
}
and :
class Topic extends Model {
public function users() {
return $this->belongsToMany('App\User');
}
}
For more details please refer to https://laravel.com/docs/5.2/eloquent-relationships.
I hope this will help.
So I have 2 tables, TABLE1 and TABLE2.
They are linked throug Eloquent:
TABLE1 has many TABLE2's, and TABLE2 has one TABLE 1.
They are linked with foreign keys as such:
in TABLE2 table1_id -> links to -> TABLE1 id.
How do I retrieve the entire table data or TABLE 2, + replace table1_id with a column of table1 (let's say by booktitle for example)?
Sorry for the abstraction, but this is the best way I can explain it? :D
The easiest way is to make use of Eloquent's eager loading and fetch all records from table2 and their corresponding record in table1.
This will do the trick:
$results = Table2::with('table1')->get();
foreach ($results as $table2record) {
echo $table2record->id; //access table2 data
echo $table2record->table1->booktitle; //access table1 data
}
You need to make the relationship in the models , for example if you have a "Category" and this has many "Products" you need to put this code in your model
public function products()
{
return $this->hasMany('App\Product', 'foreign_key_you_have');
}
and when you need to retrieve all products associated to that category in your view you need to call the function of model, for example
#foreach($category->products as $cp) // with $category->products you call all products associated to your foreign_key
<h1>{{$cp->price}}</h1>
#endforeach
I have Users and Courses.
I have a users table, and a courses table.
I also have a course_memberships table with id |
course_id | user_id
I have the appropriate hasmany relationship
for courses and users to relate to CourseMembership
But I have no idea how to create that relationship using a button on the front end. Something like a public function register() on the Courses controller that would put the Auth User ID in the user_id field, and the course id in the course_id field to form the relationship.
I know it was a wall of text, but I figured a description of the issue may be more helpful than an endless scroll of code.
Can anyone help?
You should create a method to save to the CourseMembership model within the Courses Controller. Something like this would work.
public function register($course_id){
$user_id = $this->Auth->user('id');
$data = array(
course_id => $course_id,
user_id => $user_id;
);
if($this->Courses->CourseMembership->save($data)){
$this->Session->setFlash('Registered');
$this->redirect($this->referer());
}
else{
$this->Session->setFlash('Could not register');
$this->redirect($this->referer());
}
}
This way, when people go to /Courses/register/1, it would register them for the course with the ID of 1.
Then when you do a find on the User, the data would be in $user['CourseMembership']