Can someone please help me fixing this? I am getting a 'Undefined variable: roles' error on the view with this in the controller:
public function create()
{
$offices = Office::pluck('name', 'id');
$roles = Role::pluck('name', 'id');
return view('gebruikers.create')->with('offices', $offices, 'roles', $roles);
}
According to the docs, "As an alternative to passing a complete array of data to the view helper function, you may use the with method to add individual pieces of data to the view:"
In the code you posted your trying to post multiple pieces of data. This can be done by calling the with method multiple times.
return view('gebruikers.create')->with('offices', $offices)->with( 'roles', $roles);
Related
I want to add additional data to $user collection so can get the profile fields in the view
I have tried using $user['profileFields'] = $this->getProfileFields() and pass $user to the view using compatct or with and this is working fine.
DD
However, I have found some reference over the net saying I can extend using map but when I tried it is giving the following error
BadMethodCallException
Call to undefined method App\User::map()
So here is what I am trying to understand
Question:
Is the below code is wrong and won't work for what I am looking for and the first approach is the solution? Is there any
recommended method to add additional data to the $user collection?
public function show(User $user)
{
$user->map(function ($user){
$user['profileFields'] = $this->getProfileFields();
return $user;
});
return view('admin.user.show', compact('user'));
}
collection methods are works on the collection, here you're getting the object of user.
$user->profileFields = $user->getProfileFields();
return view('admin.user.show', compact('user'));
If the profileFields is in another table and having a foreign key with model ProfileField, then try to add a one to one relation to the User model.
Inside the User model, add a function
public function profileFields()
{
return $this->belongsTo('App\ProfileField', 'foreign_key','other_key');
}
This will give you the profileFields in every user when calling using eloquent.
I'm brand new to Laravel, I need to display around 8 different drop down menus on a page all populated from tables in my Db, I am using blades.
In my controller I can create various types of arraysin one function (using eloquent) and I can dd(); them out correctly one at a time, my issue appears to be that you can only pass one array through a controller to a view. I have tried various options I found here but without success, including ->with and compact(). I have tried defining the arrays in the controller one at a time and passing them using compact() all result in errors either the variable not defined or trying to get an non-object. I am obviously going about this all wrong any help would be great.
This is not a code issue (hence no code posted) I think it more of a Laravel issue that I don't yet understand, thanks in advance.
Try like this
class YourController extends Controller{
public function yourMethod(){
$arr1 = [];
$arr2 = [];
return view('view.name', ['arr1' => $arr1, 'arr2' => $arr2]);
}
}
If you have:
$array1 = [...];
$array2 = [...];
Then you can:
return view('path.to.view', compact('array1', 'array2');
This is my route from web.php and my controller from ReservationContoller any help as to my the arrays wont pass would be great, many thanks.
Route::get('/client/{client}/reservation/{reservation}', 'ReservationController#getReservation');
public function getReservation($client, $reservation)
{
$client = Client::findOrFail($client);
$reservation = Reservation::where('client_id', $client->id)->get();
$company = Company::where('type', 'staghen')
->where('status', 'Active')
->orderBy('comp_name')
->pluck('comp_name', 'id');
$cl = array(['client' => $client]);
$res = array(['reservation' => $reservation]);
$comp = array(['company' => $company]);
return view('admin.reservations.reservation', compact('$cl', '$res', '$comp'));
}
I'm using JSON request to get nation detail in my view, here is how I am doing it;
Controller:
public function ajaxrequest(Request $request)
{
$nations = Nation::all()->pluck('nation', 'id');
return response()->json($nations);
}
now I want to access data from Area table, I will have to create another controller? or I can add that in the above controller? I have 10 different tables like nation from where I want to get data through JSON. but I am not sure whether I can do everything in a single controller.
It all depends how do you want to access data and yes you can fetch data from one controller only if it's needed.
Also you can check it based on the request
EXAMPLE :
public function ajaxrequest(Request $request)
{
$check = $request->get('something_to_check");
if($check){
$data = Table1::all()->pluck('id');
}else{
$data = Table2::all()->pluck('id');
}
return response()->json([
'data' => $data,
//...
]);
}
Like #ViperTecPro mentioned, you can access multiple tables from the same method in a controller but if possible you should have separate endpoints for each case to you get rid of multiple if checks.
Just a thought.
I have setup Spark and I have created my custom view in Settings - Students (assume User object is actually a teacher). I have also created migration and model Student.
Now http://spark.app/settings/students returns the page successfully. At this point, I need to return data from backend. I investigated Spark\Http\Controllers\Settings\DashboardController#show - which is the method returning the 'settings' view, however this doesn't return any data to view using ->with('user', $user)
But, as mentioned in Docs, :user="user" :teams="teams" :current-team="currentTeam" already available out of the box.
Where and how does Spark returns these values to /settings? And How do I make my Student object available likewise?
Now, if I want to return my Student object to front-end, I have 2 choices.
1) edit Spark\Http\Controllers\Settings\DashboardController
2) I think Spark\InitialFrontendState is the place where Spark returns these objects user, teams, currentTeam. This approach is something I've seen for the first time to be honest and I didn't really understand how it works.
So how should I achieve in Spark, something as simple as :
return view('spark::settings')->with('student', $student); ?
Add a new route and set up your own Controller & own view
web.php
Route::get('/settings/students', 'SettingsStudentController#index');
SettingsStudentController.php
class SettingsStudentController extends Controller {
public function __construct() {
$this->middleware('auth');
}
public function index(Request $request) {
$user = Auth::user();
$student = STUDENTCLASS::whatever();
return view('yourstudentview', ['student' => $student , 'user' => $user]);
}
}
In one of my controller, I'm passing the result obtained from an Eloquent query to a view. But no matter how I pass the result to the view, I'm not getting the result in the view.
The controller:
class ProductCategoriesController extends BaseController
{
public function __construct()
{
parent::__construct();
// Add location hinting for views
View::addNamespace('product-categories', app_path() . "/MyVendor/ProductsManager/Views/admin/product-categories");
}
public function index()
{
$categories = ProductCategory::all();
return View::make('product-categories::index')
->with('title', 'All Product Categories')
->with('categories', $categories);
}
...
}
If I view the dd the value of $categories inside the index controller, I get the proper Eloquent collection, like following:
object(Illuminate\Database\Eloquent\Collection)[655]
protected 'items' =>
array (size=1)
0 =>
object(MyVendor\ProductsManager\Models\ProductCategory)[653]
protected 'table' => string 'product_categories' (length=18)
protected 'guarded' => ...
But if I dd the value of $categories inside the index view, I get an empty Eloquent collection, like following:
object(Illuminate\Database\Eloquent\Collection)[655]
protected 'items' =>
array (size=1)
0 => null
I am doing similar approach in other controllers and they work fine. I don't know what the problem with this controller is. I have been using Laravel for quite some time now and never had this problem before. Maybe I'm missing something, or there's something wrong with the recent package updates. Whatever it is, it's driving me crazy. Any ideas on what the problem might be?
P.S. I'm using Laravel Framework version 4.2.6
I finally figured out what the problem was. I was using robclancy/presenter package to wrap and render objects in the view. For it, the model had to implement a PresentableInterface interface. Implementing the interface required the method to have a getPresenter method. I had added the getPresenter method in my model, but I forgot to return the presenter from that method. Returning the presenter solved the problem.
The code inside the getPresenter method is the code that I forgot to write.
public function getPresenter()
{
return new ProductCategoryPresenter($this);
}
Thank you guys for trying to help.