Laravel 5 Displaying related model data on blade - laravel

I would like to create an eager loading on my model but I keep getting and error. Anyways here is my model structure
// table name is drug_logs
class DrugLog extends Model
{
public function drug() {
return $this->belongsTo('App\Drug');
}
public function dispensedDrug() {
return $this->hasOne('App\DispensedDrug');
}
}
// table name is drugs
class Drug extends Model
{
public function drugLogs() {
return $this->hasMany('App\DrugLog');
}
}
// table name is dispensed_drugs
class DispensedDrug extends Model
{
public function drugLog() {
return $this->belongsTo('App\DrugLog');
}
}
But now on my controller I tried using this function
$drug_logs = DrugLog::with('dispensed_drugs')->get();
But I keep getting an error
BadMethodCallException in Builder.php line 2003: Call to undefined method Illuminate\Database\Query\Builder::dispensed_drugs()
Any idea on this guys?

Related

Laravel MongoDB 1:n relations

What is the correct way of defining 1:n relations in Laravel using MongoDB between two models Author and Book where one author can have several books while one book has exactly one authors?
Author.php
class Author extends Model
{
public function books()
{
// what to place here?
return $this->...
}
}
Book.php
class Book extends Model
{
public function author()
{
// what to place here?
return $this->...
}
}
Controller.php
class BookController extends Controller
{
public function store (Author $author, Request $request)
{
$book1 Book::create();
$book2 Book::create();
// connect $book1 <-> $author and $book2 <-> $author
// what to place here?
$book1->save();
$book2->save();
}
}
After calling store() I want to be able to do the following request
dump ($book1->author); // return $author
dump ($author->books); // return Collection of $book1 and $book2
assume you have the foreign key author_id in books table
class Author extends Model
{
public function books()
{
return $this->hasMany(Book::class, 'author_id');
}
}
class Book extends Model
{
public function author()
{
return $this->belongsTo(Author::class, 'author_id');
}
}
class BookController extends Controller
{
public function store (Author $author, Request $request)
{
$book1 = new Book();
$book2 = new Book();
$author->books()->saveMany([$book1, $book2]);
}
}
Suppose you have the ff:
users = model a
posts = model b
Case 1: vice versa of Case 2:
Case 2:
// on posts table/document you should have a user_id (as foreign key)
// and on post model you need define the belongsTo relationship
$this->belongsTo(User::class)
// on the User model you have to define hasOne or hasMany
$this->hasOne(Post::class) or $this->hasMany(Post::class)
Case 3:
On case 3, I'm not sure if adding a reference to each other is necessary because creating a single reference that links them can provide the data you require regardless of whether the request comes from model a or model b.
It is possible that it will not solve your problem. However, hopefully it will be of some assistance to you.
For case 1 ( in model_a ):
public function model_b_many()
{
return $this->hasMany( Model_b::class, 'id' );
}
For case 2 ( in model_b ):
public function model_a_many()
{
return $this->hasMany( Model_a::class, 'id' );
}
This will also work for case 3 :)

Custom Pivot Table name giving error no such table:main

Implementing many to many relationships with custom Pivot table name is giving error.
Service.php Model
class Service extends Model
{
public function categories()
{
return $this->belongsToMany('App\ServiceCategory', 'category_service');
}
}
ServiceCategory.php Model
class ServiceCategory extends Model
{
public function services()
{
return $this->belongsToMany('App\Service', 'category_service');
}
}
Table Names
services
service_categories
category_service (Pivot Table Name)
Test
class RelationshipTest extends TestCase
{
use RefreshDatabase;
/** #test */
public function a_service_can_belong_to_many_categories()
{
$service = factory(Service::class)->create();
$category = factory(ServiceCategory::class)->create();
$service->categories()->sync($category);
$this->assertEquals(1, $service->first()->categories()->count());
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $service->categories);
}
}
The test is giving this error.
PDOException: SQLSTATE[HY000]: General error: 1 no such table: main.categories
How to use custom pivot table name without getting this error.
I had the same error message and the issue came from my migration's foreign key constrains.
I forgot to override the default constrained() table name ("categories") to this:
Schema::create('category_service', function (Blueprint $table) {
// ...
$table->foreignId('category_id')->constrained('service_categories')->onDelete('cascade');
// ..
});

