Laravel - Method link_to_action does not exist - laravel

I am using Laravel 5.8.32. I used in my blade view:
{{ HTML::link_to_action('HomeController#index') }}
But it shows : Method link_to_action does not exist.
So why does it produce the error ? How to solve that ?

The HTML helpers has been deprecated in Laravel 5.0.
In Laravel 5.8, you can generate a URL to a given controller action with:
$url = action([HomeController::class, 'index'], ['param' => 'value']);
Since action is a global helper, you can use it in a view easily:
{{ action([HomeController::class, 'index']) }}

Related

Detecting 404 error page Laravel 4.2

I'm trying to add a class to body if the page is an error page -- specifically 404.
Similar to
{{ Request::is('my-page') ? 'newclass' : '' }}
Is there any way to detect a specific error page?
My route:
App::missing(function($exception) {
return Response::view('error-404', array(), 404);
});
Pass the view name to each (or a specific) view as a variable. For Laravel 4 you can do this in the filters.php:
View::composer('*', function($view){
View::share('name_of_view', $view->getName());
});
Then you can do {{ ($name_of_view == "error-404") ? 'newclass' : '' }} in your views.

Laravel: delete photo by photo id

I'm trying to delete a photo by it's id, but the routes do not work and I receive a MethodNotAllowedHttpException. What I do:
First I create a form (in my blade template):
{{ Form::open(array("action" => array("cms/albums/destroyphoto", $photo['id']), "method" => "DELETE")) }}
<button type="submit">Delete</button>
{{ Form::close() }}
Then i create my route:
Route::post('cms/albums/destroyphoto/{id}', 'AlbumsController#destroyphoto');
And create my function in the Albumscontroller:
public function destroyphoto($id)
{
dd('Welcome photo');
}
Any suggestions where the routing goes wrong?
Thanks in advance.
Ps. I did composer dump-autoload
When you open your form using "action" you should pass the controller class and action name. You also don't need to specify the method since you're using Route::post
Like this:
{{ Form::open(array("action" => array("AlbumsController#destroyphoto", $photo['id']))) }}
More information

Posting form in Laravel 4.1 and blade template

