Get values from belongsTo() Laravel 5

laravel belongsto not working
hasmanythrough laravel example
laravel relationship with condition
with in laravel
laravel get belongsto data
laravel wherehas
laravel relationship tutorial
one to one relationship in laravel example

i'm having a bit of a problem with Laravel 5 and Eloquent. I have 3 models userInfo, tasksAssign, and taskCreated. The User model:

public function tasksAssign()
{
    return $this->hasMany('App\Tasks', 'fk_user_id_assign', 'id')->where('status',  2);
}
public function tasksCreated()
{
    return $this->hasMany('App\Tasks', 'fk_user_id_created', 'id');
}

The tasks model

public function userInfo()
{
    return $this->belongsTo('App\User');
    //return $this->hasOne('App\User', 'id', 'id'); The hasOne here is just me testing diffrent stuff to see if i could get it to work.
}

Shoulndt i be able to get the value from my User (WHERE id = id in the URL) to show up in my tasks show view with the belongsTo()? like so: {{$tasks->userInfo->name}} or am i totally off here?


Update: Current Taskscontroller:

public function show($id)
    {
        $tasks = Tasks::findOrFail($id);
        $assignee = $tasks->assignee;
        return view('tasks.show')->withTasks($tasks);
    }

and my view {{$tasks->assignee}}, but its not showing anything, also i am trying to get the info of the assignee, but thanks for all the examples, they will be usefull later on. and the Model:

public function assignee() {
        return $this->belongsTo('App\User', 'fk_user_assign_id', 'id');
    }

Okay, so I'm assuming your schema looks like this:

