How can I resolve this problem of eloquant display data? - mysql

I want to do this request with laravel ( i have 3 tables users , userprojet and projets ) users hasmany projets and projet hasmany users that's why i do another table userprojet to join the two table and now i try to extract the projet of a specific user with this request
in model projei do this method:
public function users()
{
return $this->belongsToMany(User::class, 'user_projet');
}
//model user
public function projets()
{
return $this->belongsToMany(Projet::class, 'user_projet');
}
//in the controller projet i do this method
public function getProjectsUserConnecte(){
$user = User::with('projets')->first();
return $user->projets;
}
but any thing dosen't display and i have no error
please someone know the problem ?

Related

Storing password in Users table or Employees/Admin table

Let's say I have tables:
employees
administrators
(maybe) users
Employees and Administrators have different columns (e.g. Department, Salary column only in Employees).
Where should I store the password? I'm using Laravel, so the Users table is already created when I initialized the project. I'm torn between storing the password in both the Employees and Administrators, or only in the Users table, but I have no idea how I should implement the relationships of the tables if I were to add the users table.
I would probably keep the authentication associated with the users table, as per the default for laravel.
Here is an example of adding those relationships you would need.
// User model
public function employee()
{
return $this->hasOne(Employee::class, 'user_id', 'id');
}
public function administrator()
{
return $this->hasOne(Employee::class, 'user_id', 'id');
}
// Employee model
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
// Administrator model
public function user()
{
return $this->belongsTo(User::class, 'user_id', 'id');
}

Laravel eager load relationship with added clauses?

I have two related tables: Users & Images
class User extends Model
{
public function images()
{
return $this->hasMany('App\Images');
}
}
class Image extends Model{
public function user()
{
return $this->belongsTo('App\User');
}
}
I'm attempting to add a second method on the User model named thumbnails that would allow me to eager load a specific set of a users Images without having to load all of the users images first. Here is the logic:
public function thumbnails () {
return $this->hasMany('App\Images')
->selectRaw('src, user_id')
->where('processed', true)
->limit(3);
}
Here is how I've been calling this relation:
$posts = Post::with(['user','user.thumbnails'])->get();
using debugbar, I was examine the query:
"sql": "select src, user_id from \"images\" where \"processed\" = 1 and \"images\".\"user_id\" in (12, 14, 15) limit 3",
This only returns the user.thumbnails first Post model. Is there an issue with my thumbnails method ?
Your limit call isn't limiting 1 user's images to 3, it is limiting the entire query for all the posts' users' images to 3. It will only find 3 total images for all the users.
You can remove that limit for now:
public function thumbnails()
{
return $this->images()->where('processed', true);
}
You can call scope in inner query.
class User extends Model{
public function scopeThumbnails($query)
{
return $query->where('processed', true)
->limit(3);
}
}
in your controller
$posts = Post::with(['user' => function($query) {
$query->thumbnails();
}])->get();

eloquent relation with three tables in laravel 5.4

I have three Models named as Job , JobDetail and Customer.Job has many relation with JobDetail and Customer has many relation with Job.
Below is the structure of tables.
Job
id customer_id jobname
JobDetail
id job_id days
Customer
id name
Below are the models:
class Job extends Model
{
public function job_details()
{
return $this->hasMany('App\JobDetail','job_id','id');
}
}
class Customer extends Model
{
public function customer()
{
return $this->hasMany('App\Job','customer_id','id');
}
}
class JobDetail extends Model
{
//
}
i was trying to execute the below query but its throwing error like Call to undefined relationship [customer] on model [App\Job].
My query :
$data = Job::with(['job_details','customer'])->get();
Can some body suggest me how do i connect these models and get the data ?
Thank You !
You did not specify a relationship for customer in your Job model.
So, something like this should do the trick:
public function customer()
{
return $this->belongsTo('App\Customer','job_id','id');
}

How to get the object for has one