I'm having trouble posting forms using Laravel 4.1 with the blade template engine. The problem seems to be that the full URL including http:// is being included in the form action attribute. If I hard code the form open html manually and use a relative url, it works OK, however, when it has the full url, I am getting an exception.
routes.php
Route::any("/", 'HomeController#showWelcome');
HomeController.php
public function showWelcome()
{
echo($_SERVER['REQUEST_METHOD']);
return View::make('form');
}
Form opening tag in form.blade.php
{{ Form::open(["url" => "/","method" => "post","autocomplete" => "off"]) }}
{{ Form::label("username", "Username") }}
{{ Form::text("username", Input::old("username"), ["placeholder" => "john.smith"]) }}
{{ Form::label("password", "Password") }}
{{ Form::password("password", ["placeholder" => ""]) }}
{{ Form::submit("login") }}
{{ Form::close() }}
So if I go to my home dir / in the browser, I see the form that I have created. If I fill in the form details and click submit, I am simply taken to the same page - the request method is still GET as shown by echo($_SERVER['REQUEST_METHOD']);
I notice that the full
http://localhost/subdir/public/
url is used in the form markup. If I hardcode a form open tag in such as
<form action="/subdir/public/" method="post">
it works fine and $_SERVER['REQUEST_METHOD'] shows as post.
What am I doing wrong here?
You have created the route for the post?
example:
{{Form::open(["url"=>"/", "autocomplete"=>"off"])}} //No need to later add POST method
in Route.php
Route::post('/', 'YouController#postLogin');
you have not set up a route to handle the POST. You can do that in a couple of ways.
As pointed out above:
Route::post('/', 'HomeController#processLogin');
note that if you stick with your existing Route::any that the `Route::post needs to be before it as Laravel processes them in order (I believe).
You could also handle it in the Controller method showWelcome using:
if (Input::server("REQUEST_METHOD") == "POST") {
... stuff
}
I prefer the seperate routes method. I tend to avoid Route::any and in my login pages use a Route::get and a Route::post to handle the showing and processing of the form respectively.

Laravel Pagination links not including other GET parameters

I am using Eloquent together with Laravel 4's Pagination class.
Problem: When there are some GET parameters in the URL, eg: http://site.example/users?gender=female&body=hot, the pagination links produced only contain the page parameter and nothing else.
Blade Template
{{ $users->link() }}
There's a ->append() function for this, but when we don't know how many of the GET parameters are there, how can we use append() to include the other GET parameters in the paginated links without a whole chunk of if code messing up our blade template?
I think you should use this code in Laravel version 5+.
Also this will work not only with parameter page but also with any other parameter(s):
$users->appends(request()->input())->links();
Personally, I try to avoid using Facades as much as I can. Using global helper functions is less code and much elegant.
UPDATE:
Do not use Input Facade as it is deprecated in Laravel v6+
EDIT: Connor's comment with Mehdi's answer are required to make this work. Thanks to both for their clarifications.
->appends() can accept an array as a parameter, you could pass Input::except('page'), that should do the trick.
Example:
return view('manage/users', [
'users' => $users->appends(Input::except('page'))
]);
You could use
->appends(request()->query())
Example in the Controller:
$users = User::search()->order()->with('type:id,name')
->paginate(30)
->appends(request()->query());
return view('users.index', compact('users'));
Example in the View:
{{ $users->appends(request()->query())->links() }}
Be aware of the Input::all() , it will Include the previous ?page= values again and again in each page you open !
for example if you are in ?page=1 and you open the next page, it will open ?page=1&page=2 So the last value page takes will be the page you see ! not the page you want to see
Solution : use Input::except(array('page'))
Laravel 7.x and above has added new method to paginator:
->withQueryString()
So you can use it like:
{{ $users->withQueryString()->links() }}
For laravel below 7.x use:
{{ $users->appends(request()->query())->links() }}
Not append() but appends()
So, right answer is:
{!! $records->appends(Input::except('page'))->links() !!}
LARAVEL 5
The view must contain something like:
{!! $myItems->appends(Input::except('page'))->render() !!}
Use this construction, to keep all input params but page
{!! $myItems->appends(Request::capture()->except('page'))->render() !!}
Why?
1) you strip down everything that added to request like that
$request->request->add(['variable' => 123]);
2) you don't need $request as input parameter for the function
3) you are excluding "page"
PS) and it works for Laravel 5.1
In Your controller after pagination add withQueryString() like below
$post = Post::paginate(10)->withQueryString();
Include This In Your View
Page
$users->appends(Input::except('page'))
for who one in laravel 5 or greater
in blade:
{{ $table->appends(['id' => $something ])->links() }}
you can get the passed item with
$passed_item=$request->id;
test it with
dd($passed_item);
you must get $something value
In Laravel 7.x you can use it like this:
{{ $results->withQueryString()->links() }}
Pass the page number for pagination as well. Some thing like this
$currentPg = Input::get('page') ? Input::get('page') : '1';
$boards = Cache::remember('boards' . $currentPg, 60, function() {
return WhatEverModel::paginate(15);
});
Many solution here mention using Input...
Input has been removed in Laravel 6, 7, 8
Use Request instead.
Here's the blade statement that worked in my Laravel 8 project:
{{$data->appends(Request::except('page'))->links()}}
Where $data is the PHP object containing the paginated data.
Thanks to Alexandre Danault who pointed this out in this comment.

Laravel 4 Javascript Link using {{Html}}

Is there any Laravel4 Html() function or way to add a disabled link. Of course I could create the <a> tag directly, though I'd prefer to be consistent.
ie:
{{ Html::link('javascript:;','Delete',array('id'=>"deletebt")) }}
You must use link_to, for example :
link_to('link', 'title', array('id' => 'MyId'));

Resources