I want to show users that their level is both admin and operator.
#foreach(\App\User::whereLevel('admin')->get() || \App\User::whereLevel('operator')->get() as $user)
But I get this error
Invalid argument supplied for foreach() (View: C:\xampp\htdocs\new\shopping\resources\views\Admin\levelAdmin\create.blade.php)
I don't know if you are using (\App\User::whereLevel('admin')->get() || \App\User::whereLevel('operator')->get() cause one of the queries can be empty or if you want to fetch users where level matches admin OR operator.
For the first case, try somenthing like this:
#if(!empty(\App\User::whereLevel('admin')->get()))
#foreach(\App\User::whereLevel('admin')->get() as $user)
//
#endforeach
#else
#foreach(\App\User::whereLevel('operator')->get() as $user)
//
#endforeach
#endif
If you want to get Users where level matches operator or admin, you shoul modify your query with somenthin like this:
#foreach(\App\User::where('level', 'admin')->orWhere('level', 'operator')->get() as $user)
//
#endforeach
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();
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
I joined two table.
One classrooms and another joinclass table.
Here is my classrooms table
And here is my joinclass table
Here is my controller:
public function std_classes(Request $request)
{
$data = DB::table('classrooms')
->join('joinclass', 'classrooms.class_code', '=', 'joinclass.class_code')->get();
return View::make('std_classes')->with('data',$data);
}
And here is my view
#if(Auth::check())
#foreach($data as $data)
<h1>{{$data->class_name}}</h1>
#endforeach
#endif
Now in my view here show classrooms data.
But i want to view only login user data that's why i used #if(Auth::check())
But still now its view all data.
Why #if(Auth::check()) condition not working?
I need a clarifications, do you want to show only the data of the user that is actually logged in? In that case you should not use #if(Auth::check()) because it returns true if a user is logged and then cicle all the foreach data.
In that case you shoud check the logged user after the foreach, something like that:
#foreach($data as $data)
#if(Auth::user()->id == $data->user_id)
<h1>{{$data->class_name}}</h1>
#endif
#endforeach
Seems like this should be a simple question, but I'm not getting it. I have a table called "following" with columns 'user_id' and 'following_id'. I am trying to return a list of values from the user_id column where the corresponding following_id column contains a certain value (in this case it's 1). See code below.
public function showFollowers()
{
$users = DB::table('following')->where('following_id',1)->lists('user_id');
}
Then, in my view is have
#if (!$user->showFollowers())
You have no followers.
#else
#foreach ($user->showFollowers() as $user)
<p class="username">{{ $user->getUsername() }}</p>
#endforeach
#endif
I'm not getting anything though, and no errors so the query must be correct. I must not be looping correctly in my view...
Thanks!
You are not returning the value of the function. Try:
public function showFollowers()
{
return DB::table('following')->where('following_id',1)->lists('user_id');
}