I am trying to display the maximum value of an attribute in a table
my controller
$member = DB::table('member')
->select(DB::raw('MAX(code) as code'))
->where('status', '=', "No")->get();
return view('member.index', compact('member'));
Currently this is my view
{{ $member }}
And this is the output
[{"code":14101234}]
I wanted to display something like this
14101234
I've tried using json_decode but the result remains.
You receive a collection there, so you will have to do
#foreach($member as $item)
{!! $item->code !!}
#endforeach
Since $member is a array of object you are getting in view.
So you can fetch a object key by -> operator. you can fetch code like this. since you are doing ->get(), so it will return array of object.
#foreach($member as $m)
{{ $m->code }}
#endforeach
$member is a collection object.You can iterate through it to get the value in your view.
example: (in view.blade.php)
#foreach($member as $individual)
{{$individual->code}}
#endforeach
this would give you the value as you want
Related
I use encrypt. I can display all table data. However I can'T get one record. I don't encrypt 'id'. I tried another column but same.
Here is my error
Trying to get property 'id' of non-object
Here is my code
blade file (I click this link first )
{{ $val->id }}
blade file (display page I got error at this page)
#foreach ($data as $val)
{{ $val->id }}
#endforeach
Trying to get property 'id' of non-object
web.php
Route::get('/one','MailController#onerecord');
Route::post('/one','MailController#onerecord');
Controller
public function onerecord(Request $request)
{
$id = $request['id'];
$data = Contact::where('id',$id)->get();
return view('mail.one', ['data' => $data]);
}
Could you teach me what is wrong my code please?
Use dd($data) after retrieving your Contact with Contact::where('id',$id)->get(); in order to investigate the content of $data. Most likely an object is returned and not an array, so looping through $data inside your blade #foreach will loop through the object's properties. Therefore $val->id isn't valid but the direct access of $data->id would be, without the need of looping through $data.
I have a user that has many properties. This is user should also be able tp view the offers bet on his properties.
So have the relationship set.
User.php
public function properties(){
return $this->hasMany('App\Property');
}
Property.php
public function offers(){
return $this->hasMany('App\Offer');
}
Then in my controller this is what I have:
public function my_offers(){
$properties = Property::whereUserId(Auth::id())->get();
return view('pages.seller.offers.index', compact('properties'));
}
Then I go to my views like this:
#if($properties)
<ul>
#foreach($properties as $property)
<li>{{$property->offers->offer_message}}</li>
#endforeach
</ul>
#endif
When I view the page I see the below error:
Property [offer_message] does not exist on this collection instance.
But this property exists in my table.
If I change my list item to the one below I can see the array:
<li>{{$property->offers}}</li>
I also see that before and after the array with the data, there are two empty arrays as the image shows below:
Is there anything that I didn't correctly?
If not all the properties have offers, then you should check that before the <li>, besides that, offers is a collection, you need to loop through it, that's why you get the error.
#if($properties)
#php ($i = 1)
<ul>
#foreach($properties as $property)
#if ($property->offers)
#foreach ($property->offers as $offer)
<li>Offer {{ $i++ }}: {{$offer->offer_message}}</li>
#endforeach
#endif
#endforeach
</ul>
#endif
If you want to get only de properties that have offers (Querying Relationship Existence):
$properties = Property::whereUserId(Auth::id())->has('offers')->get();
And you should probably eager load that relationship:
$properties = Property::whereUserId(Auth::id())->has('offers')->with('offers')->get();
public function create() {
$user_id = auth()->user()->id;
$makers= Maker::all();
return view('maker.create', compact('makers'));
}
What I wanted to do is how to pass the categories name into the view but the form will accept its id. Please help.
$makers is a collection, so you need to use foreach to iterate:
#foreach ($makers as $maker)
{{ $maker->id }}
#endforeach
Update
If you want to pass it to Form::select, use pluck() method:
$makers = Maker::pluck('name', 'id');
It will generate array, which you can use:
{!! Form::select('selectName', $makers, ....) !!}
I've found many question realated to my problem but couldn't found an answer yet. It's about my foreach loop in my blade.
I want to print all product-names in my blade but I couln't figure out how to do that.
thats how I'm getting the products:
--- current code:
// controller
$id_array = Input::get('id');
$products= Products::whereIn('id', $id_array)->get();
$product_name = [];
foreach($products as $arr)
{
$product_name= $arr->lists('name');
}
returning $product_name gives me this as a output:
["football","cola","idontknow","freshunicorn","dummy-data"]
In my blade is just a simple:
#foreach($products as $product)
{{ $product}}
#endforeach
Error: htmlentities() expects parameter 1 to be string, array given
Thanks for your help and time.
It seems you are getting an object in an array in an array.
Like this:
array(
array(
object
)
)
It happens because you use the get() function to retrieve you model. The get() function always "wants" to retrieve multiple models. Instead you will have to use the first() function.
Like this:
foreach($id_array as $arr)
{
$want2editarray[] = Product::where('id', $arr)->first();
}
Hope it helps :)
Edit after #Wellno comment
That's probably because Product::where('id', $arr)->first(); returns null because it did not find anything.
I forgot to add a check after the retrieving of the product.
This can be done like this:
foreach($id_array as $arr)
{
// First try to get model from database
$product = Product::where('id', $arr)->first();
// If $product insert into array
if ($product) $want2editarray[] = $product;
}
Why do you use loop with IDs? You can find all products by IDs:
$products = Product::whereIn('id', $id_array)->get();
And then use $products in the blade template
#foreach($products as $product)
{{ $product->name }}
#endforeach
try to use Model/Eloquent to fetch data.
View should only display the data and not fetching directly from DB or do heavy calculations.
I pass a collection to the blade templating engine and in there I iterate over it. I want to display items position in the array. I thought of using items key, how do I extract it from the collections items?
No need to overcomplicate things.
// in your controller
return view()->with('collection', $collection);
// in your view
#foreach($collection as $index => $item)
{{ $index }}
#endforeach