I have the following route in my laravel project
Route::get('/',[
'uses' => 'MakeController#index'
]);
Controller
class MakeController extends Controller{
public function index(){
$makes = MakeType::all();
return View::make('Index', $makes);
// tried this
// return view('Index',compact('makes'));
}
}
index.blade.php
<select>
<option>Select Make</option>
#foreach($makes as $make)
<option>{{$make->name}}</option>
#endforeach
</select>
Problem:
The problem is when I try to load the index page, it shows me the following error
Use of undefined constant makes - assumed 'makes' (this will throw an Error in a future version of PHP) (View: /customers/6/1/4/alle-voertuigen.nl/httpd.www/resources/views/Index.blade.php)
I've visit all the possible links, and tried different ways of doing it, but nothing is working with me.
This is what it is showing when I do dd($makes), in attributes I have the name column
Please help me, thanks
Use with in your controller.
return View::make('Index')->with(compact('makes'));
Should work.
If you use this way to return a view, you must use with to add parameters :
return View::make('Index')->with('makes', $makes);
I will advise to use the view helper, I don't know what version of Laravel you use :
return view('Index', ['makes' => $makes]);
Just only thing sure, it can't work if you don't pass variables as an array (with compact or ['myVariable' => $var])
class MakeController extends Controller {
public function index() {
$makes = MakeType::all();
return View::make('index', $makes);
// tried this
// return view('index',compact('makes'));
}
}
its index not Index
Related
I'm trying to follow a small laravel tutorial.
In my application I have a controller called, HomeController.
In my HomeController, I have the followinf index function
public function index()
{
try {
//get autheticated users selected organisation
$organisation = $this->getSelectedOrganisationByUser(Auth::user());
//set application timezone
$this->setApplicationTimezone($organisation);
$this->setSiteConnection($organisation);
$this->setGAConnection($organisation);
if ($organisation->disabled) {
Auth::logout();
return redirect(route('login'));
}
//sales today
$sum='23';
return view('dashboard.index')
->with('organisation',$organisation,'sum',$sum);
} catch (\Throwable $exception) {
\Sentry\captureException($exception);
}
}
Here I'm trying to send this $sum value to my blade and display it. But, Every time i tried to display this value on my blade I get $sum is undefined error.
I tried to echo this on my blade as follows,
{{ $sum }}
What changes should I make, in order to display that on my blade.
I tried hard, but yet to figure out a solution
You need to pass the variables as array. Try this:
return view('dashboard.index', ['organisation' => $organisation, 'sum' => $sum]);
or another way is by using compact() like this
return view('dashboard.index', compact('organisation', 'sum'));
The method with() from /Illuminate/View/View.php accepts only two parameters.
To send multiple variable, you can
//Call with() for each variable
return view('dashboard.index')
->with('organisation',$organisation)
->with('sum',$sum);
//use an array as second parameter of view
return view('dashboard.index', ['organisation' => $organisation,'sum' => $sum]);
//use compact to create the array using the variable name
return view('dashboard.index', compact('organisation', 'sum'));
//use an array as first parameter in with (using compact or not)
return view('dashboard.index')->with(compact('organisation', 'sum'));
hello guys im having a problem with passing variable from my controller to views, as it does not identify its variable, here is my code:
RegisterController.php
use App\angkatan;
public function index()
{
$select = Angkatan::all();
return view('/auth/register')->with('name', $select);
}
My Route
web.php
Route::get('register', 'RegisterController#index');
and my view
register
#foreach($name as $ps)
#endforeach
the error say
Undefined variable: name (0)
im very thankful if anyone can help
You are just passing the wrong way $select variable to your view.
when you use Illuminate\View\View::with method you should pass an associative array which as key => value pairs
return view('/auth/register')->with(['name' => $select]);
You can also use compact which allows to pass variable which are accessible inside of the scope of your controller to the view and the string passed as argument to that function will be the name of variable accessible inside of the view file
$select = Angkatan::all();
return view('/auth/register', compact('select'));
You can not pass the variable in this way to the view. You have to pass an array in the second parameter of the with() method - it should be something like this:
return view('greeting', ['name' => 'James']);
return view('/auth/register', ['name' => $select]);
you can pass a second massive parameter to the view,
and also in with method, you need to pass massive as I remember.
I have in my View:
Edit</td>
But when I put 1-50 in my $property->user_id parameter, it leads to a property.
I have Route::get('/agents/{agent}/{id}/edit', 'AgentController#edit'); in my web.php file.
Route File:
Route::get('/properties/', 'PropertyController#index');
Route::get('/properties/{property}', 'PropertyController#show');
Route::get('/agents/{agent}', 'AgentController#index');
Route::get('/agents/{agent}/{id}/edit', 'AgentController#edit');
Route::post('/agents/{agent}', 'AgentController#update')->name('agent.property.update');
This is my Controller code:
public function edit($id)
{
$property = Property::find($id);
return view('agents.edit', compact('property'));
}
I don't understand this behavior in Laravel, it's not what I intend and I just want to make the route work correctly.
Laravel gives both agent_id and property_id to the controller as parameter. You are using agent_id only and assuming it as property id.
public function edit($agent_id, $property_id)
{
$property = Property::find($property_id);
return view('agents.edit', compact('property'));
}
Based on your route file, I think you should put this route:
Route::get('/agents/{agent}/{id}/edit', 'AgentController#edit');
above this one:
Route::get('/agents/{agent}', 'AgentController#index');
I want to passing a data from Shirts to details, to know a detail of product from Shirts. How to do that? I have some error here
Error's Message
This my web view
web View
FrontController.php
class FrontController extends Controller
{
public function index()
{
$shirts=Product::all();
return view('front.home', compact('shirts'));
}
public function shirts()
{
$shirts=Product::all();
return view('front.shirts', compact('shirts'));
}
public function detail($id)
{
return view('front.shirt', ['detail' => Product::findOrFail($id)]);
}
Route
Web.php
Route::get('/', 'FrontController#index')->name('home');
Route::get('/shirts', 'FrontController#shirts')->name('shirts');
Route::get('/detail', 'FrontController#detail')->name('detail');
Shirts.blade.php
<a href="{{route('detail', $shirt->id)}}">
you missing argument in route('detail')
Edit to:
Route::get('/detail/{id}', 'FrontController#detail')->name('detail');
And Try again.Hope this help :D
there is two three thing that need to change first is route file
it should be like this
Route::get('/detail/{$id}', 'FrontController#detail')->name('detail');
from this you get that parameter $id that you passes in controller .
and for view as per i see in your code there your route is
<a href="{{route('detail', $detail->id)}}">
which has to be like this
<a href="{{route('detail', $shirt->id)}}">
i think it might solve your problem
hope it will help you
I think in your Route you need to give Id parameter also,
Route::get('/detail/{id}', 'FrontController#detail')->name('detail');
This is my controller
public function show($restaurant_id)
{
$data = Restaurant::find($restaurant_id)->waitingtimes();
echo $data->first()->value; exit;
I got Trying to get property of non-object though the waitingtime model is mapped to a database table that has the value column.
Could you help please? Also, could you tell me where can I read the documentation about the returning type of the function find() thanks
Edit 1
The data is not empty; I can see the database, and the restaurant table has the id 20 and the table waitingtimes has values that its restaurant_20 is 20
Actually - what is better is to map the route to a model.
So in your Routes.php file:
Route::model('restaurant', 'Restaurant');
Route::get('/restaurant/{restaurant}', ['as' => 'restaurant.show', 'uses' => RestaurantController#show]);
Then in your controller file:
public function show(Restaurant $restaurant)
{
echo $restaurant->waitingtimes()->first()->value;
}
You can read more about Route Model Binding here.
here is the source of find()
https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Model.php#L643
and here you have the docs
http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Model.html#method_find
for your non-object error,
your $data is empty, you should check that before you use the $data collection
Finally I found the solution myself, which is:
public function show($restaurant_id)
{
$data = new Restaurant(array('id' => $restaurant_id));
$data = $data->waitingtimes();
echo $data->first()->value; exit;