In my service provider, I bind the event model in the route.
Route::model('event', Event::class);
Then I create the following route.
Route::view('/events/{event}/overview', 'cp.event-overview')
In this view, I call a blade component that looks like this.
class EventHeader extends Component
{
public $event;
public function __construct(Event $event)
{
$this->event = $event;
dd($event);
}
}
The code returns an empty model (exist: false). But if I do the same and forward the route to a controller, then is it working. Are there any ways to inject the model into Blade components?
Assuming you are calling the component in your blade view, you can pass the Event like that:
<event-header :event="request()->route('event')"></event-header>
Related
how can i inject a variable from controller to blade ,it is header
blade so it common for every page but no route url or controller
function is connected with it, please tell me a way to send the data
from Controller To header.blade.php Is there any possible way to
inject the variable?
You can share data with all views using your AppServiceProvider located in your app/Providers/AppServiceProvider.php
In the boot() method, use the view() helper and assign data.
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
view()->share('name', 'My Name');
}
}
You can use it like any other variable in your view like this :
{{ $name }}
Hello,
I have a blade Layout called:
profile.blade.php // component
And I have a blade file infos which extends from profile.blade.php component.
On my controller, I have a profile method:
public method profile(User $user) {
return view('infos.blade.php', compact('user'));
}
When I try to use the variable user to the profile.blade.php component, I have an error that says "undefined user variable"
My question is how can I get the data I received from my controller to Blade Component profle ?
Because that part of component will be used many times.
Thanks.
in your app/view/components/profile-
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class profile extends Component
{
public $user; //for consistency
public function __construct($user)
{
$this->user = $user;
}
public function render()
{
return view('components.profile');
}
}
now the component can render a variable ($user),
in your info's blade/infos.blade.php you can feed the
variable to the component like --
<x-profile :user='$user' ></x-profile>
i understand the question was posted quite a long ago
but i had the same issue and the answers posted here
didn't work for me but this method did so.....it may help somebody.
As it's a component you have to pass user parameter each time you call it, for example
#component('profile', ['user' => $user]) #endcomponent // here $user comes from controller
I created CustomerController in Http, later, I fixed-route get customers, but getting an error in a single action Controller.
I tried to show off CustomerController view for displaying customers logged in page
Here is my error message:
Use of undefined constant view - assumed 'view' (this will throw an Error in a future version of PHP)
Looks like you're trying to access the old ways to render blade file look at this :-
return View::make('customers.index', $customersList);
To use view() method
return view('admin.pages.customers.index',compact('someVaiable'));
OR
// You can define constant for your controller get methods
private $layout;
public function __construct()
{
$this->layout = 'admin.pages.customers.';
}
public function index(){
return view($this->layout.'index');
}
Take a look a this for Single Action Controllers example
https://laravel.com/docs/5.8/controllers#single-action-controllers
The first argument of the view method in the controller should be the name of the view.
Routes/web.php
Route::get('/', 'CustomerController');
app/Http/Controllers/CustomerController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class CustomerController extends Controller
{
public function __invoke(Request $request)
{
return view('customers');
}
}
resources/views/customers.blade.php
<h1>Customers</h1>
I am developing a Laravel application. I am doing route model binding in the middleware.
I have the routes like this
Route::group([ 'prefix' => 'department/{department}', 'middleware' => [ 'auth.department' ] ], function () {
Route::post('employee/create', 'EmployeeController#store')->name('employees.store');
});
This is my auth.department middleware (AuthDepartment)
class AuthDepartment
{
public function handle($request, Closure $next)
{
$department = Department::find($request->department);
//do something with the department
//I want to set the $department (Department model) in the place of {department} in the route.
return $next($request);
}
}
This is EmployeeController
class EmployeeController extends Controller {
public function store($department)
{
}
}
As you can see in the code, I am using $department parameter to get the department id from the route. But instead of getting the integer as the parameter, I want to bind the model like this.
class EmployeeController extends Controller {
public function store(Department $department)
{
}
}
With my current code, it is not working. I tried to set the route parameter in the middleware as follow to match (bind model) the value in the action.
$request->route()->setParameter('department', $department)
But it is just not working. How can I set/ replace the route parameter with a model in the middleware which can be binded to the parameter in the action of the controller? Is it possible? What could be the better approach?
If I used
$request->route()->setParameter('department', $department)
to set the parameter, I cannot set type in the action of the controller like this.
store(Department $department)
But this is fine
store(Department $department)
But I want this
store(Department $department)
Laravel already has this built in. It is called Route Model Binding.
https://laravel.com/docs/5.7/routing#route-model-binding
Remove the middleware and instead keep your controller as it is. Laravel will automatically use the ID in the request to find the model and give you an instance of it. If the model cannot be found, Laravel will throw a 404 response for you.
I am wondering what are differences between this:
Make your own BaseController and extend laravel's controller. Then extend this one in every other controller and pass some data to all views.
class BaseController extends Controller {
public function __construct() {
$user = User::all();
View::share('user', $user);
}
}
Simply use this inside view
#php
$chats = App\Messages::all();
#endphp
I have an chat app where I display all conversations, but I am wondering is there a better solution to share Messages model to all views where it is used?
In my case I include file with chat html into all views and only in that file I call Messages model inside #foreach
UPDATE: Seems like I cant convert code into code block here :/
Try to edit it... I cant