php - Laravel eloquent relationship user id issue -


i have many tables simplicity going show 2 affected ones. consist of:

  • users
  • announces

one user can have many announces , 1 announce belongs 1 user. seems many 1 relationship (or think).

schema creation 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 models quite simple have on user model:

public function announces() {     return $this->hasmany('app\announce', 'created_by'); } 

and on announce model:

public function user() {     return $this->belongsto('app\user', 'created_by'); } 

when insert announce following on laravel announcecontroller.php (store function):

$announce = announce::create($request->all());  return response::json([             'message' => 'announce created succesfully',             'data' => $this->transform($announce)     ]); 

this previous line inserts in database correctly. nothing wrong far until "transform" returns json in format want pass 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 $user = announce::find($announce['created_by'])->user never correct id ($user->id). however, $user->id , $user->name coherent, not correct ones. have checked id ($announce['created_by']) correct well.

any appreciated, understand question not best asked question going sort of nuts after trying way many hours, checking stack , tutorials.

when using find method searching id, , assume, correct way be:

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

but in fact not make sense here think, because transform method should enough use code this:

also should able use code 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,         ]; } 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -