mount() function doesn't accept parameters in Livewire? - laravel

<?php
namespace App\Http\Livewire\Components;
use Livewire\Component;
class PropertyCard extends Component
{
public $title;
public function mount($title)
{
$this->title = $title;
}
public function render()
{
return view('livewire.components.property-card');
}
}
This is Livewire controller
<livewire:components.property-card title="Test 1" />
This livewire component.
Error is
Unable to resolve dependency [Parameter #0 [ $title ]] in class App\Http\Livewire\Components\PropertyCard (View: D:\Projects\Laravel\clemsonporter.com\resources\views\livewire\new-launch-page.blade.php)

It seems you have missed a double colon (:) before title.
<livewire:components.property-card :title="Test 1"/>
Read more on parameters here.

Public properties dont need to be set in mount. Livewire will automatically detect them. The following snippet does exactly the same as your code:
class PropertyCard extends Component
{
public $title;
public function render()
{
return view('livewire.components.property-card');
}
}
But that's besides the error you're experiencing. Qirel is correct, there's some component where you aren't setting the title property.

Related

How to use policy in laravel livewire (return, it is not a trait)?

I have category policy as below partial code.
class CategoryPolicy
{
use HandlesAuthorization;
public function view(User $user, Category $category)
{
return true;
}
}
Then, I call from livewire component inside the mount method.
class Productcategorysetup extends Component
{
use CategoryPolicy;
public function mount()
{
$this->authorize('view',CategoryPolicy::class);
}
}
I got an error message
App\Http\Livewire\Generalsetting\Productcategorysetup cannot use App\Policies\CategoryPolicy - it is not a trait
Any advice or guidance on this would be greatly appreciated, Thanks.
To use authorization in Livewire, you need to import the AuthorizesRequests trait first, and use that in your class.
Secondly, the first argument to authorize() when using view, is the instance of a model - in your case, a category. But this sounds like you want to list categories, i.e. the "index" file - which means you want to check for viewAny (as view is for a specific resource). In that case, the second argument is the class-name of the model, rather than the instance of a model.
<?php
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use App\Models\Category;
class Productcategorysetup extends Component
{
use AuthorizesRequests;
public function mount()
{
$this->authorize('viewAny', Category::class);
}
}
Then in your policy,
class CategoryPolicy
{
use HandlesAuthorization;
public function viewAny(User $user)
{
return true;
}
public function view(User $user, Category $category)
{
return true;
}
}

Laravel : need a controller to be called on all views

What i want is to load a sidebar with a controller inside, on my layouts/app.blade.php.
i have read that the best way is to load it on AppServiceProvider, so i tried this :
View::composer('layouts.app', function ($view) {
$data = \App\Http\Controllers\DeliveryController::index();
$view::share('Delivery',$data);
});
That works, but the DeliveryController::index gave me this error :
Using $this when not in object context
The way that really works is to forget the AppServiceProviers and to do it on every views controller like this :
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Management\DeliveryManagement;
class WelcomeController extends Controller
{
protected $deliveryManagement;
protected $nbLastsDelivery = 3;
public function __construct(DeliveryManagement $deliveryManagement)
{
// $this->middleware('auth');
$this->deliveryManagement = $deliveryManagement;
}
public function index()
{
$deliveries = $this->deliveryManagement->getLasts($this->nbLastsDelivery);
return view ('welcome', compact('deliveries'));
}
}
Unfortunately i think AppServiceProviers is a better way, right ?
If someone can help me i would be very grateful !
EDIT :
I add code of DeliveryController and DeliveryManagement :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\DeliveryRequest;
use App\Management\DeliveryManagement;
class DeliveryController extends Controller
{
protected $deliveryManagement;
protected $nbLasts = 3;
public function __construct(DeliveryManagement $deliveryManagement)
{
$this->deliveryManagement=$deliveryManagement;
}
public function index()
{
$deliveries=$this->deliveryManagement->getLasts($this->nbLasts);
return $deliveries;
}
and :
<?php
namespace App\Management;
use App\Models\Delivery;
class DeliveryManagement extends ResourceManagement
{
protected $delivery;
public function __construct (Delivery $delivery)
{
$this->model=$delivery;
}
public function getLasts($limit)
{
$req = $this->model->orderBy('deliveries.id', 'desc')->skip(0)->take($limit)->get();
$i=0; $render = array();
foreach($req as $delivery)
{
if($i=0)
$render = [$delivery, 'latest'];
else
$render = [$delivery, 'older'];
$i++;
}
return $render;
}
}

Livewire, modal is not shown, why?

muestramodal.blade.ph
<x-button.link wire:click="edit">Edit</x-button.link>
<x-modal.dialog wire:model="edit">
<x-slot name="title">Edit transaction</x-slot>
<x-slot name="content">Hola</x-slot>
<x-slot name="footer">
</x-slot>
</x-modal.dialog>
muestramodal.php
<?php
namespace App\Http\Livewire;
use Livewire\Component;
class Muestramodal extends Component
{
public $showEditModal = false;
public function edit()
{
$this->showEditModal = true;
}
public function render()
{
return view('livewire.muestramodal');
}
}
Why is not shown modal , with click event button???
Several hours trying to solve this problem.....
Please , can anyone help me? Thanks
need to update your muestramodal.php and extends LivewireUI\Modal\ModalComponent;
<?php
namespace App\Http\Livewire;
LivewireUI\Modal\ModalComponent;
class Muestramodal extends ModalComponent
{
public $showEditModal = false;
public function edit()
{
$this->showEditModal = true;
}
public function render()
{
return view('livewire.muestramodal');
}
}

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 OOP: trying to call a var in a function that has been declared in the same class

i'm in my pages controller trying to call a variable in a function that has been declared in the same class, it's not working properly.
When I put the vars back in the function, all works good.
Please let me know what i'm doing wrong:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
class PagesController extends Controller
{
public $Fname = 'Mobile';
public $Lname ='Store';
public function about()
{
return view('pages.about', compact('Fname', 'Lname'));
}
public function contact()
{
$Fname = 'Mobile';
$Lname ='Store';
return view('pages.contact', compact('Fname', 'Lname'));
}
}
I'm receiving an Undefined variable: Fname error.
Thanks.
Erez
first you have to look at this.
this is simple oops concept not a laravel error at all.
public function about()
{
$Fname = $this->Fname;
$Lname = $this->Lname;
return view('pages.about', compact('Fname', 'Lname'));
}
Try this in your code:
public function about()
{
$Fname=$this->Fname;
$Lname=$this->Lname;
return view('pages.about', compact('Fname', 'Lname'));
}

Resources