how to change url for edit in laravel - laravel

I have an edit page, I have used resource controller for it. Everytime the page redirects from edit (../members/16/edit) it continues with the edit page url (../members/16/members). [where it should only take ../members]
How can I remove/change url for edit.
Route
Route::resource('members', MemberController::class);
edit function
public function edit(Member $members,$id)
{
$members = Member::find($id);
return view('edit', compact('members'));
}
public function update(Request $request,$id)
{
$members = Member::find($id);
$members->name = $request->name;
$members->save();
return view('committee')->with('success', 'Member Updated successfully');
}
<a class="btn btn-sm btn-icon btn-success" title="Edit NewsRoom Details"
href="'.route("members.edit",[$members->id]).'"><i class="fas fa-edit"></i></a>

You should identify what you are passing example:
...
Your update method is not redirecting anywhere, it is simply loading a different view, the correct way to do it is:
public function update(Request $request,$id)
{
$members = Member::find($id);
$members->name = $request->name;
$members->save();
//return view('committee')->with('success', 'Member Updated successfully');
return redirect()->route('members.committee')->with('success', 'Member Updated successfully');
}

Related

Laravel Auth - If user is not logged in hide Controller regenerated Edit and Delete buttons [Yajra DataTables]

I added Laravel Auth to my Yajra Laravel Datatables but for the life of me I cannot seem to figure out the code for the controller so that if a user is not logged in then hide Controller regenerated html buttons
Here is the AjaxdataController.php:
class AjaxdataController extends Controller
{
//base page
function index()
{
return view('student.ajaxdata');
}
//view data and make dynamic EDIT and DELETE Buttons per record
function getdata(Request $request)
{
$students = Student::select('id',
'first_name',
'last_name'
);
return Datatables::of($students)
->addColumn('action', function($student){
//EDIT AND DELETE BUTTONS
return '<i class="glyphicon glyphicon-edit"></i> Edit<i class="glyphicon glyphicon-remove"></i> Delete';
})
->make(true);
}
}
Here's what I am hoping to do with the AjaxdataController.php:
class AjaxdataController extends Controller
{
//base page
function index()
{
return view('student.ajaxdata');
}
//view data and make dynamic EDIT and DELETE Buttons per record
function getdata(Request $request)
{
$students = Student::select('id',
'first_name',
'last_name'
);
return Datatables::of($students)
//if logged in then addColumn()
if (Auth::user()){
->addColumn('action', function($student){
//EDIT AND DELETE BUTTONS
return '<i class="glyphicon glyphicon-edit"></i> Edit<i class="glyphicon glyphicon-remove"></i> Delete';
})
}
->make(true);
}
}
I figured it out:
1) In the "use" section at the top of app\Http\Controllers\AjaxdataController.php, import the Auth Facades:
use Illuminate\Support\Facades\Auth;
2) Edit the addColumn() api like so:
->addColumn('action', function($contact){
if (Auth::user()) {
//IF LOGGED IN SHOW EDIT & DELETE BUTTONS
return '<div class="btn-group"><i class="glyphicon glyphicon-edit"></i> Edit <i class="glyphicon glyphicon-remove"></i> Delete
</div>';
}else{
//IF LOGGED OUT SHOW THIS
return '<p class="text-info font-weight-bold">Login to Edit or Delete</p>';
}
})

ErrorException thrown with message "Trying to get property 'user' of non-object

I am using relationship in my laravel project. I am trying to show my post and replies. But i keep getting this error. I've tried various solution but none of them worked. Can anyone help me?
Post model:
protected $fillable = [
'title', 'content', 'category_id' , 'slug', 'user_id',
];
public function user1()
{
return $this->belongsTo('App\User');
}
public function replies()
{
return $this->hasMany('App\Reply');
}
Reply model:
protected $fillable = ['content','user_id','posts_id'];
public function post(){
return $this->belongsTo('App\Post');
}
public function user()
{
return $this->belongsTo('App\User');
}
PostController:
public function shro($slug)
{
$pst= Post::where('slug',$slug)->first();
return view('posts.show')->with('p', $pst);
}
show.blade.php
<div class="panel panel-default">
<div class="panel-heading">
<img src="/uploads/avatars/{{ $p->user->avatar }}" alt="" width="70px" height="60px">
<span>{{ $p->user->name }}, <b>{{ $p->created_at->diffForHumans() }}</b></span></br>
</div>
$p, or $pst (confusing variable names) is null, and this is because ->first() can return null. Do you have a Post record in your database that matched slug?
Rule of thumb; never assume something exists.
Add some logic to ensure the Post exists before trying to access a property of it:
$post = Post::where("slug", $slug)->first();
if(!$post){
abort(404);
}
return view('posts.show')->with('post', $post);
// OR
$post = Post::where("slug", $slug)->findOrFail(); // or firstOrFail();
return view('posts.show')->with('post', $post);
Also, check your $post->user:
#if($post->user)
<img src="/uploads/avatars/{{ $post->user->avatar }}" width="70px" height="60px"/>
<span>{{ $post->user->name }}, <b>{{ $post->created_at->diffForHumans() }}</b></span></br>
#endif
If $post->user returns null, you'll have a similar error.
You can eager load the user with it's relative post at once by doing :
//...
$pst= Post::where('slug',$slug)->with(['user1'])->first();
//...

