How to pass Eloquent Data to blade component? - laravel

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

Related

Laravel Livewire model property binding

This is my Livewire component
<?php
namespace App\Http\Livewire\Dashboard;
use App\Models\Post;
use Livewire\Component;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class EditPost extends Component
{
use AuthorizesRequests;
public ?Post $postSingle = null;
protected $rules = [
'postSingle.priority' => 'digits_between:1,5',
];
public function setPriority(Post $postSingle, $priority)
{
$this->authorize('update', $postSingle);
$this->postSingle->priority = $priority;
}
}
In my blade view I have
<button wire:click="setPriority({{ $postSingle->id }}, 4)"></button>
and somewhere else I show the priority with {{ $postSingle->priority }}
The reason why I don't do model property binding directly with wire:model="postSingle.priority" is that I want to run $this->authorize('update', $postSingle);.
What happens with the code below is that if I click the button, $postSingle->priority in the blade view is updated, but the Post record is not updated in my database. What am I missing?
You appear to have overlooked actually saving the record.
public function setPriority(Post $postSingle, $priority)
{
$this->authorize('update', $postSingle);
$this->postSingle->priority = $priority;
// save the record
$this->postSingle->save();
}

Laravel route model binding for Blade view component

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>

Laravel Page Route Not Using The Correct Parameters Passed In

Hey all you smart people,
Im having a issue I normally work with API routes not really used Web Routes before and finding this rather complicated for some reason :D
Ive made this route
Route::get('/test/{page?}', \App\Http\Livewire\Test::class);
sand this is my logic in the render() in the controller
public function render(Request $request, $page = 1)
{
dd($page);
}
however when I'm on the browser and type
http://url.com/test/2
The Die Dump keeps giving me page 1 all the time am i missing something here ??
Thanks for the help if anyone can help...
Update
Im not sure if its because I'm using a livewire component and not an actual controller....
Livewire Component
<?php
namespace App\Http\Livewire;
use Illuminate\Http\Request;
use Livewire\Component;
class Test extends Component
{
public function render(Request $request, $page = 1)
{
dd($page);
return view('livewire.test');
}
}
Route Parameters in livewire works like this
web.php
Route::get('/test/{page?}', \App\Http\Livewire\Test::class);
component
public function mount($page = 1)
{
dd($page);
}
ref link https://laravel-livewire.com/docs/2.x/rendering-components#route-params

Define a variable not explained

Sorry for the dumb question but can anyone please tell me how to define a variable in very simple terms? I have struggled for several months with "undefined variable" errors. Are variables stored in config? Or maybe in routes?
I have a database with a customers table. When I put this on my view home page {{$customers->name}} I get Undefined variable: customers.
Fine. So how and where do I define a variable. I would have thought it WAS defined given that the database table is literally called customers. Ugh!
My model file Customer.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $fillable = ['name', 'phone'];
public function address()
{
return $this->hasOne(CustomerAddress::class);
}
public function purchases()
{
return $this->hasMany(CustomerPurchase::class);
}
}
Undefined variable means the variable does not exist and the reasons for your case is, you did not pass it in the view.
Usually, to get the customers records from the database to your views, you can do it in several ways:
Query it prior to loading your view then pass it to your views:
//doing it in the controller
//create a controller: php artisan make:controller CustomerController
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use App\Customer; //Dont forget to import your Customer model
class CustomerController extends BaseController
{
public function index()
{
$customers = Customer::get(); //this will fetch the customer using your mdoel
return view('customer', ['customers' => $customers]); //this will pass the records to the view
}
}
//then in your routes/web.php:
Route::get('/customers', 'CustomerController#index'); //when you go to your application/customers in the browser, it will go to the controller and return the view with the records.
//OR you can skip the controllers and do it in the routes/web.php directly as what #jitesh jose mentioned.
Query straight into your view (Not really recommended, but sometimes you just need to make it work)
In your customer.blade.php
#php
$customers = \App\Customer::get();
#endphp
<ul>
#foreach($customers as $customer)
<li>{{$customer->name}}</li>
#endforeach
</ul>
My advice, try to watch a few basic Laravel videos so that you will understand the flow of the request and response.
If your model name is Customer,laravel automatically pick the table name as customers.Otherwise you have to use your desired table name in Model as follows.
protected $table = 'customers_table';
In your web.php
Route::get('/home',function () {
$customers = DB::table('customers_table')->get();
OR
$customers = Customer::get();
return view('welcome')->with('customers',$customers);
});
You can use$customers in welcome.blade.php as
#foreach($customers as $customer)
{{$customer->name}}
#endforeach

Laravel blade file can't view

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>

Resources