Hello guys I'm new to laravel, so I got confuse. Somebody please clear my doubt, here is my view :
#foreach ($users as $user)
<span class="badge bg-important">{{ $user}}
</span>
#endforeach;
Here is my controller :
public function notification()
{
$users = DB::table('users')->where("Active", 0)->count();
return view('admin.layout.master',compact($users));
}
It's throwing error ;
Invalid argument supplied for foreach()
and if I remove foreach it's working fine can anybody clear my doubt why this occur
Change
compact($users)
to
compact('users')
The parameter is the name of the variable not the actual vaiable itself
First, as #meda stated, you need to fix your compact() statement.
The other issue is that your $users variable is being set to an integer, not the collection of users. Currently you are just counting the active users, not getting them.
Change
$users = DB::table('users')->where("Active", 0)->count();
To
$users = DB::table('users')->where("Active", 0)->get();
Related
I want to get some view data using laravel. here is my code:
public function brandsview($cate_url, $brand_url)
{
$brands = Brands::where('url',$brand_url)->first();
$brand_id = $brands->id;
$products = Products::where('brand_id',$brand_id)
->where('status','!=','Hide')
->where('status','Show')
->get();
return view('lokalproductspage.brands')
->with('brands', $brands)
->with('products', $products);
}
I get the error trying to get property 'id' of non-object. Please help me.
Looks like $brands return nothing, are you checked it?
If return of $brands is not stable may be u should add some checks before use attributes like 'id'
Change first() to firstOrFail(), which will let you know if you actually found a record. Normally your error suggests there was no record found. It is a nice way to show a 404 if the user put a bad url in.
$brands = Brands::where('url',$brand_url)->firstOrFail();
I'm having a problem with laravel., I'm trying to send the variable $codes on my view :
$codes = Code::where('user', $id)->get();
return view('user.edit', ['user' => $user,'codes' => $codes]);
But I get that error Too few arguments to function e(), 0 passed in
The variable $user goes well but not the variable code, anyone have an idea for a solution?
Thank you all
use this:-
return view('user.edit')->with('codes',$codes);
you can get the user with :-
Auth::user()->name;
I'm pretty sure, if you are following Laravel naming conventions, in your eloquent query the column name is user_id and not user. Also if you are using such querys, and you have foreign, you can define relations in your models, like the following:
Code Model
public function user
{
$this->belongsTo(User::class);
}
User Model
public function codes
{
$this->hasMany(Code::class);
}
Instead of line
$codes = Code::where('user', $id)->get();
You can go with:
$codes = $user->codes();
If you don't have the foreigns in your database
$codes = Code::where('user_id', $id)->get();
If non of these helps, please dd() your variables before returning the view, and share result with me in comment. In laravel 7 how you return view is good.
I'm trying to get a list of things from the Steam service on the main page of the site. But I get an error: Invalid argument supplied for foreach().
My blade:
#foreach(json_decode($giveaway->items) as $item)
<img class="giveaway-item-img" src="https://steamcommunity-a.akamaihd.net/economy/image/class/570/{{$item->classid}}">
</div>
<div class="giveaway-item-name">{{$item->name}}</div>
#endforeach
And my Controller:
#GiveAway
$kolvo=\DB::table('giveaway_items')->where('status',0)->orderBy('id', 'desc')->count();
$giveaway = Giveaway::orderBy('id', 'desc')->first();
$giveaway_users = \DB::table('giveaway_users')
->where('giveaway_id', $giveaway->id)
->join('users', 'giveaway_users.user_id', '=', 'users.id')
->get();
$game = Game::orderBy('id', 'desc')->first();
$items = PagesController::sortByChance(json_decode(json_encode($this->_getInfoOfGame($game))));
But every time i got error. What could be the problem, how to fix the error?
Please Use this code
#foreach (json_decode($giveaway->items?:"{}") as $item)
I think the value of $giveaway->items is Null
By default, json_decode will return an object. You need to provide true as the second argument to get an associative array.
#foreach(json_decode($giveaway->items, true) as $item)
I can see that you're using this in your php:
$items = PagesController::sortByChance(json_decode(json_encode($this->_getInfoOfGame($game))));
do you really need to use json_encode, then json_decode here?
is $this->_getInfoOfGame($game) return array ?
is PageController::sortByChange() returning array ?
I would suggest not to overuse json_encode, json_decode, keep them as arrays in your php codes, only json_decode Steam data (i assume you're taking data from steam);
Send the data as array to your views (way easier to debug), it's up to how you wrote your code, but i personally don't see the use of json_decode(... in the views.
Let us know how it goes.
I want to retrieve all users data except one.for that i used the following query
$users=User::whereNotIn('name',['admin'])->pluck('id','name');
When i dd() the output I see all the users data except the one But when I send the query results in foreach() loop in view page I see
Trying to get property of non-object
Error in view page.What's the Error here? Can anyone suggest me please?
Here is the foreach loop i used in view page
#foreach($users as $user)
<option value="{{$user->id}}">{{$user->name}}</option>
#endforeach
$users = User::where('id', '!=', Auth::id())->get();
this one except current login user..you can set admin id...in such field..
or you can use this also..
$users = User::all()->except(Auth::id());
both are work!!
pluck() creates an array with [id => name] structure, so change the code in assign_role.blade.php to:
#foreach ($users as $id => $name)
<option value="{{ $id }}">{{ $name }}</option>
#endforeach
And pluck() parameters to:
->pluck('name', 'id');
it may be a bit late to answer on that question but since it helped me NOT REPEATING MYSELF i'll share it with ya!
using the exception everywhere in your application will lead to a big mess in your code, so whenever you want to dig into your users you'll need to except yourself... imagine you wanted to except the user X or Y too later?
it's kind of unnecessary repetition.
User::all()->except(Auth::id())
Instead, you can just override the all method and set a global exception.
add this following to your User Model:
/**
* get all users except specified ones
*
* #param array|mixed $keys
* #return array
*/
public static function all($keys = null)
{
$data = parent::all(); # grep data from parent
return $data->except(auth()->id); # except whoever you wanted
}
and you can use your all as you used to do wherever you wanted!
User::all(); // this will exclude you
I am working on a Laravel project in which I need to write a custom function, but when I call this function Laravel says:
Laravel : htmlentities() expects parameter 1 to be string, array given
Here is my function:
public static function get_checkup_time(){
$user_logged_in = Auth::user()->foreignkey_id;
$result = DB::table('doctors')
->select('checkuptime')
->where(['id'=>$user_logged_in])
->get();
return $result;
}
And this is my view in which I am trying to invoke this function .
#if(Auth::user()->userrolebit ==2)
{{
DateTimeFormat::get_checkup_time()
}}
#endif
i don't know what I am doing wrong here.
where() expects string as first parameter, so change this:
->where(['id'=>$user_logged_in])
to this:
->where('id', $user_logged_in)
If you are trying to return just checkup time you should do this in your method
public static function get_checkup_time()
{
$user_logged_in = Auth::user()->foreignkey_id;
$result = DB::table('doctors')
->select('checkuptime')
->where('id', $user_logged_in)
->first();
return $result->checkuptime;
}
Edit:
Following a downvote, I've seen that mine wouldn't work as well because the ->get() call should be replaced with ->first() as in #Paul's answer. See my comment below.
The $result you are returning from the method is not a string.
Therefore {{ DateTimeFormat::get_checkup_time() }} is the one returning the error.
Returning something like $result->checkuptime should do it.