Reputation: 793
Brand new to Laravel and trying to work the ORM to get data from two tables joined via a Foreign Key. I don't know what I am missing but it must be something #%#, please help and be nice :)
I have two tables widgets
and widget_types
were widgets->widget_type_id
references widget_types->id
via a foreign key. Now when using a Controller to bring in this data...only the first object has a widget_type
parameter filled in:
HomePageController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Widget;
class HomePageController extends Controller
{
public function get() {
$data['page_title'] = "Home";
$data['widgets'] = Widget::where([ ['is_default', '=', 1], ['is_guest', '=', 1] ])->with('widget_type')->get();
return $data['widgets'];
return view('home', $data);
}
}
Now that first return statement returns something like this:
[{"id":1,"widget_type_id":1,"is_default":1,"is_guest":1,"widget_title":"Welcome to the App","widget_content":"What good is a reward if you ain't around to use it? Besides, attacking that battle station ain't my idea of courage. It's more like\u2026suicide. But with the blast shield down, I can't even see! How am I supposed to fight?","created_at":null,"updated_at":null,"widget_type":{"id":1,"class_name":"text-widget","template_name":"text_widget","friendly_name":"Text Widget","created_at":null,"updated_at":null}},{"id":2,"widget_type_id":1,"is_default":1,"is_guest":1,"widget_title":"What can I do here?","widget_content":"Still, she's got a lot of spirit. I don't know, what do you think? Kid, I've flown from one side of this galaxy to the other. I've seen a lot of strange stuff, but I've never seen anything to make me believe there's one all-powerful Force controlling everything. There's no mystical energy field that controls my destiny. It's all a lot of simple tricks and nonsense.","created_at":null,"updated_at":null,"widget_type":null}]
Notice the 2nd item has null
as a widget type
WHYYYY? I can confirm the tables are created correctly and I don't think I have the key flow reversed...(if I do...I'm sorry)
Widget.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Widget extends Model
{
protected $table = 'widgets';
public function widget_type()
{
return $this->belongsTo('App\Models\WidgetType', 'id');
}
}
WidgetType.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class WidgetType extends Model
{
protected $table = 'widget_types';
}
WidgetType Migration
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateWidgetTypesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('widget_types', function (Blueprint $table) {
$table->increments('id');
$table->string('class_name');
$table->string('template_name');
$table->string('friendly_name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('widget_types');
}
}
Widget Migration:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateWidgetsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('widgets', function (Blueprint $table) {
$table->increments('id');
$table->integer('widget_type_id')->unsigned();
$table->foreign('widget_type_id')->references('id')->on('widget_types');
$table->boolean('is_default');
$table->boolean('is_guest');
$table->string('widget_title');
$table->longText('widget_content');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('widgets');
}
}
Upvotes: 1
Views: 289
Reputation: 170
you need to specify the foreign key column
Widget.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Widget extends Model
{
protected $table = 'widgets';
public function widget_type()
{
return $this->belongsTo('App\Models\WidgetType', 'widget_type_id');
}
}
WidgetType.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class WidgetType extends Model
{
protected $table = 'widget_types';
public function widgets(){
return $this->hasMany('App\models\Widget','widget_type_id')
}
}
Then in your controller you can access it. HomePageController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Widget;
class HomePageController extends Controller
{
public function get() {
$data['page_title'] = "Home";
$data['widgets'] = Widget::where([ ['is_default', '=', 1],
['is_guest', '=', 1] ])->widget_type;
dd($data['widgets']);
// this is belongs to method
return $data['widgets'];
return view('home', $data);
}
}
please let me know if you get right
Upvotes: 2