Laravel Relationship problems: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation

I must be missing something obvious. I've got a Invoice_detail Model:
class Invoice_detail extends Eloquent {
public function products()
{
$this->belongsTo('Product');
}
}
A Product Model:
class Product extends Eloquent {
public function invoiceDetails()
{
$this->hasMany('Invoice_detail');
}
}
but when I try to use this:
Route::get('/', function(){
$detail = Invoice_detail::whereId(27)->first();
return $detail->products;
});
I get: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
What am I missing here?
Yep - your relationship methods should have returns on them:
public function invoiceDetails()
{
return $this->hasMany('Invoice_detail');
}

error for inserting data in database in view in joomla

I want to insert a data in database but it show error
Fatal error: Call to a member function getform() on a non-object in
\components\com_enquiry\views\form\view.html.php
on line 9
my code is:
enqiry.php:
public function getform($data)
{
$db=JFactory::getDbo();
$db=$this->getDbo();
$query=$db->getQuery(true);
$reg=new stdClass();
$reg->name=$data['name'];
$reg->email=$data['email'];
$reg->phone=$data['phone'];
$reg->comments=$data['comments'];
$reg=$db->insertObject('#__enquiry',$reg);
}
and view.html.php:
public function display()
{
$this->msg = 'enquiry form';
$model=$this->getModel();
$data =$model->getform();
$this->assignRef('data', $data );
parent::display();
parent::display();
}
controller:
class enquiryController extends JControllerLegacy
{
public function display()
{
$vname=JRequest::getCmd('view','form');
JRequest::setVar('view',$vname);
JRequest::setVar('layout','default');
parent::display();
}
public function show()
{
$data['name']=$_POST['name'];
$data['email']=$_POST['email'];
$data['phone']=$_POST['phone'];
$data['comments']=$_POST['comments'];
$sname=JRequest::getCmd('view','thanx');
JRequest::setVar('view',$sname);
print_r($data);
}
}
could someone help me solve this error?
The problem is that your model is not loaded by the line:
$model=$this->getModel();
This either means that your model in enquiry.php is not named correctly
ComponentNameModelEnquiry
or that your view is not named enquiry.
You can try to specify which model you want, like
$model = $this->getModel('enquiry');

Joomla Component data from model to view

I am creating a component in Joomla for the first time, now i am having a hard time calling data from my model in to my view. I did this as follows:
//In my model
class InternetModelDefault extends JModel {
public function test(){
$this->test='test';
}
}
//In my view
class InternetViewInternet extends JView {
$model = $this->getModel();
$test = $model->test();
var_dump($test);
// Display the view
parent::display($tpl);
}
}
The output gives the following error:
Fatal error: Call to a member function test() on a non-object in
/var/websites/www.infrait.nl/content/components/com_internet/views/internet/view.html.php
on line 66
Where does it went wrong? Please some help..
Current mapstructure:
http://imgdump.nl/hosted/ad9e57de83060b3240f8fc6bba99237b.png
As i am new i can only share you this direct link.
your model.php like this
jimport ('joomla.application.component.modellist');
class InternetModelInternet extends JModelList
{
public function test(){
$this->test='test';
}
}
and in view.html.php get model
jimport ('joomla.application.component.view');
class InternetViewInternet extends JView
{
public function display ($tpl = null)
{
$model = &$this->getModel ();
$test = $model->test();
var_dump($test);
// Display the view
parent::display($tpl);
}
}
You are getting the error because object is not initiated properly.Change class name to InternetModelInternet. And model file name will be internet.php.
Let me know if it does not work.

Resources