laravel 5 route passing two parameters - php

i have this route
Route::get('/artist/{id}/{name}', 'HomeController#artist')->where(['id' => '[0-9]+', 'name' => '[a-z]+'])->name('artist');
and this is my link
{{$artist->name}}
and this is the artist method on HomeController
public function artist($id, $name){ $artist = Artist::where('id', $id)->where('name', $name)->first();
return view('front.artist', compact('artist'));
}
i donot know this display error. this is the error. so please any one help me with this. iam in the middle of learning laravel.
ErrorException in UrlGenerationException.php line 17:
Missing required parameters for [Route: artist] [URI: artist/{id}/{name}]. (View: C:\xampp\htdocs\laravel\resources\views\front\home.blade.php)

Yo must pass the parameters as array, see https://laravel.com/docs/5.4/helpers#method-route
route('artist',['id' => $artist->id, 'name' => $artist->name])
or you can use
{!! link_to_route('artist', $artist->name, ['id' => $artist->id, 'name' => $artist->name]) !!}

Related

Can't access specific route in Laravel

When I return from a function with a view and a compact table:
$table = (new \Okipa\LaravelTable\Table)->model(Expense::class)->routes([
'index' => ['name' => 'admin.expenses.index'],
'edit' => ['name' => 'admin.expenses.edit'],
'destroy' => ['name' => 'admin.expenses.destroy'],
])->tbodyTemplate('bootstrap.expenses.tbody')
->query(function($query) use($user){
$query->select("expenses.*");
// $query->join('projects',"projects.id","=","expenses.project_id");
// $query->addSelect('projects.name as project');
$query->where('user_id', $user->id)->whereNull('travel_id');
});
return view('backend.expenses.index', compact('table'));
Laravel displays this error:
Return value of Okipa\LaravelTable\Table::render() must be of the type string, object returned (View: /home/armand/Desktop/GSurvey/expenses/resources/views/vendor/laravel-table/bootstrap/table.blade.php)
The index blade is where it supposed to be and in the view I have just a simple line to display the table: {{ $table }}
Can anyone extend a helping hand, maybe I am missing something?
Thank you in advance,
Kind regards,
Armand Camner

Laravel 7.2.* Language string parameter is not translating with the given value

I am having a weird problem working with language parameters in laravel. It is not being replaced with the variable but outputting the entire key.
admin.php
return [
...
'delete_title' => 'Delete :item',
...
// user section
'user' => [
'user' => 'User',
...
],
];
view.blade.php
// this won't work
{__('admin.delete_title', ['item', 'User'])}}
// this won't work
{__('admin.delete_title', [':item', 'User'])}}
// this won't work
{__('admin.delete_title', ['item', __('admin.user.user')])}}
// this won't work
{__('admin.delete_title', ['item', __('admin.user.user')])}}
I think you should to pass correct args to translate function try:
{__('admin.delete_title', ['item'=>'User'])}}
You need to pass parameters as array, so:
{{ __('admin.delete_title', ['item' => __('admin.user.user')]) }}
You have typo in
'delete_title => 'Delete :item', // missing '
fixed:
'delete_title' => 'Delete :item',

Get specified properties of a model all at once in Laravel

I want to pass some user data to a view so it can be displayed in the profile page. There is quite a lot of it but I don't want to just pass everything, because there are some things the view shouldn't have access to. So my code looks like this:
return view('profile', [
'username' => Auth::user()->username,
'email' => Auth::user()->email,
'firstname' => Auth::user()->firstname,
'country' => Auth::user()->country,
'city' => Auth::user()->city->name,
'sex' => Auth::user()->sex,
'orientation' => Auth::user()->orientation,
'age' => Auth::user()->age,
'children' => Auth::user()->children,
'drinking' => Auth::user()->drinking,
'smoking' => Auth::user()->smoking,
'living' => Auth::user()->living,
'about' => Auth::user()->about,
]);
My question is: Can this be written shorter/simpler?
Thanks!
EDIT:
I don't want this: {{ Auth::user()->firstname }} because there is a logic in a view, which is bad - I think, there should be just plain variables to be displayed, in view, not anything else.
So I'm looking for something like:
return view('profile', Auth::user()->only(['firstname', 'email', ...]));
You could create by yourself a method named like getPublicData and then return all those properties you need.
...
public function getPublicData() {
return [
'property_name' => $this->property_name
];
}
...
... and then use it in your controller/views. Maybe it's not an optimal solution, but you can isolate this thing in the model and avoid too much code in the controller.
Another advanced approach could be the override of the __get method. However, I am not the first in this case.
Hope it helps!
You don't need to pass these variable to the view, you can directly access them in the view using blade
<h1>{{ Auth::user()->username }}</h1>

Routes pagination Cakephp 3

I'm using CakePHP 3 and I want to paginate my users.
However when I click on the second page, the URL looks like /users?page=2 and I expect : /users/2.
I created this route in routes.php :
$routes->connect('/users/:page', ['controller' => 'users', 'action' => 'index'], ['page' => '[0-9]+']);
And in Users/index.ctp before the "prev" button I put :
<?php
$this->Paginator->options([
'url' => [
'controller' => 'users',
'action' => 'index'
]
]);
?>
Now when I click on page 2 for example, /users/2 opens and I got this error message (RuntimeException) :
Unable to locate an object compatible with paginate.
Did I miss something or where I made a mistake ?
Thanks for your help.
The PaginatorHelper has built in the url format, i.e. to use ?page=n. It will also do sorting such as users?page=2&sort=user_id&direction=asc. Your format of /users/{page} does not handle sorting.
If your REALLY want to stick to /users/{page} you'll have to override PaginatorHelper.
try this
in side your controller with paginator component . It works for me
$this->Paginator->paginate('Users')
for custom urlenter code here
u need to implement index action as
public function index($page = null){
$this->Paginator->settings = ['limit' => 15, 'page' => $page];
$this->set('users', $this->Paginator->paginate('Users'));
}

Mistake with my bootstrap

I am looking for why I get this error:
ErrorException [ Warning ]: Missing argument 1 for Controller_cee::action_fichestandard()
For this type of URL :
http://localhost/mycontroller/myaction/4/12
But having put this in my bootstrap:
Route::set('fichestandard', '<controller>/<action>/<secteur>/<type>', array('secteur' => '[0-9]+', 'type' => '[0-9]+'))
->defaults(array(
'controller' => 'cee',
'action' => 'fichestandard',
));
And this in action:
public function action_fichestandard($secteur, $type)
Will not someone tell me where I made ​​my mistake?
If you are using Kohana 3.2, controller arguments were removed. You need to use request parameters as described in the manual.

Categories