I wanted to categorize a post using check box e.g a post that is related to sports, user shall categorize it as a sport. so how can i do this in Laravel? thanks.
This is not about Laravel. I suggest using a form that only have the checkbox with the categories, and when the user check any of the options, send it to a controller and then store in a database.
For example:
<form action="/ruta-a-tu-controlador" method="POST">
<input type="checkbox" name="category" value="Sports"> Sports
<input type="checkbox" name="category" value="Money">Money
</form>
And in your controller you recieve it with request.
public function MyController(Request $request){
$request->all();
/* Save it to your database */
}
If you give us more information, we can help you. Have you tried something? Any ideas? Is better try and then ask.
Related
Hey so I'm trying to sort entries by a type of pet, the code below is code from my blade.php
<div>
<td>
<form>
#csrf
<input name="cat" type="hidden" value="cat">
<a name="cat" href="{{ url('sorting') }}" value="cat">Cat</a>
</form>
</td>
</div>
In the blade file I'd have multiple links such as cat, dog, rabbit which essentially act as filtering options
I have a sort method in my controller that does the following
public function sorting(Request $request){
if($request->input('cat') === 'cat'){
$pets = Pet::Where('type', 'cat')->get();
return view('index', compact('pets'));
}
}
In my sort method, I'm trying to check if the cat link is clicked and then if it is it would return only pets of type cat, the problem I have is that my $request->input('cat') is returning a null. How would I correct this?
You have multiple issues in your code:
You don't seem to have a way to actually submit the form. The link in the post won't do it on it's own (unless you have some event on that link in JS)
<a>-tags don't have a value-attribute and the name-attribute means something completely different for links and is not for submitting data through forms.
A form without a method will use GET as default. You're trying to retrieve the value in PHP using $request->input() which is for POST-requests. For GET requests (which uses the query string to pass data), use $request->query().
However... you don't need the form. Just pass the value as a query parameter in the link instead:
<td>
Cat
</td>
Then in your PHP code, retrieve the value using:
if ($request->query('sort') === 'cat') {
// your code
}
So I tried to make a basic table with database integration, which worked flawlessly. Now I need to make a table that adds you the permission to edit the data you inserted into the form. But I need it to only give the person who made the form the permissions to edit it. Below I added my current progress without the permissions added. Currently I finished the Jetstream page with laravel, it has login and register so I'd want the username to be the authed person to edit the data. Is there any easy way to do it?
The controller I made:
`$rada= new Raja;
$rada->id=$req->id;
$rada->raja_nimi=$req->raja_nimi;
$rada->raja_asukoht=$req->raja_asukoht;
$rada->save();
return redirect('list');`
The view file:
<form action="add" method="POST">
#csrf
<input type="text" name="id" placeholder="Raja ID"> <br> <br>
<input type="text" name="raja_nimi" placeholder="Raja nimi"> <br> <br>
<input type="text" name="raja_asukoht" placeholder="Raja asukoht"> <br> <br>
<button type="submit">Lisa</button>
</form>
You'd need to store the id of the author in your controller, for example...
$rada= new Raja;
$rada->id=$req->id;
$rada->raja_nimi=$req->raja_nimi;
$rada->raja_asukoht=$req->raja_asukoht;
$rada->author_id = Auth::id();
$rada->save();
Then when you come to update the post in your controller you'd implement a check to make sure that the user owns the post...
$post = Raja::find($req->id);
if(Auth::id() == $post->author_id) {
//Update functionality here
} else {
//Redirect to 401 or send response message
}
A more Laravel appropriate way to perform a check when the user attempts to save a model would be to use a policy, you can find an example here
https://laravel.com/docs/8.x/authorization#policy-responses
Im very new in laravel so please if you have solution write it with code from my example code,
All I want just how to store data in session and retrive it in any page of my project, please I need you to help me by writing route,function and code in blade if needed becuase im very new in laravel
I have form in home page
<form id="booking-form">
<input type="text" id="car" name="car" >
check car
</form>
route
Route::get('/car/dashboard', 'HomeController#index')->name('car.dashboard');
function
public function index()
{
return view('index.car.cardashboard');
}
so I want to display input car in page cardashboard
cardashboard.blade.php
<label id='car'></label>
This is my two routes which pass variable on each view.
Route::resource('product', 'ProductController');
Route::resource('booking', 'BookingController');
I created a view which display the current product (ex. http://localhost:8000/product/15) using the ProductController.
Now i created another view to book this product then insert it (modal pop up) inside the product view using #include('partials.booking').
The problem is how can i pass the product id to the BookingController? It is possible?
This depends a lot on what framework you use in your frontend ( jquery etc. )
Usually your popup should somehow know what product it is referred to. You can do this for example like this
<!-- this is your html / blade -->
<div class="booking-popup">
<button class="book" data-product="{{$product->id}}">Book Now!</button>
</div>
You can then use ajax and jquery for example to get the data attribute
Another option is to provide a form with a hidden field
<form action="/booking" method="post">
{{csrf_field()}}
<input type="hidden" name="product_id" value="{{$product->id}}" />
<input type="submit" value="Book Now!" />
</form>
If you use the laravel html collective you can simplify it even more
EDIT
Since your Bookings actually belong to a product you could also rearrange your resources so that a booking is create with the route
/product/5/booking (POST)
You could then simply access the product id as parameter of the route
EDIT 2
To access it from your controller simply ( case of hidde form input )
public function store(\Illuminate\Http\Request $request) {
dd($request->input('product_id');
}
If you defined it via route simply go for whatever you set as placeholder
Route::post('product/{product}/book', 'BookingController#store');
Controller then:
public function store(\Illuminate\Http\Request $request, $product) {
dd($product)
}
Include your model page as #include('partials.booking', ['product'=>$product])
Change your form action to
<form action="{{url('product/'.$product->id.'/booking')}}" method="post"> and most important add _token hidden field if your CSRF middleware is enabled.
Change your route to
Route::post('product/{product_id}/booking', 'BookingController#store');
Just want to get some direction on how I'd accomplish this; I have a form
<form method="POST" action="myform.php">
<input name="selecttype" type="radio" value="send1only" checked><input name="selecttype" type="radio" value="send1and2">
<input type="submit" value="Next">
</form>
I have to post files for example, myform.php and myform2.php. I'm trying to accomplish something where if the 'send1only' is selected, the form posts to myform.php only. And if 'send1and2' is selected, the form posts to both myform.php and myform2.php.
How would I go about this? Basically I'm building a form with paypal so giving customer the option to process payment later (send1only) or now (send1and2), one post wouldn't be a file it'll be the paypal link and other would be to get info to me.
You can't post to two actions but you should be able to include the contents of myform2.php and there by perform the same processing conditionally
<?php
// contents of myform1.php
if (isset($_POST["selecttype"]) && $_POST["selecttype"] == "send1and2"){
// contents of myform2.php or
include "myform2.php";
}
?>