I have a bulletin system where posts can be made. What I need right now is a way to pull in the name of the creator. In the bulletin table, there is a created_by column that corresponds with the id in the users table. I've talked on IRC with some people about this but still can't seem to figure out the Eloquent relationships.
Here are my User and Bulletin models
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
public function tags()
{
//TO REMOVE RECORD
//Tag::find(1)->users()->detach();
return $this->belongsToMany('Tag');
}
public function createUser()
{
$password = Hash::make('secret');
}
public function bulletin()
{
$this->hasMany('Bulletin','created_by');
}
}
<?php
class Bulletin extends Eloquent {
protected $table = 'bulletin';
public function creator(){
return $this->belongsTo('User');
}
}
View:
@extends('layouts.bulletin')
@section('title')
@parent
::Bulletin
@stop
@section('navTitle')
@parent
Bulletin
@stop
@section('content')
@foreach($posts as $post)
<div class="col-md-12">
<div class="panel panel-info">
<div class="panel-heading">
<div class="pull-left">
<h4>{{ $post->title }}</h4>
</div>
<div class="pull-right">{{ $post->created_at }}</div>
<div class="clearfix"></div>
<p>{{ $post->created_by }}</p>{{-- Need full name, returns the int --}}
</div>
<div class="panel-body">
<p>{{ $post->body }}</p>
</div>
</div>
</div>
@endforeach
@stop
@section('scripts')
@stop
Controller:
<?php
class BulletinController extends BaseController {
/*
|--------------------------------------------------------------------------
| Default Home Controller
|--------------------------------------------------------------------------
|
| You may wish to use controllers instead of, or in addition to, Closure
| based routes. That's great! Here is an example controller method to
| get you started. To route to this controller, just add the route:
|
| Route::get('/', 'HomeController@showWelcome');
|
*/
public function showBulletin()
{
$creator = User::find(1)->creator;
$posts = Bulletin::all();
dd($creator);
return View::make('bulletin')->with(array('posts'=>$posts));
}
}
I've read through the relationships docs but still can't seem to get it, can somebody give me some tips? Thanks.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community