-1

My Channels Model is :

  class Channels extends Model
    {

      protected $fillable = ['title','slug'];

      public function Discussion()
      {
          return $this->hasMany('App\Discussion');
      }

    }

Channel Model is:

 class Channels extends Model
    {
        protected $fillable = ['title','slug'];

        public function Discussion()
        {
            return $this->hasMany('App\Discussion');
        }

    }

Channel Migration File :

class CreateChannelsTable extends Migration
{

    public function up()
    {
        Schema::create('channels', function (Blueprint $table) {
          $table->increments('id');
          $table->string('title');
          $table->string('slug');
          $table->timestamps();
        });
    }


    public function down()
    {
      Schema::dropIfExists('channels');
    }

}

Discusion Migraion file

class CreateDiscussionsTable extends Migration
{

    public function up()
    {
        Schema::create('discussions', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->integer('channel_id')->unsigned();
            $table->string('title');
            $table->text('content');
            $table->text('slug');
            $table->timestamps();
        });
    }


    public function down()
    {
        Schema::dropIfExists('discussions');
    }

}

**Controller is: **

class HomeController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }


    public function channel($slug)
    {
        $channel= Channels::where('slug',$slug)->first();
        return view('channel')->with('discussion',$channel->Discussions);
    }

}

And Finnaly the route is

Route::get('channel/{slug}',[
'uses' => 'HomeController@channel',
'as' => 'channel']);

**Now i am trying to fetch the data: **

$channel->Discussions

but it's giving me the error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'discussions.channels_id' in 'where clause' (SQL: select * from discussions where discussions.channels_id = 1 and discussions.channels_id is not null)

I don't know what exactly to do now. Please Help me

Md.Sukel Ali
  • 2,987
  • 5
  • 22
  • 34
Osman Rafi
  • 938
  • 1
  • 16
  • 37
  • 2
    It's because your model classname is Channels instead of Channel so laravel take the name of the class and add `_id` so you have to specify the name of the join key in the `hasMany` method `return $this->hasMany('App\Discussion', 'foreign_key', 'local_key');` or rename your `Channels` class to `Channel` [see](https://laravel.com/docs/5.7/eloquent-relationships#one-to-many) – Julien METRAL Jan 31 '19 at 09:35
  • Thanks @JulienMetral. It works ! – Osman Rafi Jan 31 '19 at 16:54

1 Answers1

1

First of all, it's very important to name your classes and its relationships appropriately.

Laravel makes some assumptions about your foreign keys based on your class names and relationship definitions. You could also specify the foreign keys if you want.

However, here's how i'd do it.

class Channel extends Model
{
    protected $fillable = ['title','slug'];

    public function discussions()
    {
        return $this->hasMany('App\Discussion');
    }
}

Notice here the model name is called Channel. When we have this class name and relationship, Laravel will assume that the discussion model has a foreign key called channel_id which it does.

You also need to define an inverse relationship in your Discussion model

class Discussion extends Model
{
    public function channel()
    {
        return $this->belongsTo('App\Channel');
    }
}

Now, doing the following would work:

public function channel($slug)
{
    $channel = Channel::whereSlug($slug)->first();

    return view('channel')->with('discussion', $channel->discussions);
}

If you are tied and cannot change the model name for whatever reason, then you need to specify a foreign key on your relationship definitions. For example:

public function discussions()
{
    return $this->hasMany('App\Discussion', 'channel_id');
}

public function channel()
{
    // This one can stay the same as Laravel will try to match `channel_id` 
    // on the discussion table to the id on the channels table
    return $this->belongsTo('App\Channel');
}

Fore more information, read about Eloquent Relationships.

Sorry, I was already writing this up when @Julien Metral commented, but this is an extension of what he already said :)

Mozammil
  • 8,520
  • 15
  • 29