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
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 am having an issue by modifying the route for a view. I want instead of /company/id to show /company/id/name
the route:
Route::get('/company/{id}/{name}', 'PagesController#showCompany')->name('company.detail');
show method in controller:
public function showCompany($id){
$company = Company::find($id);
return view('company.show')->with('company', $company);
}
and in the view $companies is from a search controller - and it should get the results with a link to open the view
#foreach($companies as $company)
Show detail
#endforeach
if using only with id like /company/id works. What i am wrong?
A simple an elegant way (i think) is:
{{route('company.detail', ['id' => $company->id, 'name' => strtolower(preg_replace('/[^A-Za-z0-9-]+/', '-', $company->company_name))}}
You can have a friendly url name. I am sure that there are better solutions out there.
If you have more params in the route you can use an associative array and initialize each param name with a value.
the controller now is the same with the id.
My controller:
public function getNotificationApplication($user_id, $notification_id)
{
$userdetails = Notification::with('user')->where(['user_id'=>$user_id])->first();
$dataList = [];
$dataList['notify'] = NotificationTypeA::with('notification')->where(['notification_id'=>$notification_id])->get();
return view('Notification.notification_application', $userdetails, $dataList);
}
My route:
Route::get('/notification_application/{user_id}/{notification_id}', 'NotificationController#getNotificationApplication')->name('show.notification_application');
my view:
#foreach ($userdetails as $userdetail)
{{$userdetail->storage_location}}
#endforeach
When I am retrieving data from the database, I get a
Undefined variable: userdetails
I can retrieve other variable ($data) that I am passing as an array, I can display them like this
#foreach ($notify as $notification)
<tr>
<td>{{ $notification->item_name }} </td>
<td>{{ $notification->risk_level }}</td>
<td>{{ $notification->quantity }}</td>
<td>{{ $notification->volume }}</td>
<td></td>
</tr>
#endforeach
When I use
`dd($userdetails);`
in the controller I get the correct data.
i have no clue how to do this.
Change it:
return view('Notification.notification_application', $userdetails, $dataList);
to
return view('Notification.notification_application', array('userdetails' => $userdetails, 'dataList' => $dataList));
and try again.
Explanation: The second parameter of view() is an array in which we can pass the key => value pair. And on view, we can get the data using the key passed in the array of second parameter of view()
Reference
You have to pass an array to the blade.
For that, you can use syntax as Mayank Pandeyz said or alternatively you can usecompact() which will make code much cleaner.
return view('Notification.notification_application', compact('userdetails' ,'dataList'));
Thanks to Parth Vora and Mayank Pandeyz
i solved my problem combining both of their answers.
First of all i was passing 2 parameters one of which is an array the other is simply the first row of the table.
so i have to make a slight change in the controller
$userdetails['userdetails'] = Notification::with('user')->where(['user_id'=>$user_id])->first();
and then return
return view('Notification.notification_application', array('userdetails' => $userdetails), $dataList);
that did the trick..
thank you...
you can use the following methods
1) return view('Notification.notification_application', compact('userdetails' ,'dataList'));
2) return view('Notification.notification_application', ['userdetails' => $userdetails , 'dataList' => $dataList]);
3)
return view('Notification.notification_application')
->with('userdetails', $userdetails)
->with('dataList, $dataList);
You can pass data in return view have two way :
return view('Notification.notification_application', compact('userdetails','dataList'));
OR
return view('Notification.notification_application',
[
'userdetails'=>$userdetails,
'dataList'=>$dataList
]);
I'm using Laravel 5, MySQL.
I'm trying to get the data from my database to a table in my view.
I have done this before on the users table and listed all users from that database in a table on my html view.blade.
I can see by looking at both my user version and my new version that with in the blade tempting see below, there are spaces in the column names i.e. Cost Code need to be CostCode or Cost_Code:
<td>{{ $simstotals->Voice Numbers}}</td>
<td>{{ $simstotals->Cost Code }}</td>
<td>{{ $simstotals->Status }}</td>
To this, with out changing column names in database:
<td>{{ $simstotals->VoiceNumbers}}</td>
<td>{{ $simstotals->CostCode }}</td>
<td>{{ $simstotals->Status }}</td>
now I can't change the database column names so that nice easy solution is out the window.
So how do i get this to work, please help!!
Try SOLUTION below :
$eloquentObject->{"Column Name"}
Simple, use the syntax below:
$object->{'Date Found'}
Laravel Collection implements ArrayAccess, so it can be always referred as an array.
E.g. you can try doing the following:
$simstotals['Voice Numbers'];
Another "hacky" way is to implement custom __get and __set methods. In this case you'll be able to access these fields with something like $simstotals->Voice__Numbers (double underscore).
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
abstract class HackedModel extends Model
{
public function __get($attr)
{
if (strpos($attr, '__') !== false) {
$attr = str_replace('__', ' ', $attr);
}
return parent::__get($attr);
}
public function __set($attr, $value)
{
if (strpos($attr, '__') !== false) {
$attr = str_replace('__', ' ', $attr);
}
parent::__set($attr, $value);
}
}
Then extend this model.
Of course, this is a dirty "hack" and I don't think it's a good idea :)
Good luck!
I ran into this as well, I rewrote the SQL query that was pulling the data to the array to rename the columns (this is mssql)
$varName = DB::select(
"SELECT [index]
,[Row]
,[Supplier]
,[Plant]
,[Buyer]
,[PO Created] as POCreated
,[Ord Typ] as OrdTyp
FROM [dbo].[PullDaily]
WHERE XXXX = XXXX");
I am using laravel 4. I have a table which displays data from "cars" table. I want to click at one of car's name and display all information about this vehicle in a new page. I have done this:
index.blade.php
#foreach ($cars as $car)
<tr>
<td>
{{ HTML::link('/desc', $car->Description, array('id' => 'linkid'), true) }}</td>
{{ Form::open(array('action' => 'CarController#showDesc', $car->id)) }}
{{ Form::hidden('id', $car->id) }}
{{ Form::close() }}
<td>
{{ $car->License }}</td>
<td>{{ $car->Make }}</td>
<td>{{ $car->status }}</td>
</tr>
#endforeach
CarController.php
public function index() {
$cars = Car::all();
return View::make('pages.index', compact('cars'));
}
public function showDesc() {
$description = //here want to get name, year, color from "cars" where id = POST['id']
return View::make('pages.description', compact('description'));
}
//pages description is the new page which will display all information about that specific vehicle ,
routes.php
Route::resource('cars', 'CarController');
Route::post('desc', array('uses' => 'CarController#showDesc'));
The problem is that browser shows: This webpage not available
you are using Route::resource('cars', 'CarController'); and this will create restful routes to access your controller and in the car controller you should have functions like index(), show($id), destroy($id).
To see what are the valid routes, run php artisan routes in your project directory.
if you want to follow the restful pattern which is what you want to do I think by using a resource route. be sure you have the function show($id) in your CarController. the route to that function is cars/{$id} so in your view make a link to this route:
{{link_to_action('CarsController#show', $car->Description, $car->id)}}
I hope this will fix the problem
in your view you have a link to "/description" and in the routes.php file you don't have a route to that link