Problems linking view and controller - php

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,
]);
}

Related

why is always this error Undefined variable $question

I try to give the data from database to the blade.php.
The controller:
class DataController extends Controller
{
// sql query from datebase
public function question_number()
{
$questions = DB::table('questions')->sum('id');
return view('/statistics',['question' => $questions ]);
}
public function group_number()
{
$groups = DB::table('groups')->sum('id');
return view('/statistics', ['group' => $groups ]);
}
}
in there I have define the value $question and $group
and in Blade.php I have try to use this value:
<div>Die Anzahl von Fragen</div>
<p> {{ $question }} </p>
<div>Die Anzahl von Gruppen</div>
<p> {{ $group }} </p>
in route
Route::get('/statistics', 'DataController#question_number');
Route::get('/statistics', 'DataController#group_number');
but the error is always
Undefined variable $question
What did I miss? And what should I do?
You want to send 2 variables to one view, I think. To do this you don't need 2 routes and 2 controllers. In fact you CAN'T do that.
Route File:
Route::get('/statistics', 'DataController#question_number');
Controller:
class DataController extends Controller
{
// sql query from datebase
public function question_number()
{
$questions = DB::table('questions')->sum('id');
$groups = DB::table('groups')->sum('id');
return view('/statistics',['question' => $questions , 'group' => $groups]);
}
}
View (without change):
<div>Die Anzahl von Fragen</div>
<p> {{ $question }} </p>
<div>Die Anzahl von Gruppen</div>
<p> {{ $group }} </p>

Pass data to view in Laravel

I am new to Laravel I just want to pass variable to view but every time I have got Undefined variable: companies error
this is my Controller:
public function index()
{
$companies = Company::all();
return view(
'companies.index', [
'companies' => $companies
]);
}
and this is my view:
<ul class="list-group">
#foreach($companies as $company)
<li class="list-group-item">{{ $company->name }}</li>
#endforeach
</ul>
I use Laravel version 5.5
Make sure in your route file on routes/web.php you have a route to the index function like so:
Route::get('urlToYourView', 'YourController#index');

withX() does not work in Laravel 4.2

The problem is that withErrors() is working perfectly in this code, but withMessage() isn't.
I also tried with('message', 'Test message!'). In views file, I can retrieve withErrors using $errors variable, but if I want to retrieve withMessage, I have to use Session::get('message'). Why is $message not working?
Controller:
public function registration() {
$rules = array(...);
$validator = Validator::make(Input::all(), $rules);
if($validator->fails()) {
return Redirect::route('registration')->withErrors($validator);
}
else {
//Some code here...
return Redirect::route('registration')->withMessage('Test message!');
}
}
Template:
#extends('default.base')
#section('main')
#if(!empty($errors->all()))
<div class="alert alert-danger" role="alert">
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}
#endforeach
</ul>
</div>
#endif
#if(isset($message))
{{ $message }}
#endif
#stop
That is because errors is a special case. When creating a view, Laravel check's if there is a session variable with the name errors. If so it will then pass the contents as $errors to the view.
Illuminate\View\ViewServiceProvider#registerSessionBinder
if ($me->sessionHasErrors($app))
{
$errors = $app['session.store']->get('errors');
$app['view']->share('errors', $errors);
}
This means you either use Session::has('message') and Session::get('message') in your view or you add a View Composer that does basically the same that Laravel does with errors:
View::composer('*', function($view){
if(Session::has('message')){
$view->with('message', Session::get('message'));
}
});

Undefined variable in View?

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.

"Use of undefined constant..." error in Laravel

I am a Laravel newbie. I want to pass the results of a database query to a view. I get an error message "Use of undefined constant tasks - assumed 'tasks'". What am I doing wrong?
My code is as follows:
class TasksController extends BaseController{
public function index(){
$tasks = Task::all();
//return View::make(tasks.index, ['tasks' => $tasks]);
return View::make(tasks.index, compact('tasks'));
}
A snippet from my template page is shown below:
<body>
<h1>All tasks!</h1>
#foreach($tasks as $task)
<li>{{ $task-title }} </li>
#endforeach
return View::make('tasks.index')->with(compact('tasks'));
also change:
<li>{{ $task-title }} </li>
to
<li>{{ $task->title }} </li>
should be like this.
Try this,
return View::make(tasks.index, $tasks);
instead of
return View::make(tasks.index, compact('tasks'));

Categories