Laravel 4 unable to retrieve value from belongsTo relation using query builder - laravel-4

I have two tables, organizations and categories. This is how my tables and models are set up:
Tables:
organizations
id - integer
category_id - integer, fk
name - string
...
categories
id - integer
name - string
Models:
class Organization extends Eloquent
{
public function category()
{
return $this->belongsTo('Category');
}
public function comments()
{
return $this->morphMany('Comment', 'commentable');
}
}
class Category extends Eloquent
{
public $timestamps = false;
public function organization()
{
return $this->hasMany('Organization');
}
}
In my routes.php file, I have the following code (where $category is equal to "organizations"):
$query = Organization::query()
->join('categories', 'categories.id', '=', $category . '.id')
->select('categories.name as category');
$tiles = $query->get();
In my view, I am able to perform the following actions without any errors:
foreach($tile->comments as $comment)
{
...
}
...
$tile->name;
However, calling $tile->category->name will give me the error Trying to get property of non-object. And calling $tile->category will just return null. How can I display the category associated with the organization? I am able to retrieve other properties and relations just fine, but this is giving me a problem.

You have a bug in your code: $category . '.id' should be $category . '.category_id' doing so should make $tile->category->name work.
Also please note (you probably already know this) that in the code that you provided you are not actually using the belongsTo relation as set in your Organization class, you are just using the joined data.
The following would also work using the Eloquent ORM utilizing the models relationship methods:
$tiles = Organization::with('category')
->get();
foreach ($tiles as $tile)
{
echo $tile->name . '<br>';
echo $tile->category->name . '<br>';
}
Or you could do so the other way round from the categories model like so:
$tiles = Category::with('organization')
->get();
foreach ($tiles as $tile)
{
echo $tile->name . '<br>';
foreach($tile->organization as $org)
{
echo $org->name . '<br>';
}
}

Related

laravel multiple relation query

I have 3 model in my project and i want to get a query or collection result as i say:
JewelsItem model:
protected $table = 'jewel_items';
public function jewel(){
return $this->belongsTo('App\Models\Jewel');
}
public function sellInvoice(){
return $this->belongsTo(SellInvoice::class,'sell_invoice_id');
}
2.Jewel model:
public function jewelsItems(){
return $this->hasMany('App\Models\JewelsItem');
}
3.sellInvoice model:
protected $table = "sell_invoices";
public function jewelsItems(){
return $this->hasMany(JewelsItem::class,'buy_invoice_id');
}
Query: i want to get all jewelsitems that has no sellInvoice and has jewel name like 'something'.
note: jewel model has a name attribute .and i want to add name of jewel to first of all the collection's items of result.
i know how to get all jewel with name of 'something' :
Jewel::where('name','like','something'.'%')->get();
but i can't get all jewelItem related to it an add name of jewel to first of them.
To look for condition in another relationship you can use whereHas() and whereDoesntHave()
$name = 'something';
$items = JewelsItem::whereHas('jewel', function($query) use ($name) {
$query->where('name', 'LIKE', "%{$name}%");
})
->whereDoesntHave('sellInvoice')
->with('jewel')
->get();
It reads: getting jewel items where jewel has $name in the name field
and doesn't have sell invoice
then add related jewel to the jewel item.
When you want to retrieve the name of the jewel, you can access its property like so.
foreach($items as $item) {
echo $item->jewel->name;
}

How to show value from laravel eloquent relationship

