Call a Controller Function Directly from view button - laravel-5

Problem I want to call My controller function from my blade button and I face some errors.
(ErrorException in UrlGenerator.php line 603: Action
App\Http\Controllers\OrderController#Orderfun not defined. (View:
C:\Users\MalikTariq\Project\resources\views\admin.blade.php))
My controller function is:
public function Orderfun(){
// $data=Orders::all();
// foreach ($data as $key => $value) {
// echo "value is =".$value;
echo "My name is Malik ";
}
Route is:
Route::get('Order/Orderfun', 'AdminController#Orderfun');
My blade code is:
<button type="button" class="btn btn-danger" id="bt1">View Orders</button>

Use route method instead of action method in href attribute of a element in your view.

Related

Listener does not listen for triggered event - LIVEWIRE

I have a button on my blade that calls a select method, this method emitted an event but it is not arriving
Button blade:
<div class="ml-auto">
<button class="br-button secondary circle" type="button" aria-label="Selecionar" wire:click="selecionar({{ json_encode($user) }})">
<i class="fas fa-arrow-right"></i>
</button>
</div>
Method Select: which is inside the search component
public function selecionar($user)
{
$this->emit('userSel', $user);
$this->buscaNome = "";
$this->usuarios = [];
}
Method userSel: which is inside the create component
protected $listeners = ['userSel'];
public function userSel($user)
{
$this->state = $user;
}
I've already tried to issue directly from the blade.
Check the network tab. emit should create a 2nd request (Livewire2) and you can check whether the listener gets executed:
public function userSel($user)
{
dd($user);
$this->state = $user;
}
In your case the create component should be present on the page

Trying to get property of non-object Laravel (get string)

I am now working with my "Update function".
public function edit($id)
{
$product = Inventory::find($id);
return view('/update',compact('product')) ;
}
This is my view button in my blade
<a href="{{ route('edit',$prod->id) }}" class="btn btn-raised btn-primary btn-sm">
view
</a>
It works when I redirect it to my edit blade
but when I changed the $id to $prod_num
public function edit($prod_num)
{
$product = Inventory::find($prod_num);
return view('/update',compact('product')) ;
}
my $id is bigInt and primary key(AU) while $prod_num is a string.
how is gonna work if I want to look for the prod_id
Thank you
The find() method will looks in the primary key of your model. If you want to get a record by another attribute, you can use where() and first():
$product = Inventory::where('prod_num', $prod_num)->first();

linking in the laravel join function

I get data from two different tables with Join function. But the link code for the edit page does not work.
index.blade.php file
#foreach($data as $row)
tr>
<td>{{$row->customer_name}}</td>
<td>{{$row->customer_phone}}</td>
<td>{{$row->product_name}}</td>
<td>{{$row->sale_date}}</td>
<td>{{$row->delivery_date}}</td>
<td>
<a href="{{route('orders.create'),[$row['order_id']]}}">
<button class="btn btn-info btn-sm fa fa-edit"></button></a>
</td>
</tr>
#endforeach
web.php file
Route::group(['namespace'=>'orders', 'prefix'=>'orders', 'as'=>'orders.'], function (){
Route::get('/', 'indexController#index')->name('index');
Route::get('/create', 'indexController#create')->name('create');
Route::post('/create/', 'indexController#store')->name('create.post');
Route::get('/detail/{id}', 'indexController#show')->name('detail');
Route::get('/edit/{id}', 'indexController#edit')->name('edit');
Route::post('/edit/{id}', 'indexController#update')->name('edit.post');
});
indexController.php file
public function index()
{
$data = DB::table('orders')
->select('orders.customer_name', 'orders.customer_phone', 'products.product_name', 'orders.sale_date','orders.delivery_date')
->join('products', 'orders.product_id', '=', 'products.product_id')
->get();
return view('orders.index', compact('data'));
}
ErrorException (E_ERROR)
Cannot use object of type stdClass as array (View: D:\xampp\htdocs\personality\resources\views\orders\index.blade.php)
You have an stdClass object, you can't get parameters from it using array syntax.
$object->parameter; // object
$array['element']; // array
So instead of this:
$row['order_id']
Can you try this:
$row->order_id
Pass the ID of a model as a second argument to route and access the attribute of a model (object) with the arrow operator
<a href="{{ route('orders.create', $row->order_id) }}">
Hope this helps

how to access function defined in model to view?

my laravel model funciton :
public function isAdminOrSuperAdmin()
{
return $this->role() == config('custom_config.constants.user_types.SUPER_ADMIN')
|| $this->role() == config('custom_config.constants.user_types.ADMIN');
}
i try to access in view :
#if($user->isAdminOrSuperAdmin())
<a class="btn btn-primary pull-right" style="margin-top:
-10px;margin-bottom: 5px" href="{!! route('admin.users.create') !!}">
Add New
</a>
#endif
but it show error:
Method Illuminate\Database\Eloquent\Collection::isAdminOrSuperAdmin does not exist. (View:/resources/views/admin/users/index.blade.php)
thanks in advance.
Check the error:
Method Illuminate\Database\Eloquent\Collection::isAdminOrSuperAdmin does not exist. (View:/resources/views/admin/users/index.blade.php)
This means, you are trying to call a method of your model on a Collection instance instead of an actual User model instance.
When querying several items from your database, Laravel returns an instance of the Collection class that contains all the resulting model objects.
Maybe you are doing something like this:
public function aCoolFunction()
{
$user = User::where('column', 'value')
->get(); // <-----
return view('my_view')->with('user', $user);
}
The get() method returns a Collection, not a single element.
Try the first() instead:
public function aCoolFunction()
{
$user = User::where('column', 'value')
->first(); // <-----
return view('my_view')->with('user', $user);
}
Now in your view the $user variable will actually hold and instance of your User model user in which the isAdminOrSuperAdmin() method is defined, and not a collection of it.
I don't think this is the best way but you can pass the function to view using your controller :
in your controller :
public function index(User $user)
{
$ModelFunction = $user->yourModelFunction();
return View('test',compact('user','ModelFunction'));
}
And in your View :
{{ $ModelFunction }}
you must call function like this:
#if(is_admin_or_super_admin())

Authorization Policy argument Error in Laravel

What can I do with this:
"Argument 1 passed to App\Policies\NotePolicy::update() must be an
instance of App\Note, instance of App\User given, called in
/Users/acny/Desktop/project/vendor/laravel/framework/src/Illuminate/Auth/Access/Gate.php
on line 481 (View:enter code here
/Users/acny/Desktop/project/resources/views/notes/edit.blade.php)"
NotePolicy.php
public function update(Note $note, User $user)
{
return $user->id == $note->user_id;
}
edit.blade.php (for show update button):
#can('update', $note)
<button type="submit" class="btn btn-primary">Update Note</button>
#endcan
If I write every thing instead of 'update' in edit.blade.php then I have not error but that's not what I want.
thank for your help
https://laravel.com/docs/5.6/authorization#writing-policies
Laravel passes the $user as the first argument. Switch your arguments around.

Resources