I create order system in Laravel, I can add new order as user and I can display all order as admin user. I have only problem with edit this order as user.
This is my controller
public function index(){
$orders = Orders::with('category', 'type')->where('user_id', auth()->user()->id)->orderBy('id', 'desc')->paginate(6);
return view('account.orders', compact('orders'));
}
public function store(Request $request) {
$order = new Orders;
$order->user_id = auth()->user()->id;
$order->category_id = $request->input('category_id');
$order->type_id = $request->input('type_id');
$order->name = $request->input('name');
$order->description = $request->input('description');
$order->price = $request->input('price');
$order->save();
return back()->with([
'status'=> [
'type'=>'success',
'content'=>'Zmiany zostaĆy zapisane',
]
]);
}
public function edit(Orders $order_id){
$categories = Categories::get();
$types = OrdersCategories::get();
$order = Orders::where('user_id', auth()->user()->id)->findOrFail($order_id);
return dd($order_id);
}
When I click edit in user dashboard not show data from database.
Try This,in your controller edit function.
try {
$categories = Categories::get();
$types = OrdersCategories::get();
$order = Orders::where('user_id', auth()->user()->id)
->findOrFail($order_id);
return redirect()->action('Admin\OrderController#index');
} catch (\Exception $exception) {
return back()->withError($exception->getMessage())->withInput();
}
Because your model is called Orders. You should be using /{orders}/ instead of /{order}/ in your routes if you want to make use of route-model binding.
Change the route to Route::get('/{orders}/edit', 'OrderController#edit')->name('edit');
Related
how i have to write redirect rout for redirect after save tournament to my new tournament site.
Thanks
This doesnt work
return redirect()->route('tournaments.show', $tournament->slug);
Controller
public function store(Request $request)
{
$tournament = new Tournament();
$tournament->title = $request->title;
$tournament->city = $request->city;
$tournament->street = $request->street;
$tournament->game_room = $request->game_room;
$tournament->email = $request->email;
$tournament->registration_link = $request->registration_link;
$tournament->text = $request->text;
$tournament->phone = $request->phone;
$tournament->time_registration_at = $request->time_registration_at;
$tournament->date_registration_at = $request->date_registration_at;
$tournament->time_starter_at = $request->time_starter_at;
$tournament->date_starter_at = $request->date_starter_at;
$tournament->user_id = Auth::user()->id;
$tournament->region_id = $request->region_id;
$tournament->slug = SlugService::createSlug(Tournament::class, 'slug', $request->title);
$tournament->save();
return redirect()->back();
}
This is incorrect if using resource controller,
return redirect()->route('tournaments.show', $tournament->slug);
it should be
return redirect()->route('tournaments.show', ['tournament'=>$tournament->slug]);
provided that your routeKeyName for your route model binding is set to use a slug and not an id in the model or using the new laravel route model binding in web.php
Route::resource('tournaments',TournamentController::class)->parameters([
'tournament'=>'tournaments:slug'
]);
I am trying to redirect to a previous controller which has an id extension, but the function I'm in right now does not contain an id extension.
How can I redirect to that page again? I am giving the data from the controller.
public function comment($id, Request $request)
{
$user_name = $request->session()->get('user_name');
$ticket = Complaint::Where('id', '=', $id)
->SELECT('ticket_no')
->get();
foreach ($ticket as $tickets) {
$tik = $tickets['ticket_no'];
$comment = \DB::Select('SELECT comments.comment,comments.comment_by FROM comments JOIN complaints ON complaints.ticket_no=comments.complaint_id Where comments.comment_by=? or comments.comment_by=? AND comments.complaint_id=? ORDER BY comments.id desc limit 2', ["Admin", $user_name, $tik]);
return view('work.comment', compact('ticket', 'id', 'comment', 'user_name'));
}
}
public function getComment(Request $request, $id)
{
$complain_id = $request->input('ticket_no');
$user_name = $request->input('user_name');
$comment = $request->input('comment');
return redirect('ifa-dashboard/comment/', $id);
}
This is the route
Route::get('ifa-dashboard/comment/{id}','ComplainController#comment')->name('ifa.comment');
Route::POST('ifa-dashboard/getComment','ComplainController#getComment');
You can return the user back to the previous page using the back() method. For example:
return redirect()->back();
I have many to many relationships between posts and tags
The problem is when I want to get the post id and the tag id to link them in their immediary table I did a simple query to do that and I used latest() method in laravel query builder and it threw this error:
Method Illuminate\Support\Collection::latest does not exist.
This is my store function:
public function store(Request $request)
{
{
$this->validate($request,[
'title'=>'required',
'body'=>'required',
'tags'=>'required',
'slug'=>'required',
]);
$input = $request->all();
$tags = explode(",", $request->tags);
if ($input != null) {
$post = post::create($input);
$post->tag($tags);
// the problem is in this two lines
$post_id = DB::table('posts')->pluck('id')->latest();
$tagged_id = DB::table('tagging_tagged')->pluck('id')->latest();
//
dd($tagged_id , $post_id);
$relationship = new post_tagged;
$relationship->post_id = $post_id;
$relationship->tagged_id = $tagged_id;
$relationship->save();
return redirect('/admin/blog')->with('success' , 'Post added successfully');
}
return redirect('/admin/blog');
}
}
the idea that I want to achieve with those two lines is to get the latest id in the post table and in the tags table (which is the newest post)
and then storing it in the immediary table
I solved the problem by doing this
store function
public function store(Request $request)
{
{
$this->validate($request,[
'title'=>'required',
'body'=>'required',
'tags'=>'required',
'slug'=>'required',
]);
$input = $request->all();
$tags = explode(",", $request->tags);
if ($input != null) {
$post = post::create($input);
$post->tag($tags);
$post_id = DB::table('tagging_tagged')->latest('taggable_id')->first();
$post_id = $post_id->taggable_id;
$tagged_id = DB::table('tagging_tagged')->latest('id')->first();
$tagged_id = $tagged_id->id;
$relationship = new post_tagged;
$relationship->post_id = $post_id;
$relationship->tagged_id = $tagged_id;
$relationship->save();
return redirect('/admin/blog')->with('success' , 'Post added successfully');
}
return redirect('/admin/blog');
}
I have created a wishlist for a shopping cart.So when a non authenticate user wants to see his wishlist after he added to the wishlist. How to show the wishlist based on him before he logged in?
This is my wishlist i have made for only authenticate users:
public function addWish(Request $request)
{
if(Auth::check()){
$name = $request->name;
$product = Product::where('name' , '=', $name)->first();
$product_id = $product->id;
$product = DB::table('wishes')
->where('wishes.product_id','=',$product_id)
->where('wishes.status','=',1)
->select('wishes.product_id')
->first();
if(!$product){
$wish = new Wish();
$wish->user_id =Auth::user()->id;
$name = $request->name;
$product = Product::where('name' , '=', $name)->first();
$wish->product_id = $product->id;
// $product = Product::find($cart->product_id);
$wish->price =$product->price;
$wish->status = 1;
$wish->save();
return redirect('shop-wish');
}
else{
return redirect('shop-wish');
}
}
And this one is for show the list :
public function getWishPage()
{
$id = Auth::user()->id;
$wishList = \DB::table('wishes')
->join('products','wishes.product_id','products.id')
->select('products.feature_image','products.name','products.price as p_price','wishes.id')
->where('wishes.status','=',1)
->where('wishes.user_id','=',$id)
->get();
return view('cart.wishlist',compact('wishList'));
}
But how do i show the non-authenticate users wishlist? Any suggestion or solution would be appreciable?
public function addWish(Request $request)
{
if (Auth::check()){
// ...
} else {
$name = $request->name;
$product = Product::where('name' , '=', $name)->first();
$product_id = $product->id;
if (\Session::has("wishList.$product_id") === true) {
return redirect('shop-wish');
}
\Session::put("wishList.$product_id", $product_id);
return redirect('shop-wish');
}
}
and:
public function getWishPage()
{
if (Auth::check()) {
$wishListId = \Session::get('wishList');
dd($wishListId);
} else {
$id = Auth::user()->id;
$wishList = \DB::table('wishes')
->join('products','wishes.product_id','products.id')
->select('products.feature_image','products.name','products.price as p_price','wishes.id')
->where('wishes.status','=',1)
->where('wishes.user_id','=',$id)
->get();
return view('cart.wishlist',compact('wishList'));
}
}
I want to add a product to my cart But for that i want to logged in the user.Means after he logged in only he can see his cart. But after logged in if he sees the cart, he should see the the product just he added to cart but he saw the old one.Though I put the url on session in controller.How do i make it correct?
Here is show Login form :
public function showLoginForm()
{
Session::put('url.intended',\URL::previous());
return view('cart.login');
}
Add to Cart controller :
public function addCart(Request $request)
{
if(Auth::check())
{
$name = $request->name;
$product = Product::where('name' , '=', $name)->first();
$product_id = $product->id;
$product = DB::table('carts')
->where('carts.product_id','=',$product_id)
->where('carts.status','=',1)
->select('carts.product_id')
->first();
if(!$product){
$cart = new Cart();
$checkBox = Input::get('additionals');
if (is_array($checkBox)){
$cart->additionals = array_sum($checkBox);
}
else {
$cart->additionals =0;
}
$cart->user_id =Auth::user()->id;
$name = $request->name;
$product = Product::where('name' , '=', $name)->first();
$cart->product_id = $product->id;
// $product = Product::find($cart->product_id);
$cart->price =$product->price;
$cart->status = 1;
$cart->save();
return redirect('shop-cart');
}
else {
return redirect('shop-cart');
}
}
else{
return redirect('/login');
}
}
And Login controller :
protected function login(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required',
]);
if (auth()->attempt(array('email' => $request->input('email'),
'password' => $request->input('password'))))
{
if(auth()->user()->is_activated == '0'){
Auth::logout();
return back()->with('warning',"First please active your account.");
}
else{
return Redirect::to(Session::get('url.intended'));
}
}
else{
return back()->with('error','your username and password are wrong.');
}
}
Add to Cart Route:
Route::get('add-to-cart/{name}','CartController#addCart');
public function addcart(Request $request)
{
$id=$request->id; $time=60*24*14; /*60 * 24 * 14 = 14 drays 60=minutes 24=hours 14=days*/
$value=0;
if( Cookie::get('cart')!==null ){
$anonim=Cookie::get('cart');
DB::table("cart")->insert(["anonim"=>$anonim,"product_id"=>$id]);
return 0;
}else{
$value=DB::table("cart")->max("anonim")+1;
if(empty($value)){
$value=0;
}
DB::table("cart")->insert(["anonim"=>$value,"product_id"=>$id]);
$cookie = cookie('cart', $value, $time);
return response()->cookie($cookie);
}
}