I have tables like this
products - id, name, description, category_id, brand_id, color_id, material_id, price
extras - id, product_id, size_id, stock
images - id, product_id, name, path
size - id, name
Model product :
public function extra()
{
return $this->hasMany('App\Extra', 'product_id', 'id');
}
public function image()
{
return $this->hasMany('App\Image', 'product_id', 'id');
}
Model extra :
public function size()
{
return $this->hasOne('App\Size', 'id', 'size_id');
}
public function product()
{
return $this->belongTo('App\Product', 'product_id', 'id');
}
Model image :
public function product()
{
return $this->belongsTo('App\Product', 'product_id', 'id');
}
how to get the size name, image name & path value from the extras and images tables?
I can get the size name using this query echo $product['extra'][0]['size']['name'];, but I only get one value I don't know how to loop to get all the value.
$products = Product::with(['brand', 'category', 'color', 'image', 'material', 'extra', 'user'])->get();
foreach ($products as $product)
{
echo $product['name'] ." ". strip_tags($product['description']) ." ". $product['category']['name'] ." ". $product['brand']['name'] ." ". $product['color']['name'] ." ". $product['material']['name'] ." ". $product['price'] ." ". $product['sku'] ." ". $product['user']['name'] ."</br>";
echo $product['extra'][0]['size']['name'];
}
Any suggestion what I need to do or something that I can read to solve thiat? Thank you.
I believe below code will work for you :
// Extra relationship in Product.php
public function extra()
{
return $this->hasMany('App\extra', 'product_id', 'id');
}
// To get product extra with eager loading
$products = Product::with(['extra', 'extra.size'])->get();
foreach ($products as $product)
{
foreach ($product->extra as $extra){
echo $extra->size->name
}
}
The product contains multiple values of each relation you implement 'with' so run foreach loop or try this for a specific result
$products-brand->where("SOME CONDITION")->get();

Why can't laravel/eloquent find certain table?

I'm trying to fetch some data from a table called "category", but laravel is throwing the
"SQLSTATE[42S02]: Base table or view not found" error.
I've tried with other table names like "company" and it works perfectly. Both of these tables exist but one of them can not be found with the same code.
This throws the error:
public static function getCategories()
{
$categories = [];
$cat = DB::table('category')->get();
if (isset($cat)){
foreach ($cat as $category_name){
array_push($categories, $category_name);
}
return json_encode($categories);
}
return null;
}
This works as expected (same code except for the table name string):
public static function getCategories()
{
$categories = [];
$cat = DB::table('company')->get(); //table name changed
if (isset($cat)){
foreach ($cat as $category_name){
array_push($categories, $category_name);
}
return json_encode($categories);
}
return null;
}
The only difference between those two tables is table collation:
company: utf8_general_ci
category: utf8mb4_swedish_ci
Create a model if it you don't have one. Add the following into your category model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $table = 'category';
}
Controller
public static function getCategories()
{
$categories = [];
$cat = Category::all();
if (isset($cat)){
foreach ($cat as $category_name){
array_push($categories, $category_name);
}
return json_encode($categories);
}
return null;
}
You're currently using Query Builder, try use eloquent way.
Ok, I found out we had two separate database-servers with almost identical data in them. Both had databases and tables with the same names, but one was missing the category table.
Of course I was connected to the wrong server from laravel after all.
TL;DR: Simple user error in configuration.

Laravel 5.2 Eloquent - Model through Many-To-Many Relationship

