Laravel 7 variable not passed to view - php

I have a strange error:
i pass a simple var from controller to view like this:
namespace App\Http\Controllers\BE;
class User extends Controller
{
// some stuff here
public function sendPasswordReminder(Request $request) {
return response()
->view('BE.user.passwordreset', ['name' => 'James'], 200);
// very simple, nothing special here, also tried return view() stuff
}
}
and an absolutly basic blade:
#extends('BE.templates.main')
#section('content')
{{ $name }}
#endsection
and i get the response
Facade\Ignition\Exceptions\ViewException
Undefined variable: name (View: /Users/modii/Work/l7/distr/resources/views/BE/user/passwordreset.blade.php)
i ended up with this absolute basic version while trying to figure out what is wrong. this should be very basic. its not working. tried to clear cache both with artisan and manually. no result.
i have NO IDEA what could be wrong. If anyone has an idea...

Related

How to call controller function inside blade php?

Im trying to figure out how to call function from controller.
When I call this in blade php its working fine
Todo::where('is_done', false)->count();
But Im trying to make less mess in blade php and call it like
{{ Todo::isDone(); }}
What im trying in controller is
public static function isDone()
{
return Todo::where('is_done', false)->count();
}
Getting error
Call to undefined method App\Models\Todo::isDone()
You could put it on the model or just use the full namespace. Try
{{ App\Http\Controllers\Todo::isDone() }}

Laravel: When I pass a model to a controller it is always null. Why?

