Search code examples
phplaravellaravel-5eloquentlaravel-5.3

Laravel eloquent relationship user id issue


I currently have many tables but for simplicity i am going to show the two affected ones. They consist of:

  • Users
  • Announces

One user can have many announces and one announce only belongs to one user. It seems like a Many to One Relationship (or so i think).

Schema creation for database:

Schema::create('X_USER', function($table)
    {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->rememberToken();
        $table->timestamps();
    });

Schema::create('X_ANNOUNCE', function (Blueprint $table) 
    {           
        $table->increments('id');
        $table->integer('created_by')->unsigned();
        $table->foreign('created_by')->references('id')->on('X_USER')->onDelete('cascade');
        $table->timestamps();
    });

Now the models are also quite simple I have this on User model:

public function announces()
{
    return $this->hasMany('App\Announce', 'created_by');
}

And this on the Announce Model:

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

When I insert an announce i do the following on my laravel AnnounceController.php (store function):

$announce = Announce::create($request->all());

return Response::json([
            'message' => 'Announce Created Succesfully',
            'data' => $this->transform($announce)
    ]);

This previous line actually inserts everything in database correctly. Nothing wrong so far until I do the "transform" that returns the json in the format I would want to pass to the frontend:

private function transform($announce)
{
    //Get User
    $user = Announce::find($announce['created_by'])->user;

    return [
           'announce_id'            => $announce['id'],
           'announce_title'         => $announce['title'],
           'announce_user_id'       => $user->id,
           'announce_created_by'    => $user->name,
        ];
}

When I do $user = Announce::find($announce['created_by'])->user I never get the correct id ($user->id). However, $user->id and $user->name are coherent, just not the correct ones. I have checked that the id ($announce['created_by']) is correct as well.

Any help is greatly appreciated, I understand that this question is not the best asked question but I am going sort of nuts after trying for way too many hours, checking stack and tutorials.


Solution

  • When you are using find method you are searching by id, and I assume, the correct way would be:

    $user = Announce::where('created_by', $announce['created_by'])->first()->user;
    

    But in fact it does not make much sense here I think, because for transform method it should be enough to use code like this:

    Also you should be able to use code like this for tranform method:

    private function transform($announce)
    {
        return [
               'announce_id'            => $announce->id,
               'announce_title'         => $announce->title,
               'announce_user_id'       => $announce->user->id,
               'announce_created_by'    => $announce->user->name,
            ];
    }