Schema::create('user', function($table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

Schema::create('task', function($table) {
    $table->increments('id');
    $table->integer('fk_user_id_assign')->unsigned();
    $table->foreign('fk_user_id_assign')->references('id')->on('user');
    $table->integer('fk_user_id_created')->unsigned();
    $table->foreign('fk_user_id_created')->references('id')->on('user')
    $table->timestamps();
});

In English, a task has a creator, and has an asignee. I'm not sure which you're trying to access with the ->userInfo() relation, so I'll describe the way to access either.

Because you're not using the name user_id as the foreign key on the task table, you'll need to specify which foreign key you want to load users from.

class User extends Eloquent {
    public function createdTasks() {
        return $this->hasMany(Task::class, 'fk_user_created_id', 'id');
    }

    public function assignedTasks() {
        return $this->hasMany(Task::class, 'fk_user_assign_id', 'id');
    }
}

class Task extends Eloquent {
    public function creator() {
        return $this->belongsTo(User::class, 'fk_user_created_id', 'id');
    }

    public function assignee() {
        return $this->belongsTo(User::class, 'fk_user_assign_id', 'id');
    }
}

To get all tasks created by a given user, use

$tasksCreatedByUser = $user->createdTasks;

To get all tasks assigned to a given user, use

$tasksAssignedToUser = $user->assignedTasks;

To get the user a task was created by, use

$creator = $task->creator;

To get the user a task is assigned to, use

$assignee = $task->assignee;

Finally, if you want to do something like list the assignee of every task created by a user, do something like

$tasksCreatedByUser = $user->createdTasks;
foreach($tasksCreatedByUser as $task) {
    $assignee = $task->assignee;
    echo "Task #{$task->id} was created by user #{$user->id} and assigned to user #{$assignee->id}\n";
}

If the parent model has a ->hasOne() or ->hasMany() relation, then the child model must have a complementary ->belongsTo() relation. In this case, the parent model is the User model and the child model is the Task model.

Eloquent: Relationships - Laravel, The save Method; The create Method; Belongs To Relationships; Many To Many Once the relationship is defined, we may retrieve the related record using Additionally, Eloquent assumes that the foreign key should have a value matching� I have this table with 4 id's related to other tables. Table List: id table1_id table2_id table3_id table4_id 1 2 3 2 1 2 1 4 3

You suppose to have a user_id field in your tasks table where user_id should refer to the id column in the users table, for example:

// App\Task.php
public function userInfo()
{
    // Laravel'll assume 'user_id' (fk) is available in tasks table
    return $this->belongsTo('App\User');
}

Alternatively, you may explicitly tell the local key name using:

// App\Task.php
public function userInfo()
{
    return $this->belongsTo('App\User', 'the_key_that_refers_to_users_table');
}

How to call value from belongsTo Relationship to hasMany, 1 of 5 replies November 17, 2018 How to call value from belongsTo Relationship to hasMany public function strand() { return $this->belongsTo(' App\Strand'); } public function section() { return But that is not why you would be getting an undefined variable error. Proudly hosted with Laravel Forge and DigitalOcean. A hasOne() relationship matches 'this' model's 'id' to a 'foreignKey' in the related table. A belongsTo() relationship matches a related table's 'id' to a 'localKey' field on 'this' model. Another way to think about it is the belongsTo() relationship should live on the model that has the field that links to the related tables id.

when you use belongsTo Laravel will assume the forgein key is 'table_id' and when use haseOne or hasMany Laravel will assume it 'tables_id' with s then to specify your own chose you need to do this

public function userInfo(){
    return $this->belongsTo('App\User', 'your_onw_key');
}

belongsTo returns id only, not entire object, belongsTo returns id only, not entire object. Posted 5 years ago by selim You can do ->get()->first() so you can do $reservation->client odd so I think that only Jeffrey Way and other Laravel veterans like you could be able to solve it. property), but returning client field value, which happens to be the id. By default, Laravel will use the fully qualified class name to store the type of the related model. For instance, given the one-to-many example above where a Comment may belong to a Post or a Video , the default commentable_type would be either App\Post or App\Video , respectively.

Did You Know: Five Additional Filters in belongsTo() or hasMany , Did You Know: Five Additional Filters in belongsTo() or hasMany() 5. withDefault() to Prevent the Errors. If you try to reference the parent entry that you will get error, something like trying to get property of non-object. But you can override even the default values, by specifying them as parameters: The get method is responsible for transforming a raw value from the database into a cast value, while the set method should transform a cast value into a raw value that can be stored in the database. As an example, we will re-implement the built-in json cast type as a custom cast type:

Get values from belongsTo() Laravel 5, Get values from belongsTo() Laravel 5. i'm having a bit of a problem with Laravel 5 and Eloquent. I have 3 models userInfo, tasksAssign, and� Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works on all supported database systems. The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks.

Laravel (5.7) Eloquent Relationships, In addition, Eloquent assumes that the foreign key has a value matching the id (or the public function user() { return $this->belongsTo('App\User'); } } Once the relationship is defined, we can retrieve the Post model for a particular select * from books select * from authors where id in (1, 2, 3, 4, 5, ). Laravel provides an updateOrCreate method to do this in one step. Like the firstOrCreate method, updateOrCreate persists the model, so there's no need to call save() : // If there's a flight from Oakland to San Diego, set the price to $99.

Comments
  • What is the name of the column that the users ID is stored in within the tasks table?
  • fk_user_assign_id is the one in the tasks table
  • I don't know if it's a typo in your actual code, or just in the question, but in your hasMany, you have the fk field name as fk_user_id_assign, but in the belongsTo you have it as fk_user_assign_id.
  • That was it Thank you! :)
  • So i'm thinking im starting too catch up a bit here, Edited my answer, for better formating. Thanks for the help so far! Helped me understand it a bit more.
  • Thanks! I tried it a few times before, but I coulndt get it output to my view, Do i have to refer to it in my TasksController aswell somehow? or should i work directly with the current Controllers code $tasks = Tasks::findOrFail($id); return view('tasks.show')->withTasks($tasks);
  • Thanks for the help, there we're an error in the spelling thats why it dindt work.