I'm new to php platforms and what I'm trying to do is to pass a variable from the controller to view for populating a dropdown list with entries from the database, but whatever i'm trying is not working. I get the following error:
Undefined variable: products_create
I can't understand what I'm doing wrong.
Controller
public function create()
{
$products_create = categories::all(['id', 'category']);
return View::make('products.create', compact('id', 'category'));
}
View
{!! Form::label('category', 'Categorie') !!}
{!! Form::select('category', $products_create) !!}
You pass variables to a view in the form of an associative array:
return View::make('products.create', ['products_create' => $products_create]);
compact is a function that helps you build such an array where all keys are the same as the variable name. However you have to pass the actual variable name to the function:
return View::make('products.create', compact('products_create'));
Related
Hey as i am passing a blade view which is having it own controller also i am including it into the view which does not have its own controller. it gives me an undefined variable error can any one help me how to it.
I have a view which does not have any controller only have Route like this Route::get('index', function () { return view('index'); }); in this view i am passing another view which having its own controller and also having some data from an array. but after using this view inside the view i get undefined variable error.
Two steps :
Declare & transfer $variable to View from Controller function.
public function index()
{
return view("index", [ "variable" => $variable ]);
}
Indicate where transferred $variable from Controller appear in view.blade.php.
{{ $variable }}
If you do not make sure, $variable is transferred or not
{{ isset($variable) ? $variable : '' }}
If this helps anyone, I was completely ignorant to the fact that my route was not hooked with the corresponding controller function and was returning the view directly instead, thereby causing this issue. Spent a good half hour banging my head till I realized the blunder.
Edit
Here again to highlight another blunder. Make sure you're passing your array correctly. I was doing ['key', 'value] instead of ['key' => 'value'] and getting this problem.
You can try this:
public function indexYourViews()
{
$test = "Test Views";
$secondViews = view('second',compact('test'));
return view('firstview',compact('secondViews'));
}
and after declare {{$secondViews}} in your main view file(firstview).
Hope this helps you.
public function returnTwoViews() {
$variable = 'foo bar';
$innerView = view('inner.view', ['variable' => $variable]);
return view('wrapper.view, ['innerView' => $innerView]);
}
This may be what you are looking for?
... inside your wrapper.view template:
{!! $innerView !!}
EDIT: to answer the question in the comment: In order to fetch each line you for do this inside your $innerView view:
#foreach($variable as $item)
{{ $item }}
#endforeach
... and in the wrapper view it will still be {!! $innerView !!}
How would I correctly display my partial based on the variable inside my index function? I've tried many different ways in terms of using the variable in the if statement but to no avail, namely - {{ $test }}, "test", $test. How can I make this work?
Here's the index function in my controller:
public function index(Request $request) {
$type = "abc";
return view('view', compact("type"));
}
Here's my blade file:
#if($type === "abc")
#include('myPartial')
#endif
When you sent data to view using compact() you sent an array to view.
in your code you are sending this:
$type = ['type' => "abc" ]
if you want to use it in view you should use it as an array and like this:
{{ $type['type'] }} //output : abc
I am trying to update the record in the database. This is a specific element that is an element of another. Here is my code, it doesnt work :/
web.php :
Route::patch('/projects/{projectID}/{id}', 'ProjectsController#update');
Controller:
public function update($projectId, $id, CreateProjectRequest $request)
{
$page = Page::findOrFail($id);
$page->update([
'name' => $request->name,
]);
return redirect('/projects/' . $projectId);
}
HTML:
{!! Form::model($page, ['method'=>'PATCH', 'action' => ['ProjectsController#update', $project->id, $page->id]]) !!}
{!! Form::text('name',null,['class'=>'blue-inp']) !!}
{!! Form::submit('Save changes',['class'=>'btn btn-save-blue']) !!}
{!! Form::close() !!}
You need to switch the class type-hint CreateProjectRequest for just Request in your controllers update method.
The variables passed from the form input fields can then be accessed like this:
$name = $request->input('name');
More on the topic: https://laravel.com/docs/5.0/requests
You can simplify the controller method, assuming the CreateProjectRequest handled all the necessary validation and it's inputs match the inputs you want to update
Edit: I prefer to validate everything with formrequests with rules and by the time it reaches the controller, it just calls services or does stuff.
As far as naming conventions, you may not need to send the $id in the url parameter and send it within the body, in order to avoid using Request in the formrequest to validate if it exists in the database
public function update($projectId, $id, CreateProjectRequest $request)
{
$data = $request->validated();
Page::findOrFail($id)->update($data);
return redirect('/projects/' . $projectId);
}
I am very new in Laravel. I'm trying to bind a property of the Model to selected values of the select tag. now, the following code can not show the selected tags.
{!! Form::label('tag_list','Tags') !!}
{!! Form::select('tag_list[]',$tags, null,['class'=>'form-control','multiple']) !!}
when I gave
{!! Form::label('tag_list','Tags') !!}
{!! Form::select('tag_list[]',$tags, [1,2,3],['class'=>'form-control','multiple']) !!}
it worked.
in model Article I have
public function getTagListAttribute()
{
return $this->tags->lists('id')->all();
}
this does not help. In some thread I found that for Laravel 5.2 pluck should work instead of list.
so I tried
public function getTagListAttribute()
{
return $this->tags()->pluck("id")->toArray();
}
I am using Laravel 5.2.39. What am I missing?
The third parameter of the select() method should contain the default value(s) of the select. You're passing null so there are no default values which will be selected automatically.So that it gives you error.
You can write your model as
public function getTagListAttribute()
{
return $this->tags->lists('id')->toArray();
}
I'm new to Laravel and I'm getting an error which I think has more to do with logic than anything else but I can't quite seem to grasp how to overcome it.
So, I have a page with a simple form to search for a particular string in my database. But I want to have the result show up on the same page.
Here's what I have so far:
This is my Route.php:
Route::get('/', 'HomeController#index');
Route::post('find', 'HomeController#find');
This is my HomeController:
public function index()
{
return View::make('index');
}
public function search()
{
return View::make('index');
}
public function find()
{
$match = Input::get('find');
if($match) {
$results = Book::where('title', 'like', '%'.$match.'%')
->orWhere('author', 'like', '%'.$match.'%')
->get();
return View::make('index', array('results', $results));
} else {
return Redirect::to('/')->with('flash_error', 'No string added!');
}
}
And my View (index.blade.php):
{{ Form::open(array('url' => 'find', 'method' => 'POST')) }}
{{ Form::text('find', '', array('class' => 'search-query', 'placeholder' => 'Search')) }}
{{ Form::submit('Submit', array('class' => 'btn btn-info')) }}
{{ Form::close() }}
#if (Session::has('flash_error'))
{{ Session::get('flash_error') }}
#endif
#foreach ($results as $result)
{{$result->title}}
#endforeach
(eventually the foreach will be replaced by some ajax loading to display each result)
And the error says "undefined variable: results" and shows the foreach.
I get why that error shows up since on the first pass to this page the results haven't been loaded yet but how can I overcome this? I really want the data to be shown on the same page without having to go to another page to display them.
Like I said, I think this is mostly logic related (although I'm very new to Laravel so it might be that too)
Any help would be greatly appreciated !
you need to pass an associative array as your second param of the make method
return View::make('index', array('results' => $results);
The problem here is that in your use of index.blade.php in multiple controllers, you forgot which controllers provide which variables (and as a result, which variables may be omitted).
When you request / (HomeController#index), index.blade.php is rendered, but since no $results are passed to the view, you see the Undefined Variable warning. This is not a problem in HomeController#find, because you define $results. To combat this, you'll need to do something along the lines of an isset() check on $results before you foreach over it. Like so:
#if(isset($results))
#foreach ($results as $result)
{{$result->title}}
#endforeach
#endif
Your logic may vary based on your page's layout (you might want to add an else and display some alternate placeholder content).
Also, if abstracting the call to View::make() with $results into index_gen() isn't keeping your code DRY, then I'd suggest replacing it in find() with the call to View::make().