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();
}
Related
Employees.php file
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* #method static find($id)
*/
class Employees extends Model
{
use HasFactory;
protected $table = 'employees';
public $timestamps = false;
}
EmployeesController.php
<?php
namespace App\Http\Controllers;
use App\Models\Employees;
class EmployeesController extends Controller
{
public function employees_salaries()
{
return view('director.employees_salaries');
}
public function employees()
{
$employees = Employees::all();
return view('director.employees', ['employees'=>$employees]);
}
public function destroy($id)
{
$employees = Employees::find($id);
$employees->delete();
return redirect('/director.employees')->with('status', 'Your Data is Deleted');
}
}
employees.blade.php file
<from action="/delete/{{$employee->id}}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-delete btn-form me-3">
Delete
</button>
</from>
route.php file
Route::match(['get', 'post'], '/employees', array(EmployeesController::class, 'employees'))->name('employees');
Route::delete('/delete/{id}', array(EmployeesController::class, 'destroy'))->name('delete');
I cleared the cache but have no idea what the problem is. it looks like I wrote the function correctly
p.s version Laravel 9
mySQL 8
phpMyAdmin
Welcome to SO, i think youre not using the variable you assigned the value into,from controller in your blade view. maybe try to make sure you have the right variable or maybe try to use var dump.
try to put this in your controller b4 parsing value to view, to check whether you get the data you wanted or not
dd('$employees');
make sure you use the variable you assigned in your controller to your view
<?php
namespace App\Http\Controllers;
use App\Models\Employees;
class EmployeesController extends Controller
{
public function employees_salaries()
{
return view('director.employees_salaries');
}
public function employees()
{
$employees = Employees::all();
return view('director.employees', ['employees'=>$employees]); //<< here you name the variable 'employees'
}
public function destroy($id)
{
$employees = Employees::find($id);
$employees->delete();
return redirect('/director.employees')->with('status', 'Your Data is Deleted');
}
}
change this variable in view
<from action="/delete/{{$employee->id}}" method="POST"> //<< this one you use '$employee' instead of '$employees'
#csrf
#method('DELETE')
<button type="submit" class="btn btn-delete btn-form me-3">
Delete
</button>
</from>
there might also be other problem, but for now thats what i can point out.
since im also learning.
I am using first time laravel livewire in my project. I am trying to create one simple filter option and this filter will trigger from a dropdown. My problem is when a dropdown value change its completely hide my component view.
I am not sure where i am making a mistake in this but i want to know the best way to resolve this.
my code is this Livewire component
<?php
namespace App\Http\Livewire\Frontend\Boat;
use App\Models\Product;
use Livewire\Component;
class Listing extends Component
{
public $filter_option;
// public $message;
public $product;
public function mount()
{
$this->product = Product::where([
'type_id' => 1,
'status' => 1,
])->orderBy('name', 'asc')->get();
}
public function filter_speed()
{
$this->product = Product::where([
'type_id' => 1,
'status' => 1,
])->orderBy('name', 'asc')->get();
}
public function render()
{
return view('livewire.frontend.boat.listing');
}
}
My blade is
<select wire:model="filter_option" class="form-control" wire:change='filter'>
<option value="#">Filter</option>
<option value="speed">Speed</option>
</select>
//or
<button wire:click='filter_speed'>Filter by speed</button> <!-- Simple button click -->
In my opinion in your case its not necessary to use Query on mount().
But If in case of Filter u can use this method:
<?php
// Livewire
namespace App\Http\Livewire\Frontend\Boat;
use App\Models\Product;
use App\View\Components\comment;
use Livewire\Component;
class Listing extends Component
{
public $filter_option;
// public $message;
// public $product;
public $orderBy = 'id';
public $orderType = 'desc';
public function filter_speed()
{
$this->orderBy = 'speed';
// $this->emit('filter_speed');
}
public function render()
{
$product = Product::where([
'type_id' => 1,
'status' => 1,
])
->orderBy($this->orderBy, $this->orderType)
->get();
return view('livewire.frontend.boat.listing', compact('product'));
}
}
<button wire:click.prevent='filter_speed'>Filter by speed</button>
Instant of making many Query make one with custom variable
here's the code for livewire whenever I run this code it shows nothing I am using laravel 7 and livewire for this approach.
For controller
<?php
namespace App\Http\Livewire;
use App\Models\brands;
use Livewire\Component;
class SearchBar extends Component
{
public $searchTerm;
public function render()
{
$searchTerm = '%'.$this->searchTerm.'%';
return view('livewire.search-bar', [
'users' => brands::where('Name', 'like', $searchTerm)->get()
]);
}
}
For view
<div>
<input type="text" placeholder="Search users..." wire:model='searchTerm'/>
<ul>
#foreach($users as $user)
<li>{{ $user->Website }}</li>
#endforeach
</ul>
</div>
I think it should actually work. but if not, this is how i would write it.
You do not need to pass the variables via the view() function.
<?php
namespace App\Http\Livewire;
use App\Models\brands;
use Livewire\Component;
class SearchBar extends Component
{
public $searchTerm;
public $users
public function render()
{
$searchTerm = '%'.$this->searchTerm.'%';
$this->users = brands::where('Name', 'like', $searchTerm)->get();
return view('livewire.search-bar');
}
}
I hope this works, otherwise maybe something is wrong with the livewire installation?
I think your issue is that you're searching your Brands model for a "Name" where database columns are typically lowercase, so you would need to use "name" instead. Unless you named the column with an uppercase, you would need to use lowercase. Here is also how I would format the Livewire Component.
<?php
namespace App\Http\Livewire;
use App\Models\Brands;
use Livewire\Component;
class SearchBar extends Component
{
public $searchTerm;
public function render() {
return view('livewire.search-bar', [
'users' => $this->users,
]);
}
public function getUsersProperty() {
$query = Brands::query()
->when($this->searchTerm, fn($query) => $query->where('name', 'like', '%'.$this->searchTerm.'%'))
->get()
return $query;
}
}
I would isolate the function to actually retrieve the Brands to another function, so it doesn't clutter your render method. Notice that I didn't declare a public $users variable. When you don't declare a variable in a component, Livewire will search for a getVariableNameProperty() function and call that. So on each render, you are calling that function without having to explicitly call it, and returning the query results to the view.
Also just a note, models are typically singular unless you explicitly create them as plural, so your Brands model could be Brand. If your model does happen to be Brand, you just need to change the Brands parts in the above code to Brand. As well, you are using a lowercase first letter on your model--that needs to be uppercase.
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 have been trying to list a dropdown in the index page with data from database. I created a model and made some changes in controller to display it in my view page but making any change in the controller gives a blank page with 500 Internal server error in the console. Please help me out to sort this problem.
Table name: walker_type
Routes:
Route::get('/', 'WebController#index');
Model: ProviderType.php :
<?php
class ProviderType extends Eloquent {
protected $table = 'walker_type';
}
Controller: WebController.php
public function index() {
$walkerTypeList = ProviderType::all();
return view('website.index')->with(['walkerTypeList' => $walkerTypeList]);
}
View:index.php
#foreach ($walkerTypeList as $car)
<option data-icon="glyphicon-road" value="{{ $car->name }}"> {{ $car->name }} </option>
#endforeach
Had u declare your model below your namespace?
eg. use App\WalkerType;
also you forgot to declare a namespace to your Model.
it should have namespace App;
or if you have a folder for your model to make it more conventional.
you should have a namespace on each of your model.
eg. App\Model
and then use that in every controllers by declarin in between your namespace and class
eg.
namespace App\Controllers;
use App\Model\WalkerType;
class SomeController extends Controller{
protected $data; //this is a class variable that can call anywhere to your class by calling it this way $this->data
public function some_method(){
$this->data['variable_a'] = "some_value"; //this can call in you view later by $variable_a
$this->data['sum'] = 1+4; //result can be display in your view by calling the variable $sum
return view('someview',$this->data);
}
}
I hope this can help you for your project efficiently, cause we had experienced that we forgot to include some of the data that has been processed on the controller and needed to display in your view file but forgot to include.