Laravel pass param url id to controller function

I have a page with data/information on it with a download button that converts my page to pdf. Now, the download button is my problem with how I can do it.
My current page URL.
Route::get('/inventory/{id}', function ($id) {
$inventory = Inventory::find($id);
return view('layouts._inventory-template', ['inventory' => $inventory])
});
My button in the view
<span></span>PDF
Function in ClientController
public function viewPdf($id)
{
$inventory = Inventory::find($id);
$pdf = PDF::loadView('layouts._inventory-template');
return $pdf->download('inventory.pdf');
}
Question: How I can implement the logic of PDF download button in the view page?
In your router:
Route::get('/viewPdf/{id}', 'ClientController#viewPdf');
You can simple call your route in href:
<span></span>PDF
I would suggest:
Route::get('/inventory/{id}', 'ClientController#downloadPdf')->name('download-pdf');
ClientController
public function downloadPdf()
{
$inventory = Inventory::find($id);
$pdf = PDF::loadView('layouts._inventory-template', $inventory);
return $pdf->download('inventory.pdf');
}
Button
Download<i class="la la-download"></i>
Note: change Auth::user()->id in the button if ever you want something else
Simply add download property in a tag
<a href="{{ action('ClientController#viewPdf', ['id' => $request()->route('id')]) }}" class="button button-secondary" download><span></span>PDF</a>

Laravel Crinsane/LaravelShoppingcart associate() doesn't work

In my Laravel I'm using this shoppingcart .
When I add one product to my shopping cart I want to have associate() with App\Product Model .
This is my code :
public function store(Request $request)
{
// store product information to Cart
Cart::add($request->id, $request->name, 1, $request->price)
->associate('App\Product');
return redirect(route('cart.index'))
->with('success_message', 'محصول با موفقیت به سبد اضافه شد');
}
But when I want to access model $item->model->id in view I get this error :
Trying to get property 'id' of non-object (View: C:\xampp\htdocs\digikala\resources\views\cart.blade.php)
Edit :
my cart view (cart.blade.php) codes :
<table border="1" class="table">
#foreach (Cart::content() as $item)
{{ $item->model->id }}
#endforeach
</table>
Finally i could solve this problem .
Everything was about $request data inside store() method .
Mistakes were in the form of adding product to cart .
I did not post the product ID correctly to the store() method .
If anyone had this problem in the future I hope to test $request first by this code :
public function store(Request $request)
{
return $request;
}
public function store(Request $request)
{
//verify the submitted product
$product = Product::where('slug', $request->input('slug'))->first();
//check if item is available
if($product === null){
return redirect()->route('cart.index')->with('error', 'Unable to add product to cart');
}
else{
Cart::add($product->id, $product->title, 1, $product->price)
->associate('App\Product');
return redirect()->route('cart.index')->with('success', 'Product added to cart');
}
return false;
}

Codeignitter route for passing id to controller and from controller to view

I have a view where I have a Table of Users, which I print with a Loop.
In this Table I have a Link:
<i class="fa fa-edit">
In the route-config I have following route
$route['edit_user_id/(:num)'] = 'user/edit_user_id/$1';
In the Controller user I have the function
public function edit_user_id($id){
$data['id'] = $id;
$this->load->view('header');
$this->load->view('user/edit_user_id/edit_user_id', $data);
$this->load->view('footer');
}
Im trying to call the function with an id and pass this to the view where
I want to edit the User with this id
Can someone help me to find my failure?
Solution:
$data['id'] = $id;
is wrong it has to be
$data->id = $this->uri->segment(2);
public function edit_user_id($id){
$this->load->view('user/edit_user_id/edit_user_id', $data);
}
check this line of yours
I found the solution,$data['id'] = $id; is wrong it has to be $data->id = $this->uri->segment(2);

Resources