I'm trying to change my ways in Laravel, but I find it quite frustrating
Normally, in a controller I'd write something like this
public function edit($id) {
$question = Question::findOrFail($id);
return view('question.edit', compact('question');
}
This obviously works. In the HTML the route that calls this is {{ route('question.edit', $question->id) }}. Now I want to use the method it is written by Artisan when you create the controller. If I do:
public function edit(Question $question) {
return view('question.edit', compact('question');
}
This doesn't work (of course I'm changing the blade directive to {{ route('question.edit', $question) }}), this always passes an empty Question model, it doesn't have id or any of the other fields that were accessible in the blade file. If I do a dd() in the blade file, it'll show the correct model, when passed to the Controller is empty.
What am I doing wrong?
You need to match your type hinted variable name to the name of the route parameter if you want Implicit Model Binding to work, otherwise you are just asking for a dependency and it will inject a new instance of that model:
// vvvvvvvv
Route::get('question/{question}/edit', 'YourController#edit');
// vvvvvvvv
public function edit(Question $question)

Laravel Data passed to View become a request

I have this annoying error in laravel where my HomeController passing a data to view changes it to an route request.
As you can see here, I am simply passing a string from my HomeController#index ...
public function index()
{
$user = Auth::user();
return view('home', ['char' => 'test']);
}
then there would be a GET request at the console equivalent to the data.
As you would have guessed, anything I pass here would trigger a GET request. Let's try passing an empty stdClass Object.
public function index()
{
$user = Auth::user();
return view('home', ['char' => new \stdClass()]);
}
and the same thing happens.
I am passing BTW to a vue component and but displaying it directly in .blade.php doesn't cause this error.
#extends('layouts.app')
#section('content')
<character-page :user="{{ json_encode($char) }}"/>
#endsection
You might say that it is probably the vue component making the GET request but no, I totally have no http request in my vue compo, it is just an innocent template-style-script component.

Laravel 4 - How to set input value as a route

I want to implement a text input field via Blade to my view in Laravel 4.
{{ Form::open(array("route" => "search.show")) }}
{{ Form::text("name") }}
{{ Form::close() }}
The text input posts a string to my controller, that is used to select some data from my database via
class SearchController extends \BaseController {
public function show($name) {
return(Item::where("name", "like", "%" . $name . "%")->get());
}
}
When I submit the form with the text input, I want to hit the search.show Route, that I implemented with
Route::resource("search", "SearchController");
Now I want to know, how to post the text to my routes.php, that my controller is able to get the input by the parameter of the show method?
I know I could enter the data via Input::get("name"), but isn't it a design failure, when Router::resource gives me the template for this?
Sorry for my bad english, but I hope you guys could help me out.
Thank you.
You're specific need isn't answered by the Resource controller layout. A search in general will have a number of different facets beyond a keyword, although this isn't always true.
The best approach I can offer you would be to avoid using any of the pre-existing Resource Controller routes as you may need them for what they are actually for in the future.
You can add a search method to your Resource controller when you declare the routes.
Route::get('search', [
'uses' => 'SearchController#search',
'as' => 'search'
]);
Route::resource('SearchController');
However, as a resource controller is not a good solution for this. Use a regular controller. Just remove the resource controller call in routes.php and only implement the search method.
public function search()
{
// Your code here
}
The advantages to this are that you take back full control over the naming of the route, the HTTP verb used (GET makes more sense for a search) and you don't pollute a resource controllers templated routes with functionality that doesn't belong.
The second part is how to pass variables. Searching is typically difficult using parameters passed in as separated arguments in the url. Using a GET request just use a query string.
http://myapp.com/search?keyword=search%20%keyword
Now just use the Input facade or dependency inject the Request class into the controller.
$keyword = Input::get('keyword');
if you using form better not using function which hash variable like show($id) function, because url should be like this .../search/show/{$id}, try use store() function.
public function store()
{
$name = Input::get("name");
/* your code */
}
==
{{ Form::open(array('route' => "search.store")) }}
{{ Form::text("name") }}
{{ Form::close() }}
You can inject a Request object to show method of the controller.
class SearchController extends \BaseController {
public function show(Request $request) {
$name = $request->input("name");
}
}
Or you can do form model binding : http://scotch.io/quick-tips/php-tips/laravel-php/laravel-form-model-binding.
On Laravel 4.x you have to use the Input Facade
class SearchController extends \BaseController {
public function show(Request $request) {
$name = Input::get("name");
}
}

laravel 4: always the fourth controller method doesn't work->blank page

All the first three methods of all my controller work well, but when i add a fourth one this one doesn't work (the construct method not included) and give me a blank page with the url of the controller action.
my controller class:
class StoreController extends BaseController{
public function __construct(){
parent::__construct();
$this->beforeFilter('csrf', ['on'=>'post']);
}
public function getIndex(){
return View::make('store.index', ['products'=>Product::take(4)->orderBy('created_at','DESC')->get()]);
}
public function getView($id){
return View::make('store.view', ['product'=>Product::find($id)]);
}
public function getCategory($cat_id){
return View::make('store.category', [
'products'=>Product::where('category_id','=',$cat_id)->paginate(6),
'category'=>Category::find($cat_id)]);
}
public function getSearch(){
$keyword=Input::get('keyword');
return View::make('store.search', [
'products'=>Product::where('title','LIKE','%'.$keyword.'%')->get(),
'keyword'=>$keyword]);
}
}
In my route.php file:
Route::controller('store', 'Storecontroller');
And the triggerer form of the action is:
<div id="search-form">
{{ Form::open(['url'=>'store/search', 'method'=>'get']) }}
{{ Form::text('keyword', null, ['placeholder'=>'Search by keyword', 'class'=>'search']) }}
{{ Form::submit('Search', ['class'=>'search submit']) }}
{{ Form::close() }}
as I said the getSearch method doesn't work and I'm given a blank page with the url of the action (not the returned view)
thanks
As the page is blank, make sure that debug is set to true in your app config (should be the topmost setting, preferably in the local dir). This means that when an error occurs you will be shown an error page with a detailed stack trace and error message, making it easier for you to debug your app.
Make sure that the view store.search exists, is named correctly (check for typos) and contains the html/php you need to display the $products.
Next, you have two possibilities:
Set a default value for the keyword input
// For example:
Input::get('keyword', 'default')
Check if there is a keyword specified
// For example:
if (Input::has('keyword')) {...} else {...}
As a side note: You should not perform (heavy) tasks inside of arrays like you do here. Put them inside variables and include them into the view array like you did with the keyword variable. Your code will also be more readable and maintainable. There's a lot more about this, but thats of the point here.
Let me know if this helped you.

Categories