Support the ongoing development of Laravel.io →
Eloquent Views
Last updated 2 years ago.
0

I think you're foreach loop is wrong in the HTML.

Try...

@foreach ($books as $book)

Notice how I pluralized $books.

http://php.net/manual/en/control-structures.foreach.php

You'll need to tweak your controller too so it matches $books.

0

I still get Trying to get property of non-object error. I'm still confused.

0

Try removing the compact around your books array and edit your original post with how your code looks now please.

0

A few more things...Can you post the whole error message, sometimes it tells you a file and a line number. Can you also post your routes.php file too please.

Lastly (and I could be wrong about this)...do you mean to use belongsTo instead of hasOne in the following snippet, similar to what you did in your Photo model:

public function photo(){
    return $this->hasOne('App\Photo','book_id');
}
0

Sorry for the late reply man erro goes like this this is from the admin side but same problem Error

like this one below if you can't see it.

Trying to get property of non-object (View: C:\xampp-2\htdocs\mypharma\resources\views\admin\partials\medicines-list.blade.php) (View: C:\xampp-2\htdocs\mypharma\resources\views\admin\partials\medicines-list.blade.php)

my Controller

  public function getMedicineList(){
    $dosages = $this->dosage->all();
    return view('admin.show-medicines', compact('dosages'));
  }

my models

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Dosage extends Model
{
	protected $table ='dosages';

	protected $fillable = ['dosage_name', 'medicine_id', 'price'];
	
	public function medicine(){
		return $this->belongsTo('App\Medicine');	
	}
	
	public function photo(){
		return $this->hasOne('App\Photo');
	}

}

Photo.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Photo extends Model
{
	protected $table = 'photos';
	protected $fillable = ['dosage_id','file_name','file_size','file_mime','file_path','created_by'];

	//get the dosage picture
	public function dosage(){
		return $this->belongsTo('App\Dosage');
	}
}

my view

  @foreach($dosages as $dosage)
                                            <tr>
                                                <td>{{$dosage->medicine->generic_name}}</td>
                                                <td>{{$dosage->dosage_name}}</td>
                                                <td>{{$dosage->photo->created_by}}</td>
                                                <td>
                                                    <a href="" data-target = "#editDosage" data-toggle="modal" class="btn btn-default btn-sm btn-icon icon-left">
                                                        <i class="entypo-pencil"></i>
                                                        Edit
                                                    </a>  <a href="{!! route('deleteDosage', $dosage->id) !!}"  data-target="#deleteDosage" class="btn btn-danger btn-sm btn-icon icon-left">  <i class="entypo-cancel"></i>
                            Delete
                     </a>
                                                </td>

                                            </tr>
                                            @endforeach

The medicine relationship works fine but when i call $dosage->photo->created_by it doesn't even display the id of the one who created it. Needs your help badly sir.

Last updated 7 years ago.
0

Oh yeah i finally get it. I can't access the file_path but I can return json.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

finger finger Joined 23 Jan 2017

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.