简体   繁体   中英

Return results from two tables joined by foreign key in Laravel.

I have two tables in MySQL where one is referencing another.

The first one is the users table

在此处输入图片说明

The second table is the event table which contains the creator_id referencing the id in the users table. so creator_id is the foreign key.

在此处输入图片说明

Now my problem is how to get the name of each user that owns each column in the event table using laravel.

I'm clueless about how to do this. And the laravel doc I've read so far is not helping.

This is the model for the event table.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Auth;
use App\signupModel;


class eventModel extends Model
{
    //


      public $table = "event";

      protected $fillable = ['title', 'description', 'event_flier', 'event_logo', 'org_name', 'org_logo', 'free', 'state', 'city' ];

      public function getUser(){
        $id = Auth::user()->id;
        return users::where('id', $this->id); 

      }


}
?>

The model for the users is:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class signupModel extends Model
{
    //

      public $table = "users";
      protected $primaryKey = 'id';
     protected $fillable = [
        'first_name', 'last_name', 'email', 'password', 'gender', 'phoneNumber', 'twitter', 'facebook', 'instagram', 'dob' 
    ];
}

I just want a way where each user can see his data from the event table when logged in using laravel.

please help. Thanks.

Here belongsTo is used return the creator of the event.

namespace App;

use Illuminate\Database\Eloquent\Model;
use Auth;
use App\signupModel;

class eventModel extends Model
{
      public $table = "event";

      protected $fillable = ['title', 'description', 'event_flier', 'event_logo', 'org_name', 'org_logo', 'free', 'state', 'city' ];

      public function getUser(){
        return $this->belongsTo('App\signupModel', 'creator_id');
      }

}
?>

In your controller, you can retrieve the creator of an event as follows

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\eventModel;
use App\signupModel;

class HomeController extends Controller
{
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $event = eventModel::find(1);
        $creator = $event->getUser;  // return user instance
        dump($creator); // for check result
        echo $creator->first_name;  //print firstname of creator
    }
}

you need to do in user model

public function getEvents(){
        return $this->hasOne('Your event model', 'creator_id');
      }

and import model in your controller and try like that

$events=User::with('getEvents')->get();

Or if you want to fetch one record you should try this in your controller

$events=User::find('Your Auth Id');
$creators=$events->getEvents;

Or you should try this one also

$users = DB::table('users')
            ->leftJoin('events', 'users.id', '=', 'events.creator_id')->where('users.id','=','Your user id')
            ->get();

An Event belongs to its Owner(User)

If you follow the core Laravel syntax, the Event model must look like this:

class Event extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

And the User model must be like:

class User extends Model
{
    public function event()
    {
        return $this->hasOne('App\Event');
    }
}

And if so, you can use:

$events = App\\User::find(1)->event;

This is a common case and you may use your logic according to your relationship type. Here is the doc .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM