in my laravel5 controller I take some rows from DB:
$order = Order::select('ordered_by')->where('column1','=','1')->get();
then in my view, where I render a table,
#foreach($users as $user)
#if(strpos($order, $user->id) == true) match! #endif
#endforeach
I get result
[{"ordered_by":"309"},{"ordered_by":"524"},{"ordered_by":"541"},{"ordered_by":"545"}]
Why strpos not working? (Actually it gives me "match!" in table only for user #546)
Tried this:
#if (in_array($user->id,$order) ==true ) ok #endif
but it shows me error in_array() expects parameter 2 to be array, object given
How can I check if $user->id exists in $order output?
Try this code:
$order = Order::where('column1','=','1')->get();
$orders = array_pluck(collect($order)->toArray(), 'ordered_by');
#foreach($users as $user)
#if(in_array($user->id, $orders))
ok
#endif
#endforeach
strpos — Find the position of the first occurrence of a substring in a string
You can got it using in_array
Try given script
$order = Order::select('ordered_by')->where('column1','=','1')->get()->toArray();
#foreach($users as $user)
#if (in_array($user->id,$order)) ok #endif
#endforeach
Related
I'm trying to access a Controller method from inside my view, but I'm getting this error:
htmlspecialchars() expects parameter 1 to be string, object given (View: D:\Programming\Php\Laravel\Laravel-Phone_Book\resources\views\contact_list.blade.php)
Here's my part of my view that's throwing the error:
<tbody id="tableBody">
#foreach ($data as $item)
<tr>
<!-- Test -->
<td>{{ $item->nome }}</td>
<td>{{ $item->numero_count }}</td>
<td>
<ul style="list-style: none;">
{{ $phones = app\Http\Controllers\ContactListController::get_telefones_by_usuario($item->u_id) }}
#foreach ($phones as $phone)
<li>{{ $phone }}</li>
#endforeach
</ul>
</td>
</tr>
#endforeach
</tbody>
Here's my controller function:
public function get_telefones_by_usuario($id)
{
$telefones = Telefone::join("usuarios", "usuarios.id", "=", "telefones.id_usuario")
->select("telefones.numero as numero")
->where("usuarios.id", "=", $id);
return $telefones;
}
Here's my Controller's function that injects data into my index view (that includes the view I'm trying to access the data from):
public function index()
{
// $usuarios = Usuario::all();
// $telefones = Telefone::all();
$data = Usuario::join("telefones", "usuarios.id", "=", "telefones.id_usuario")
->select(
"usuarios.id as u_id",
"usuarios.nome as nome",
"telefones.numero as numero",
DB::raw("COUNT(telefones.numero) AS numero_count")
)
->groupBy("usuarios.nome")
->orderBy("usuarios.nome")
->get();
return view("index", compact("data"));
}
What am I doing wrong? u_id is supposed to be an integer, not an array or anything. Why is htmlspecialchars() not parsing it?
Thank you.
Edit:
Tried placing the following at the top of my partial view:
#inject('ContactListController', app\Http\Controllers\ContactListController")
Then replacing the part where I call my Controller method above with the following:
<?php $phones = $ContactListController::get_telefones_by_usuario($item->u_id) ?>
Now the error went away but I'm not getting anything back from my query, which I should:
On the Phones column there should be a list of phones associated with each person.
What's going on?
I think you should edit the below line.
$telefones = Telefone::join("usuarios", "usuarios.id", "=", "telefones.id_usuario")
->select("telefones.numero as numero")
->where("usuarios.id", "=", $id)->get();
also when printing try to print the value using the right index.
<li>{{ $phone->numero }}</li>
I'm trying to get all the users where a given method in User model meets. Please see my code below:
User.php
public function isPicker(){
return ($this->where('isPicker', 1)) ? true : false;
}
Now, I can use User::all();, but it returns all the users. What I want is to only return the users that meets the isPicker() method. What I'm trying to do in view is:
#foreach($users as $user)
#if($user->isPicker())
{{ $user->first_name }}
#endif
#endforeach
This is also working fine, but it is not that efficient to use. What if there's a lot of method to check? Any idea for this?
Just do:
$users = User::where('isPicker', 1)->get();
Or create a scope:
public function scopeIsPicker($query)
{
return $query->where('isPicker', 1);
}
// usage
$users = User::isPicker()->get();
Well you could change you code up a little to look like this.
#foreach($users->where('isPicker', 1)->all() as $user)
{{ $user->first_name }}
#endforeach
But this will only work if the users var is a collection.
Other wise just change you query on how your getting the users to something like this.
User::where('isPicker', 1)->get()
Instead of checking in model file, you can directly query in your controller try below code
if you want to display only isPicker=1 users.
Controller Code :
$users = User::where('isPicker', 1)->get();
Blade code :
#foreach($users as $user)
{{ $user->first_name }}
#endforeach
OR
if you want to display all users including isPicker 0&1.
Controller Code :
$users = User::all();
Blade code :
#foreach($users as $user)
#if($user->isPicker == 1)
{{ $user->first_name }}
#else
<p>Picker is 0</p>
#endif
#endforeach
Note : Remove your isPicker function from user model file because its unuse.
I use Laravel with foreach in a blade.php
{{ $users = App\User::where('user_id', $post->user_id)->get() }}
and I got the result of this which show on the page
[{"user_id":"123","email":"123#com","password":"1234","first_name":"Tony"}]
But I want to get "Tony" only, how can I call?
{{ ($users = App\User::where('user_id', $post->user_id)->first())->first_name }}
Or better in sense of performance, if you select posts with user:
{{ $post->user->first_name }}
But it is better to prepare necessary data in controller.
get() returns an array, you should use first()
{{ ($users = App\User::where('user_id', $post->user_id)->first())->first_name }}
Since you said you are using foreach in blade. Try something like this
#foreach($user as $u)
{{$u->first_name}}
#endforeach
This will give you first_name for all users
I need to find out unique value. So I tried bellow code. It is through undefined variable error.
Controller:
$employee = Employee::all();
Return view ('page', compact('employee'));
Page view:
$uniqueEmpLoc = $employee->unique('location')->values()->list('location')->toArray();
#Foreach($uniqueEmpLoc as $empLoc)
{{ $empLoc }}
//this is select box used for search
#endforeach
//Display Entire data
#foreach($employee as #employee)
//Display all value
#endforeach
But I got an uniqueEmpLoc is undefined error. I'm using LARAVEL 5.1. Please help me to solve this problem.
There are some errors in your code:
I don't think compact(employee) would work. Shouldn't that suppose to be compact('employee') ?
In Blade, there is no need of putting curly braces at all. Remove them.
Try out the following:
$employees = Employee::unique('locations')->values()->list('location')->toArray();
return view('page', compact('employees'));
And then in your view:
#foreach($employees as $employee)
{{ $employee }}
#endforeach
Use this query in controller
$employees = Employee::distinct()->list('location')->toArray();
return view('page', compact('employees'));
In view
#foreach($employees as $employee)
{{ $employee }}
#endforeach
I agree with the other answers here, this code should be in the controller. You shouldn't be doing logic in the views.
In the controller do:
$uniqueEmpLoc = $employee->unique('location')->values()->list('location')->toArray();
$employee = Employee::all();
Return view ('page', compact('employee', 'uniqueEmpLoc'));
The reason your code isn't working is the line that defines $uniqueEmpLoc is interpreted by blade as text, not code.
If you really want to do that in the view, you need to wrap it in #php tags.
#php
$uniqueEmpLoc = $employee->unique('location')->values()->list('location')->toArray();
#endphp
#Foreach($uniqueEmpLoc as $empLoc)
{{ $empLoc }}
//this is select box used for search
#endforeach
I'm trying to get the total comments the user have..
Controller:
public function index()
{
$setting = Setting::findOrFail(1);
$comments = Comment::where('published', '=', '1')->get();
$users = User::all();
return view('page.index', compact('setting', 'comments', 'users'));
}
View:
#foreach($comments as $comment)
{{ count($users->where('user_id', '=', $comment->user_id)) }}
#endforeach
The problem is that it only returns 0 and i have 2 comments there.. even using the user id to instead of "$comment->user_id" it doesnt work. still display 0.
$users is a collection, not a query. Treat it as such:
#foreach ($comments as $comment)
{{ $users->where('user_id', $comment->user_id)->count() }}
#endforeach
The collection's where method does not take an operator.
From the wording in your question it seems you actually want it the other way around:
$comments = Comment::wherePublished(1)->get()->groupBy('user_id');
Then in your view:
#foreach ($users as $user)
{{ $comments->has($user->id) ? count($comments[$user->id]) : 0 }}
#endforeach
I'm late to answer your question and Joseph already showed you the problem but you may also do it differently. You can group comments using Collection::groupBy, for example:
$comments = Comment::all()->groupBy('user_id');
Then in your view you may try this:
#foreach($comments as $user => $userComments)
// count($userComments)
// Or
#foreach($userComments as $comment)
// {{ $comment->title }}
#endforeach
#endforeach
In the first loop (#foreach($comments as $user => $userComments)), the $user is the user_id and it's value would be all comments (an array) by this user but in group.