i have a model request that has a foreign key to category and a collection of buyers
class Request extends Model {
// TABLE NAME
protected $table = 'requests';
// MASS ASSIGMENTS
protected $guarded = ['id'];
// FK
public function category()
{
return $this->hasOne('App\Objects\Category', 'id');
}
// FK
public function buyers()
{
return $this->hasMany('App\Objects\RequestBuyer');
}
}
i have a query that get's all request records with the collection of buyers
$requests = Request::with('buyers')->get();
this return a json that contains all records with the collection of buyers but i want to include the category details in this json response.
How do i accomplish this?
do as following,
$requests = Request::with('buyers', 'category')->get();
By the way what is the relationship between Request and Category?

Laravel Eloquent relations (need advice)

I am having problem with Laravel Eloquent relations i understand how they work but i dont know how to "use" them properly, so i need some guidance/pointers.So here goes.
I have Exam table
Schema looks like (thanks to lukasgeiter)
exams
id
title
duration
questions
id
text
exam_id
answers
id
text
question_id
correct (boolean)
Relations:
Exam Model
public function questions(){
return $this->hasMany('Question');
}
Question Model
public function answers(){
return $this->hasMany('Answer');
}
public function exam(){
return $this->belongsTo('Exam');
}
Answer Model
public function question(){
return $this->belongsTo('Question');
}
And i understand this part but now i want users to be able to solve exam and store that data (i need to save an answer from user, for example user_id 1, exam_id 2, question_id 1, answer true). i have done it this way but i think its wrong (Yeah it does work but i dont think its the right way)
Schema looks like
Users
id
email
pass
...
SolvedExams
id
user_id
exam_id (havent put relation here not sure if needed)
solved (boolean) // if its completed right or wrong
SolvedQuestions
id
exam_id (havent put relation here not sure if needed)
answer(boolean)(then later i check this boolean with answers) //if the answer is right or wrong
Now with relations i have done same as i said before
User Model
public function SolvedExams() {
return $this->hasMany('SolvedExams');
}
SolvedExam model
public function User() {
return $this->belongsToMany('User');
}
public function questions() {
return $this->hasMany('solved');
}
SolvedQuestions model
public function exam() {
return $this->belongsTo('SolvedExam');
}
Is this the right way or am i doing it wrong (and I am a begginer with relations)
I think you are pretty close...
I'd do it this way:
Tables
exams
id, title, duration
questions
id, text, exam_id
answers
id, text, question_id, correct
users
id, email, password
tries
id, user_id, exam_id
answers_try (pivot table)
id, try_id, answer_id
Relations
Exam
public function questions(){
return $this->hasMany('Question');
}
public function tries(){
return $this->hasMany('Try');
}
Question
public function answers(){
return $this->hasMany('Answer');
}
public function exam(){
return $this->hasMany('Exam');
}
Answer
public function question(){
return $this->belongsTo('Question');
}
public function tries(){
return $this->belongsToMany('Try');
}
User
public function tries(){
return $this->hasMany('Try');
}
Try
public function answers(){
return $this->belongsToMany('Answer');
}
public function user(){
return $this->belongsTo('User');
}
public function exam(){
return $this->belongsTo('Exam');
}
Usage
Get the users answer for a question
$answer = User::find(1)
->tries()->where('exam_id', 2)->first()
->answers()->where('question_id', 3)->first();
Creating a new exam
$exam = new Exam;
$exam->save();
$question = new Question;
$question->text = 'Foo?';
$exam->questions()->save($question);
$answer1 = new Answer;
$answer1->text = 'Foo!';
$answer1->correct = true;
$answer2 = new Answer;
$answer2->text = 'Bar!';
$answer2->correct = false;
$question->answers()->saveMany([$answer1, $answer2]);
Saving users answer
$exam = Exam::find(1);
$user = Auth::user();
$try = new Try;
$try->user()->associate($user)->save();
$exam->tries()->save($try);
$try->answers()->attach(2); // 2 is the answer id