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>
Related
I want to ask if ever I can limit my data in table. I want to remove one data out of my 6 data.
here is my table
Now I want to remove number 3 row which is SESSION_VALIDITY how can I remove that data and make it just 5 data in my table? I want to limit my data coz' I dont need to view SESSION_VALIDITY. help thanks
my Index
#foreach ($settings as $setting)
<tr>
<td>{{ $setting->settings_code }}</td>
<td>{{ $setting->subject}}</td>
<td>{{ $setting->description }}</td>
<td></td>
</tr>
#endforeach
my Controller
public function index()
{
$settings = Setting::all();
return view('admin.settings.index', compact('settings'));
}
If you just want to not show that row in your $settings data, you can exclude it using your query like this.
public function index()
{
$settings = Setting::where('settings_code', '<>', 'SESSION_VALIDITY')->get();
return view('admin.settings.index', compact('settings'));
}
If you want to delete it entirely, you can run a one-time function to delete that row from the table.
public function deleteSessionValidity()
{
$setting = Setting::where('settings_code', 'SESSION_VALIDITY')->first();
$setting->delete();
}
You should filter your query in the controller.
Instead of Setting::all(), you should use:
$settings = Setting::where('settings_code', '!=', 'SESSION_VALIDITY')->get();
You should try this:
public function index()
{
$settings = Setting::where('settings_code', '!=', 'SESSION_VALIDITY')->get();
return view('admin.settings.index', compact('settings'));
}
OR
#foreach ($settings as $setting)
<tr>
#if($setting->settings_code != "SESSION_VALIDITY")
<td>{{ $setting->settings_code }}</td>
<td>{{ $setting->subject}}</td>
<td>{{ $setting->description }}</td>
<td></td>
#endif
</tr>
#endforeach
inside foreach put if statement
like:
#foreach ($settings as $setting)
#if ($setting->settings_code != "SESSION_VALIDITY")
your <td> here
#endif
#endforeach
Instead of creating a foreach loop I want to iterate over the results and
I am using the each() method:
$collection = Comment::all();
$comment = $collection->each(function ($comment){
dd($comment->comment);
});
when I dd() I get:
"Hatter; 'so I should think."
But when I pass to view:
return view('welcome')->with('comment',$comment);
I get
[{"id":1,"post_id":5,"comment":"Hatter; 'so I should think.","created_at":"2018-04-05 15:23:20","updated_at":"2018-04-05 15:23:20"},
{"id":2,"post_id":5,"comment":"Alice gently remarked;.","created_at":"2018-04-05 15:23:20","updated_at":"2018-04-05 15:23:20"},
and so on..
This is the view:
{{$comment}}
I want to iterate through the collection and put data into $comment and then show it in the view.
It is quite simple like this.
Iterate then -> following the property name
#foreach($comment as $row)
<li>{{ $row->id }}</li>
<li>{{ $row->post_id}}</li>
//AND SO ON
#endforeach
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.
In the following code I used {!! URL::route('editCatForm',['id'=>$row->id]) !!} to go to named route editCatForm with query string ?id=5 or whatever that comes dynamically on $row->id
#foreach($categories as $row)
<tr>
<td>{{ $count++ }}</td>
<td>{{ $row->category_name }}</td>
<td>{{ $row->category_status }}</td>
<td>edit / delete</td>
</tr>
#endforeach
My route for this is
Route::get('editCatForm/{id?}',array('uses'=>'Categories#editCat','as'=>'editCatForm'));
but still it shows url like
http://localhost/projects/brainlaratest/editCatForm/2
instead of
http://localhost/projects/brainlaratest/editCatForm?id=2
The route points to function
public function editCat($id)
{
$catEdit = Category::find(Input::get('id'));
$categories = $this->getCat();
return view('categoriesAddForm',compact('categories','catEdit'));
}
What may be the issue that query string isn't working here?
Format of your url is editCatForm/{id?} so if you provided id it will try to replace {id} with your number and you will get editCatForm/5.
Problem is in your controller action. function editCat($id) already takes $id from route - you should replace Input::get('id') with just $id.
URL::route(...) can be replaced by just helper function route(...).
If you want get rid of /id you can remove {id} from your route and then route(...) will just add ?id=5 instead of /5. You would have to remove $id argument from function and get id by Request::input('id');.
This is how route() function is supposed to work.
If you insist on having the query string then you need to append the ?=2 to the URL manually and you cannot do routing based on this.
One way of building the query string is like this
$params = array('id' => 5);
$queryString = http_build_query($params);
URL::to('projects/brainlaratest/editCatForm?'.$queryString )
The Routing is correct. Your problem is to getting the $id in the action.
Since, you have passed $id in route parameter, you can capture the segment $id inside your action.
public function editCat($id)
{
$catEdit = Category::find($id); // edit this line.
$categories = $this->getCat();
return view('categoriesAddForm',compact('categories','catEdit'));
}
source: http://laravel.com/docs/5.0/routing#route-parameters