i have a foreach with my list of products in my index.blade.php, it work well, now i'm tryng to filter, i done my menu with my categories and genders.
I would like show products with category = "t-shirt" and gender= "woman" but i have this error:
ErrorException in StoreController.php line 36: Missing argument 1 for
dixard\Http\Controllers\StoreController::products()
i'm using this link:
<a href="{{url('shop', ['category'=> 't-shirt', 'gender' => 'woman'])}}" title="">
<span>Woman</span>
</a>
my route:
Route::get('shop', 'StoreController#index');
Route::get('shop/{category}/{gender}','StoreController#products');
My controller
public function products($category, $gender)
{
$gender_id= Gender::where('gender', $gender )->first();
$category_id= Category::where('name', $category)->first();
$filter = ['gender_id' => $gender_id->id, 'category_id' => $category_id->id];
$products = Product::where($filter)->orderBy('id', 'asc')->get();
$categories = Category::all();
return view('store.index', compact('products','categories'));
}
You can use named routes. There's nothing special with this.
Route::get('shop/{category}/{gender}', [
'uses' => StoreController#products',
'as' => 'shopRoute'
]);
And your URL:
route('shopRoute', ['category'=> 't-shirt', 'gender' => 'woman'])
Use the route function (https://laravel.com/docs/5.1/routing)
<a href="{{route('shop', ['category'=> 't-shirt', 'gender' => 'woman'])}}" title="">
<span>Woman</span>
</a>
i fixed like this:
Route::get('shop','ShopController#index');
Route::get('shop/{categoryA}','ShopController#category');
Route::get('shop/{categoryB}/{genderB}','ShopController#categoryGender');
Route::get('shop/{categoryC}/{genderC}/{slugC}','ShopController#product');
I dont know why but i changed that of variables that i pass to my route and it work!
Related
In CodeIgniter 3.x, we can pass data with array by using this code:
$data=array(
'apartment' => $this->apartmentmodel->get_all(),
'title' => "List Apartement"
);
$this->load->view('apartment/index',$data);
But, this code is not working when implement on my Laravel 6 project
$data=array(
'apartment' => DB::table('apartment')->get();,
'title' => "List Apartement"
);
return view(apartment.index,$data);
What is the problem and how to fix it? Anyone can help me?
Just remove ; from line number 2 before , your it will be work
$data=array(
'apartment' => DB::table('apartment')->get(),
'title' => "List Apartement"
);
return view('apartment.index', $data);
or you can use compact too, see below docs
https://laravel.com/docs/6.x/views
In Laravel we can use eloquent for fetch data from database.
$data=[
'apartment' => Apartment::get();,
'title' => "List Apartment"
];
return view('apartment.index', ['data' => $data]);
It will help full for you.
Thanks
PHpanchal
return view('apartment.index',compact('data'));
You can use compact() method, and you have an error (if this is project code).
Try:
$apartment = DB::table('apartment')->get();
$title = 'List Apartement';
return view('apartment.index', compact('apartment', 'title'));
Or
$data=[
'apartment' => DB::table('apartment')->get();,
'title' => "List Apartement"
];
return view('apartment.index', compact('data'));
Or:
$data=[
'apartment' => DB::table('apartment')->get();,
'title' => "List Apartement"
];
return view('apartment.index', ['data' => $data]);
Update:
In template file you can use it by calling $data, because in return statement you pass the variable name that will be shared with current template. So, to get some data from your array $data you just need to do:
#foreach($data['apartment'] as $apartment)
{{ $apartment }}
#endforeach
And to get your title use $data['title'] as well
The concerning route:
Route::patch('admin/track/practice/{practice_id}/close-practice-session/{session_id}/{new?}', array(
'as' => 'close-practice-session',
'uses' => 'AdminController#closePracticeSession'
));
new is an optional route parameter.
The Controller method:
public function closePracticeSession($club, $practice_id, $session_id, $new = null)
{
$clubDetails = new ClubDetails();
$club_id = $clubDetails->getClubID($club);
date_default_timezone_set(config('app.timezone'));
$CurrentTime = date("Y-m-d H:i:s");
try
{
DB::table('practice_sessions')
->where('id', $session_id)
->where('club_id', $club_id)
->update(['is_current' => 0, 'updated_at' => $CurrentTime]);
if ($new == 'Y')
{
return redirect()->action('AdminController#getTrackPracticeSession', [$club, $practice_id]);
}
else
{
return redirect()->action('AdminController#getTrackPracticeSession', [$club, $practice_id, $session_id])
->with(array('success'=>'Practice was successfully closed.'));
}
}
catch(\Exception $e)
{
return view('errors.500')->with(self::getRequiredData($club))->with('error', $e->getMessage());
}
}
I have two forms on my view, one has the optional parameter, one doesn't.
When I click on the button on the form which has the optional parameter, I am getting a BLANK screen.
Here are some strange things:
No error message. Checked the laravel.log
Even if I remove all the logic from the controller method and do a
simple var_dump, I still get a blank screen
When I click on the button without the optional parameter, it
behaves as expected
I have been trying for the last two days to resolve this without any luck. I have even tried to make the {new} parameter mandatory. Anytime I am passing the last parameter, I am getting a blank screen.
Any idea? I am sure I am doing something silly. Just can't see it.
Update (the two forms on the view) - the csrf token is in the header.
{!! Form::open([
'method' => 'PATCH',
'route' => ['close-practice-session', $alias, $practiceDetails[0]->practice_id, $practiceDetails[0]->id]
]) !!}
{!! Form::submit('Close Session', ['class' => 'btn btn-primary btn-sm', 'style' => 'width: 160px;margin-left: 0px!important']) !!}
{!! Form::close() !!}
<!-- #2 -->
{!! Form::open([
'method' => 'PATCH',
'route' => ['close-practice-session', $alias, $practiceDetails[0]->practice_id, $practiceDetails[0]->id, "Y"]
]) !!}
{!! Form::submit('Close + Create New', ['class' => 'btn btn-secondary btn-sm', 'style' => 'width: 160px;margin-left: 0px!important']) !!}
{!! Form::close() !!}
As per your route
Route::patch('admin/track/practice/{practice_id}/close-practice-session/{session_id}/{new?}', array(
'as' => 'close-practice-session',
'uses' => 'AdminController#closePracticeSession'
));
Your controller function should be like this
public function closePracticeSession(Request $request, $practice_id, $session_id, $new = null)
{
$clubDetails = new ClubDetails();
$club_id = $clubDetails->getClubID($club);
date_default_timezone_set(config('app.timezone'));
$CurrentTime = date("Y-m-d H:i:s");
try
{
DB::table('practice_sessions')
->where('id', $session_id)
->where('club_id', $club_id)
->update(['is_current' => 0, 'updated_at' => $CurrentTime]);
if ($new == 'Y')
{
return redirect()->action('AdminController#getTrackPracticeSession', [$club, $practice_id]);
}
else
{
return redirect()->action('AdminController#getTrackPracticeSession', [$club, $practice_id, $session_id])
->with(array('success'=>'Practice was successfully closed.'));
}
}
catch(\Exception $e)
{
return view('errors.500')->with(self::getRequiredData($club))->with('error', $e->getMessage());
}
}
Please take a look at this SO post. This gave me a hint to solve my problem. I had an identical GET route in my routes.php file. Once I modified my PATCH route to the following, everything is working as expected.
Route::patch('admin/close-practice-session/{practice_id}/{session_id}/{new?}', array(
'as' => 'close-practice-session',
'uses' => 'AdminController#closePracticeSession'
));
I'm using L5.2.
I'm busy trying to create a shopping cart and I've ran into a problem that I can't seem to figure out how it's coming about.
What is suppose to happen is after I've added products to the shopping cart, I click on the shopping cart link and I'm suppose to get taken to another page that says "getCart" echoed on the page.
What is happening is that once I click on the shopping cart link I'm getting the error below and I don't see how I could be getting that error if I'm only echoing out "getCart".
The error I'm getting is
NotFoundHttpException in Handler.php line 103: No query results for model [App\Modules\Menus\Models\Menu].
My routes.php
Route::resource('/', 'OpenController');
Route::get('/{id}', 'OpenController#content');
Route::get('/add-to-cart/{id}', [
'uses' => 'OpenController#getAddToCart',
'as' => 'product.addToCart'
]);
Route::get('/shopping-cart', [
'uses' => 'OpenController#getCart',
'as' => 'product.shoppingCart'
]);
My OpenController.php
namespace App\Modules\Open\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Modules\Menus\Models\Menu;
use App\Modules\Portfolio\Models\Portfolio;
use App\Modules\Products\Models\Product;
use App\Modules\Open\Models\Cart;
use Session;
class OpenController extends Controller
{
public function getAddToCart(Request $request, $id){
$product = Product::find($id);
$oldCart = Session::has('cart') ? Session::get('cart') : null;
$cart = new Cart($oldCart);
$cart->add($product, $product->id);
$request->session()->put('cart', $cart);
return redirect()->back();
}
public function getCart(){
echo "getCart";
}
}
products.blade.php
<li>
<a href="{{ route('product.shoppingCart') }}">
<i class="fa fa-shopping-cart" aria-hidden="true"></i> Shopping Cart
<span class="badge">{{ Session::has('cart') ? Session::get('cart')->totalQty : '' }}</span>
</a>
</li>
UPDATE:
I've kind of managed to fix it, but I'm hoping someone can still help me out.
I changed my route from
Route::get('/shopping-cart', [
'uses' => 'OpenController#getCart',
'as' => 'product.shoppingCart'
]);
to
Route::get('/products/shopping-cart', [
'uses' => 'OpenController#getCart',
'as' => 'product.shoppingCart'
]);
Can someone explain why it wouldn't work with just the /shopping-cart
It wouldn't work because "/products/shopping-cart" is a different route that "/shopping-cart".
And since you call route "product.shoppingCart" you also have to have that route declared.
Hope that explained it.
That's what i'm trying to do without any success:
In welcome.blade I have a foreach with some boards and subboards(random generated by user) where you can click on subboard and go something like this /subboardOne. I got this on my routes.php
Route::get('/{subboaName}', 'ThreadController#index');
Route::post('/{subboaName}', 'ThreadController#store');
then you can post a thread on this subboard via form but since i really don't know how laravel knows where he is, the form is something like this:
<form class="form col-md-12 center-block" role="form" method="POST" action="/{{$subboardcoll->id}}">
this $subboardcoll->id comes from the controller, where it sends via the index function the collection:
public function index($subboard)
{
$subboardcoll = Subboard::where('subboaName', $subboard)->first();
$threads = Thread::where('subboaId', $subboardcoll->id)
->orderBy('created_at', 'desc')
->get();
return view('threads.thread', compact('threads', 'subboardcoll'));
}
then i'm trying to send my form and store the thread autoinserting the subboardId but laravel doesn't recognize subboards method:
public function store(Request $request)
{
$this->validate($request, [
'comentario' => 'required|max:2000',
//'g-recaptcha-response' => 'required|recaptcha',
//'imagen' => 'required',
]);
$request->subboards()->threads()->create([
'thrName' => $request->nombre,
'thrComment' => $request->comentario,
'thrImg' => $request->imagen,
'thrSubject' => $request->tema,
]);
return redirect()->back();
}
And gives me this erorr:
BadMethodCallException in Macroable.php line 81: Method subboards does not exist.
Can you guys helpme to know why? also is there better form to do what i'm trying? im newbie on laravel, thanks
EDIT:
Thread.php
public function subboard()
{
return $this->belongsTo(Subboard::class, 'subboaId');
}
Subboard.php
public function thread()
{
return $this->hasMany(Thread::class);
}
The method subboards do not exist in a request object. Consider doing this
public function store($id, Request $request)
{
$this->validate($request, [
'comentario' => 'required|max:2000',
//'g-recaptcha-response' => 'required|recaptcha',
//'imagen' => 'required',
]);
Subboard::find($id)->threads()->create([
'thrName' => $request->nombre,
'thrComment' => $request->comentario,
'thrImg' => $request->imagen,
'thrSubject' => $request->tema,
]);
//Alternative query statement
Subboard::where('id', $id)->first()->threads()->create([.....
return redirect()->back();
}
I need to return a json with all products and stores.
I tried this
$products = $products->paginate(20);
$stores=$this->getStores();
return ['product'=> $products, 'stores' =>$stores];
but $products returns empty. (If i run "return $products" it works fine)
Is there something i can do? Why laravel doesn't let me have the pagination in an array?
You could do something like this :
return Response::json(
array(
'products' => $products,
'stores' => $stores,
), 200
);
You also need to 'use' the Response facade:
use Illuminate\Support\Facades\Response;
Solution:
return ['product'=> $products->toArray(), 'stores' =>$stores];
or
return Response::json(
array(
'products' => $products->toArray(),
'stores' => $stores,
), 200
);