2 and I am getting some strange problem I am using model inject in blade for example when I am using this code:
<?php $b = $model1->where('id', '=', $product->user_id)->first(); ?>
{{$b->first_name}}
I am getting what I want as a result but when I do the same thing wiht this code:
<?php $b1 = $model2->select('name','number')->where('device_id', '=', $product->imception_id)->distinct()->first(); ?>
{{$b1}}
This gives me :
{"name":"jan","number":"447515485475"}
But when I am doing this :
{{$b1->name}}
I am getting Trying to get property of non-object I don't know how this will work what do I need to do please answer thanks in advance.
Related
$cntct = DB::table('products')
->join('merchants','merchants.id','=','products.merchant_id')
->where('products.id',$id)
->get();
dd($cntct[0]->address);die;
data show from this query
"{
"address":"dummy"
"state":"dummy",
"zip":"45345",
"country":"Pakistan",
"city":"dummy"
}"
I want access this address but when i write dd($cntct[0]->address->address);die;
error show
Trying to get property 'address' of non-object
Maybe, change the
->get()
to
->first()
Cuz you only want one value (you give the products id)
And then
dd($cntct->address); // no need of die
You can try foreach ($cntct as $item){ //your code }.
Maybe the query does not return any result and that's the cause of an error - Trying to get property 'address' of non-object.
Show only the variable number in the blade from the $sum value
In Blade:
{{$sum}}
In controller:
$sum = OrderPosition::select(DB::raw('sum(quantity*product_price) AS total_sales'))->where('quantity', '<>', 1)->get();
the output is {"total_sales":"550.00"}
You are trying to display the object. Try this in blade
{{$sum->total_sales}}
I am working on a quiz. Below is a method and I am trying to get an array that can work in in_array function:
public function mcq($id)
{
$questions = Chapter::find($id)->questions()->inRandomOrder()->limit(1)->first();
$question_check = TestsResultsAnswer::where('user_id', auth::id())->pluck('question_id')->toArray();
$sponsors = Sponsor::All();
return view('pages.mcq', compact('questions', 'sponsors', 'question_check'));
}
Below is blade code where I am passing the question_check variable by using in_array function:
#foreach ($questions as $key => $question)
#if(in_array($key, $question_check))
{{'Question Already Attempted'}}
#endif
#endforeach
But I am getting following error:
(2/2) ErrorException
Trying to get property of non-object (View: E:\xampp\htdocs\laravel\lea\resources\views\pages\mcq.blade.php)
My goal is to check if question was already attempted then print something. Please Help me to resolve this problem.
Using first() will return a Model and not a collection, so you can't loop through. Use get() to fetch the result as a collection.
I.e: Instead of this
$questions = Chapter::find($id)->questions()->inRandomOrder()->limit(1)->first();
do this
$questions = Chapter::find($id)->questions()->inRandomOrder()->limit(1)->get();
This should fix your current problem.
Considering this,
My Target is to make a check if question already attempted then print something.
I think this is what you really want to do.
In Controller
$questions = Chapter::find($id)->questions()->inRandomOrder()->get()
In Blade
#foreach ($questions as $question)
#if(in_array($question->id, $question_check))
{{'Question Already Attempted'}}
#endif
#endforeach
I'm trying to add an custom attribute unto the retrieved collection
$category = categories::where('cat_id',$id)->first();
foreach($category as $c):
dd(var_dump($c->cat_id))
$c->custom_attr = $c->cat_id;
endforeach;
but it throws me this error
Trying to get property of non-object
not unless if I use 'get()' instead of 'first()' then it worked. If I do
$category = categories::where('cat_id',$id)->first();
foreach($category as $c):
dd(var_dump($c))
endforeach;
it returns me
bool(true)
null
if I do
$category = categories::where('cat_id',$id)->first();
foreach($category as $c):
dd(var_dump('xx'))
endforeach;
Sure the dump returns 'xx'. Any ideas, help please?
You should use get() instead of first().
first() gives you an object, get() will give you a collection of objects.
If you want to get properties, do not use foreach() or for(), just get properties like this:
$category->name;
I've added a custom filed to the customer addresses, but I don't know how to query using this field.
I'm executing the following piece of code:
$collection = Mage::getModel('customer/address')
->getCollection()
->addAttributeToSelect('custom_field')
->addFieldToFilter('custom_field', '1');
echo var_dump($collection);
But I'm facing this error:
Fatal error: Call to a member function getBackend() on a non-object in
app\code\core\Mage\Eav\Model\Entity\Abstract.php on line 816
My question is: how can I query through this field?
Thanks.
After a few tests, I could solve the problem. Here's the code:
$collection = Mage::getResourceModel('customer/address_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('custom_field', 1)
->getItems();
echo print_r($collection, true);
Try using
Mage::getResourceModel('customer/address_collection')
Instead of
Mage::getModel('customer/address') ->getCollection()
See /app/code/core/Mage/Customer/Model/Customer.php
$collection = Mage::getResourceModel('customer/address_collection')
->addAttributeToSelect('custom_field')
->addAttributeToFilter('custom_field', '1')
->getItems();