0

i new in laravel development, i have code problem.

i have MotorsController.php like this

class MotorsController extends BaseController{
  protected $motor;
  public function __construct(Motor $motor){
    $this->motor = $motor;
  }

  public function index(){
    $motors=$this->motor->with('motorcategory', 'motorcolor')->get();
    return View::make('motors.index', compact('motors'));
  }

and i have model like below

Motor.php

<?php

 class Motor extends Eloquent {
protected $table = 'motors';
protected $guarded = array();

public function motorcategory(){
    return $this->belongsTo('MotorCategory');
}
public function motorcolor(){
    return $this->belongsTo('MotorColor');
}

public static $rules = array(
    'name' => 'required',
    'police_number' => 'required',
    'sex' => 'required'

);
}

and MotorCategory.php

<?php

class MotorCategory extends Eloquent {
    public function motor(){
        return $this->hasMany('Motor');
    }
}

and MotorColor.php

class MotorColor extends Eloquent {
    public function motor(){
        return $this->hasMany('Motor');
    }
}

in motors.index i want to get motor data which have relation with table motorcolor and motorcategory,

but now i still have error like:

ErrorException (E_UNKNOWN)

Trying to get property of non-object (View: C:\Users\LalatTempur\Laravel\ironhorse\app\views\motors\index.blade.php)

i hope someone can help me.. by the way,, sorry for my english :)

my motors.index

<tbody>
                            @foreach ($motors as $motor) 
                            <tr>
                                <td>{{ $motor->name }}</td>
                                <td>{{ $motor->police_number }}</td>
                                <td>{{ $motor->motor_category->name }}</td>
                                <td>{{ $motor->motor_color->name }}</td>

                                <td>{{ $motors->purchase_date}}</td>
                                <td>{{ $motors->status }}</td>
                                <td>
                                    {{ link_to_route('motors.show', 'Detail', array($motor->id), array('role' =>'btn', 'class' => 'btn btn-primary btn-xs')) }}&nbsp; | &nbsp;
                                    {{ link_to_route('motors.edit', 'Edit', array($motor->id), array('role' =>'btn', 'class' => 'btn btn-info btn-xs')) }}&nbsp; | &nbsp;
                                <!--    {{ link_to_route('members.destroy', 'Delete', array($member->id), array('role' =>'btn', 'class' => 'btn btn-danger btn-xs')) }}&nbsp;&nbsp;-->
                                <!-- </td>
                                <td> -->
                                {{ Form::open(array('method' => 'DELETE', 'route' => array('motors.destroy', $motor->id), 'style'=>'display:inline;' )) }}
                                        {{ Form::submit('Delete', array('class' => 'btn btn-danger btn-xs ')) }}
                                    {{ Form::close() }} 
                                </td>
                            </tr>
                            @endforeach
                        </tbody>
1
  • Does every motor have a category and color assigned in your database? Commented Dec 26, 2014 at 23:37

1 Answer 1

1

You are accidently using the plural of motor inside your foreach:

<td>{{ $motors->purchase_date}}</td>
<td>{{ $motors->status }}</td>

Should be:

<td>{{ $motor->purchase_date}}</td>
<td>{{ $motor->status }}</td>

Although the error message doesn't really fit well, because a collection would be an object too.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.