I have 3 models: Environments, Sites and Incidents. Each have their respective tables.
My model relationships are defined as follows:
Environments have many Sites (environment_id in sites table)
public function sites()
{
return $this->hasMany('App\Site');
}
Sites belong to an Environment (environment_id in sites table) and belong to many Incidents (incident_site relationship table with incident_id and site_id)
public function environment()
{
return $this->belongsTo('App\Environment');
}
public function incidents()
{
return $this->belongsToMany('App\Incident');
}
Incidents belong to many Sites (incident_site relationship table with incident_id and site_id)
public function sites()
{
return $this->belongsToMany('App\Site');
}
Problem: I am trying to retrieve a collection of all Site Incidents through the Environment model like this:
$environment->incidents()->count();
The only way I've been able to get it to work in a controller so far is like this:
$environment->load(['sites.incidents' => function ($q) use ( &$incidents ) {
$incidents = $q->orderBy('id','desc')->get();
}]);
But it's not ideal to work with in other areas of the App.
Question: How do I go about making the above relationship work through a method in the Environment model? Is there an easier way?
There isn't any provision for using hasManyThrough() in many-to-many relation. But you can achieve this by using either DB::raw() or you can add following function to your BaseModel as given in this forum.
public function manyThroughMany($related, $through, $firstKey, $secondKey, $pivotKey)
{
$model = new $related;
$table = $model->getTable();
$throughModel = new $through;
$pivot = $throughModel->getTable();
return $model
->join($pivot, $pivot . '.' . $pivotKey, '=', $table . '.' . $secondKey)
->select($table . '.*')
->where($pivot . '.' . $firstKey, '=', $this->id);
}
Update: Use
first you would need to create a Model for incident_site
class incident_site extends Model{
public $table = 'incident_site';
//your other code
}
In your Enviorment model add the Incidents() method:
public function Incidents()
{
return $this->manyThroughMany('App\Incident', 'App\incident_site', 'site_id', 'id', 'incident_id');
}
Update:
Have modified the function according to your needs.
Change your function to following:
public function manyThroughMany($related, $through ,$middle , $firstKey, $secondKey, $pivotKey)
{
$model = new $related;
$table = $model->getTable();
$throughModel = new $through;
$pivot = $throughModel->getTable();
$middleModel = new $middle;
$middleModelIds = $middleModel->where($this->getForeignKey(),$this->getKey())->get()->lists('id')->toArray();
//$middleModelIds = $this->with($middleModel)->where()->get()->lists('id')->toArray();
//$middleModelIds = $this->sites()->get()->lists('id')->toArray();
return $model
->join($pivot, $pivot . '.' . $pivotKey, '=', $table . '.' . $secondKey)
->select($table . '.*')
->whereIn($pivot . '.' . $firstKey,$middleModelIds);// '=', $this->id);
}
Use:
Extra argument of middle table needs to passed.
public function Incidents()
{
return $this->manyThroughMany('App\Incident', 'App\incident_site','App\Site','site_id', 'id', 'incident_id');
}

Laravel Error: Trying to get property of non-object

I am getting an error
Trying to get property of non-object (View: C:\xampp\htdocs\laravel\proj\resources\views\mycases.blade.php)
I have defined a relationship between two models Ccase and Hiring.
public function hirings()
{
return $this -> hasMany('App\Hiring', 'case_ID')->orderBy('id','desc');
}
and paginating the results using a method below
public function getHiringsPaginateAttribute($perPage)
{
return $this->hirings()->paginate($perPage);
}
The other model 'Hiring' has a method to define relationship with Ccase as follows:
public function ccase()
{
return $this->belongsTo('App\Ccase', 'id');
}
In my controller, I have following code:
if(isset($search_term))
{
$search_term = preg_replace('/\s+/', ' ', $search_term);
$search_term = trim($search_term);
if (strlen($search_term) > 0 && strlen(trim($search_term)) == 0)
$search_term = NULL;
$search_terms = explode(' ',$search_term);
$fields = array('id', 'title', 'case');
$hirings = $hirings->whereHas('ccase', function($q) use ($search_terms, $fields){
foreach ($search_terms as $term)
{
foreach ($fields as $field)
{
$q->orWhere($field, 'LIKE', '%'. $term .'%');
}
}
});
}
$hirings = $hirings->getHiringsPaginateAttribute($results_per_page);
In mycases.blade.php, my code is
{{$hiring->ccase->id}}
This line is throwing the above said error while the output of {{$hiring->ccase}} is:
{"id":1,"case":"HI this is a sample case i am putting just for test.","created_at":"2015-02-22 11:54:09","updated_at":"2015-02-22 11:54:09"}
What might be wrong with the code?
Unfortunately, you can't use related models in views. Here's the detailed explanation why.
Your case can be solved by specifying the name of the associated column on the parent table:
return $this->belongsTo('App\Ccase', 'ccaseId', 'id');
In model Hiring, it will be like
public function ccase()
{
return $this->belongsTo('App\Ccase', 'case_ID', 'id');
}
And now in view use it like this:
{{ $hiring->ccase->id }}
Not sure, but i think that you could use relations in view, you should use an eager loading in controller where you are quering for $hirings, just add a:
with(['hirings.ccase'])
Could you please provide a peace of code from controller where you make a query to Hiring model for clear?

Resources