user4358665
user4358665

Reputation:

Relationships on Laravel

I'm working on repair platform using laravel 5.2 I've tryed many thing but I cant put it to work :/ hope someone can help me

this is my repair table

Schema::create('repairs', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('brand');
        $table->foreign('brand')->references('id')->on('brands');
        $table->integer('equipment');
        $table->foreign('equipment')->references('id')->on('equips');
        $table->string('model');
        $table->string('description');
        $table->integer('status');
        $table->foreign('status')->references('id')->on('statuses');
        $table->string('code');
        $table->string('notes');
        $table->timestamps();

    });

Then I have controller

public function index()
{
  $repairs = repair::all();
  return view('repair.index_repair',compact('repairs'));
}

and this is my model

class Repair extends Model

{

protected $fillable = ['brand','equipment','model','description','status','code'];

public function brand ()
{
    return $this->belongsTo('App\brands', 'id');
}

when I'm trying to print brands name like this

@foreach($repairs as $repair)
                <tr>
                  <th>{{$repair->brand->name}}</th>
                  <th>{{$repair->equipment}}</th>
                  <th>{{$repair->model}}</th>
                  <th>{{$repair->description}}</th>
                  <th>{{$repair->status}}</th>
                  <th>{{$repair->code}}</th>
                  <th>
                    {!! Form::open(array('route'=>['repair.destroy',$repair->id],'method'=>'DELETE')) !!}
                    {{ link_to_route('repair.edit','Edit',[$repair->id],['class'=>'btn btn-primary']) }}
                    |
                    {!! Form::button('Delete',['class'=>'btn btn-danger','type'=>'submit']) !!}
                    {!! Form::close() !!}
                  </th>

                </tr>

                @endforeach

I've got this error "Trying to get property of non-object". This is a very commun error and I see some topic with people having the same issue and and I try to use thier solution but doesnt work. I dont know what to do anymore.. Can anyone help me please?

Thank you

UPDATE

Now I have this:

Controller:

public function index()
{
  $repairs = repair::with('brands')->get();
  return view('repair.index_repair',compact('repairs'));
}

Repair model:

class Repair extends Model

{

protected $fillable = ['brand','equipment','model','description','status','code'];

public function brands ()
{
    return $this->hasOne('App\brands','brand','id');
}

}

Brands Model:

    class brands extends Model
{

protected $fillable = ['name'];

    public function repair () {

      return $this->belongsTo('App\Repair','brand','id');

    }


}

and it gives me this error "QueryException in Connection.php line 729: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'brands.brand' in 'where clause' (SQL: select * from brands where brands.brand in (1, 2))"

I try everything... :/

Upvotes: 1

Views: 54

Answers (1)

Buglinjo
Buglinjo

Reputation: 2077

You should have brand_id not brand in your table Laravel to figure out your key automatically. If you want to use brand than you should tell Laravel that it is brands foreign key:

public function brand ()
{
    return $this->belongsTo('App\brands', 'brand', 'id');
}

Upvotes: 1

Related Questions