Im having a little problem that i dont understand..
My Controller:
<?php
class SearchController extends BaseController{
public function postSearch(){
$course = Input::get('course_category');
if(empty($course)){
return Redirect::route('search')
->with('global','<div class="alert alert-danger" role="alert" align="center">Du måste välja kurs och universitet!</div>');
}else{
$courses = Posts::where('course_category','LIKE','%'.$course.'%')->get();
return View::make('search')
->with('course_category',$courses)
->with('courses',$courses);
}
}
}
My View:
#extends('layout.main')
#section('content')
{{Form::open(array('method' => 'POST', 'route' =>array('post-search')))}}
<div class="form-group">
{{Form::label('course_category','Språk')}}
{{Form::select('course_category',array(
'' =>'-Choose--',
'php' => 'PHP',
'javascript' =>'Javascript',
'java' =>'Java',
'C#' =>'C#',
'html' =>'HTML',
'css' =>'CSS',
'.net' =>'.NET',
'jquery' =>'jQuery',
'ajax' =>'Ajax'
))}}
</div>
{{Form::submit('Sök',array('class'=>'btn btn-info'))}}
{{Form::token()}}
{{Form::close()}}
#if($courses->count())
#foreach($courses as $c)
<p>{{$c->title}}</p>
#endforeach
#endif
#stop
This is the error that my View search is throwing:
ErrorException (E_UNKNOWN)
Undefined variable: courses (View: /Applications/MAMP/htdocs/LaraBoost/app/views/search.blade.php)
How is it possible that the $courses variable in the view is undefined when i pass it to the View from the Controller?
What did i miss?
Looks like, you have problem in your controller code, the logic doesn't seem right but to overcome the view problem you may try something like this:
#if(isset($courses) && $courses->count())
//...
#endif
Since $courses is undefined then it's not being sent. If you try this instead
#if(isset($courses) && $courses->count())
//...
#else
{{'No Courses'}}
#endif
You'll get the idea and it'll let you understand what you are doing.
Related
I created a variable in my controller so I can inject a Page Title to my pages. I have other variables setup exactly the same way and they work, but for some reason I am getting an Undefined variable: pageTitle on the new variable.
ShowUsers.php
public function render()
{
...
return view('livewire.show-users', [
'users' => $query->with('documents')->paginate($this->perPage),
'currentUser' => auth()->user(),
'pageTitle' => 'Users',
]);
}
page-header.blade.php
...
<h1 class="ml-3 text-2xl font-bold leading-7 text-gray-900 sm:leading-9 sm:truncate">
{{ $pageTitle }}
</h1>
...
You are not binding variable pageTitle into the page-header blade file. You can bind variables using Laravel Blade.
liveware/show-users.blade.php
#extends('page-header', ['pageTitle' => 'Users'])
I am trying to code the edit route for laravel and for some reason keep getting the error "Trying to get property of non-object laravel". The Create controller works fine, however when I use the controller#update route I keep getting this error
My Controller for adding an event: (update)
public function update(Request $request, $id)
{
//create event
$my_user = my::find($id);
$my_user->programme = $request->input('programme');
$my_user->goal1 = $request->input('goal1');
$my_user->goal2 = $request->input('goal2');
$my_user->goal3 = $request->input('goal3');
$my_user->goal4 = $request->input('goal4');
$my_user->goal5 = $request->input('goal5');
$my_user->user_id = auth()->user()->id;
$my_user->save();
return redirect('/home')->with('success','Event Created');
}
edit page
#extends('layouts.app')
#section('content')
<div class="container">
<h1>Edit Post</h1>
{!! Form::open(['action' => ['myUserController#update', $my_user], 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('title', 'Event Name')}}
{{Form::text('goal1', $my_user->goal1, ['class' => 'form-control', 'placeholder' => 'Goal One'])}}
</div>
{{Form::hidden('_method','PUT')}}
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
</div>
#endsection
Given that you are using a Route::resource you can type-hint your parameters by writing something like
public function update(Request $request, MyUser $myuser){
// The $myuser parameter comes from the Route::resource and can be verified using php artisan route:list in your terminal
// edit $my_user
$my_user->save();
return redirect('/home')->with('success','Event Created');
}
Update after reviewing LaravelCollective documentation for Form
Thank you Sohel0415 for mentioning that you do not need to call $my_user->id for providing the route parameter with the Form facade.
You can use this method on your code:
{{ Form::open(array('url'=>'admin/users/store' , 'method' =>'POST')) }}
and your route define by this method in web.php file:
Route::post('users/store', 'admin\UserController#store');
i am new to larvel.I tried to pass the variable from controller toview but it did not worked.
i am getting an error:
"Whoops, looks like something went wrong."
code used in controller:
public function showWelcome()
{
return View::make('hello', array('theLocation' => 'NYC'));
}
Code in hello.blade.php:
<h1 class="highlight">Blade has arrived in {{ $theLocation }} .</h1>
can you tell me is there any syntax error in the above code and is there any possibility of debugging the error??
There are many ways to pass data from Controller to View like:
return view('hello')->with(['key' => 'value']);
or
return view('hello', ['key' => 'value']);
And you can use it on view like:
<p>{{ $key }}</p>
Controller
return view('hello')->with(['theLocation' => 'NYC']);
View
<h1 class="highlight">Blade has arrived in {{ $theLocation }} .</h1>
This is the error I am getting on the sample view:
Undefined variable: sampleRecord
This is the controller code:
public function show($sample_id)
{
return View::make('samples.show')->with([
$this->sampleRepository->find($sample_id),
$this->sampleRecord->getSamplePartNumberRecord,
]);
}
This is the view code:
<p>{{ $sampleRecord }}</p>
#foreach($sampleRecord->SamplePartNumbers() as $samplePartNumberRecord)
<p>Sample Part Number: <br />{{ $samplePartNumberRecord }}</p>
#endforeach
your controller should be:
public function show($sample_id)
{
return View::make('samples.show')->with([
"sampleRepository" => $this->sampleRepository->find($sample_id),
"sampleRecord" => $this->sampleRecord->getSamplePartNumberRecord,
]);
}
I'm developing a very basic application using Laravel 4.1 where users can signup and ask question, pretty basic stuffs. I'm now a bit confused about the restful method which would look something like this public $restful = true in laravel 3. Since then laravel has changed a lot and I got stucked with the restful idea. So I decided to leave it and go on developing the skeleton of my application. Everything went well until I created the postCreate method in my homeController to let authorized users submit their question through a form. I believe I routed the method correctly and the index.blade.php view is alright as well. I just can't figure out why I'm getting this following error even though the codes seem to be okay.
Route [ask] not defined. (View: C:\wamp\www\snappy\app\views\questions\index.blade.php)
If you have got what I'm doing wrong here would appreciate if you point it out with a little explanation.
I'm totally new in laravel 4 though had a bit of experience in the previous version.
Here's what I have in the HomeController.php
<?php
class HomeController extends BaseController {
public function __construct() {
$this->beforeFilter('auth', array('only' => array('postCreate')));
}
public function getIndex() {
return View::make('questions.index')
->with('title', 'Snappy Q&A-Home');
}
public function postCreate() {
$validator = Question::validate(Input::all());
if ( $validator->passes() ) {
$user = Question::create( array (
'question' => Input::get('question'),
'user_id' => Auth::user()->id
));
return Redirect::route('home')
->with('message', 'Your question has been posted!');
}
return Redirect::route('home')
->withErrors($validator)
->withInput();
}
}
this is what I have in the routes.php file
<?php
Route::get('/', array('as'=>'home', 'uses'=>'HomeController#getindex'));
Route::get('register', array('as'=>'register', 'uses'=>'UserController#getregister'));
Route::get('login', array('as'=>'login', 'uses'=>'UserController#getlogin'));
Route::get('logout', array('as'=>'logout', 'uses'=>'UserController#getlogout'));
Route::post('register', array('before'=>'csrf', 'uses'=>'UserController#postcreate'));
Route::post('login', array('before'=>'csrf', 'uses'=>'UserController#postlogin'));
Route::post('ask', array('before'=>'csrf', 'uses'=>'HomeController#postcreate')); //This is what causing the error
And finally in the views/questions/index.blade.php
#extends('master.master')
#section('content')
<div class="ask">
<h2>Ask your question</h2>
#if( Auth::check() )
#if( $errors->has() )
<p>The following erros has occured: </p>
<ul class="form-errors">
{{ $errors->first('question', '<li>:message</li>') }}
</ul>
#endif
{{ Form::open( array('route'=>'ask', 'method'=>'post')) }}
{{ Form::token() }}
{{ Form::label('question', 'Question') }}
{{ Form::text('question', Input::old('question')) }}
{{ Form::submit('Ask', array('class'=>'btn btn-success')) }}
{{ Form::close() }}
#endif
</div>
<!-- end ask -->
#stop
Please ask if you need any other instance of codes.
Your 'ask' route is not named. When you pass 'route' => 'foo' to Form::open, that assumes you have a route named 'foo'. add 'as' => 'ask' to your /ask route and it should work.
Alternatively, use URL or Action to resolve the form's target url instead:
Form::open(['url' => 'ask']);
Form::open(['action' => 'HomeController#postCreate']);
you are using name route ask in your form which is not exist. I have created the name route ask for you.
Route::post('ask', array('before'=>'csrf', 'as' => 'ask', 'uses'=>'HomeController#postcreate'));
{{ Form::open( array('route'=>'ask', 'method'=>'post')) }}
^^^^ -> name route `ask`
{{ Form::token() }}