Laravel: store in session and retrive in blade - php

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>

Related

How to make a table with permissions? Laravel

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

Laravel pass variable from FirstController to SecondController

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');

How can i categorize a post using checkbox in laravel?

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.

Form action not going to correct method in php

I'm trying to submit my form. My form was called by this function:
function controller1()
{
$code = $this->uri->segment(3);
$data['content'] = $code;
$this->load->view(FRONTEND_DEFAULT, $data);
}
But when I submit my form, it goes back to the controller1 method, but I want it to go to another form. My form is:
<form id="form1" action="controller2" method="POST">
<input type="text" name="textbox">
<input type="submit" value="submit" name="submit">
</form>
I want it to go to controller2 but it goes to controller1 :
http://my.website.com/controller1/controller2
How do I prevent it from going back to controller1?
EDIT: I am using codeigniter as my framework.
Since you're using CodeIgniter, why not just read their manual? It shows you how to quickly and easily put together a form:
http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#theform
Here is a copy & paste for a form to be properly pointed to your controller:
<?php echo form_open('controller/method'); ?>
Details on the form helper
You need to specify either a relative or absolute url
see http://www.w3schools.com/tags/att_form_action.asp
i.e. action="example.htm" or action="http://www.example.com/example.htm"
Your action="controller2" is likely failing to resolve and going back to the page that output the form though I would've thought it would give an error if it couldn't resolve that action correctly

Please help me with file upload with text fields in codeigniter

Please help me with file upload with text fields details going to database in codeigniter. ( for example i want image name and some another form field going to database and file uploads to server)
Also how do i stop the form submission in database upon page resfresh ?
The best I can do is link you to the relevant pages so that you can learn more. If you provide more information I might be able to help you more:
File upload:
http://codeigniter.com/user_guide/libraries/file_uploading.html
Database:
http://codeigniter.com/user_guide/database/index.html
I recommend using the Active Record part of the database library:
http://codeigniter.com/user_guide/database/active_record.html
As for how to stop form submission on page refresh, simply use redirect('controller/method'); after you have handled the form data. For example:
if(!is_bool($this->input->post('fieldvalue'))
{
$this->model->writeToDB($_POST);
redirect('controller/method');
}
so that every time data is submitted, it is added to the db and then the redirect will revisit the page, thus the browser won't remember what was in the post array and that will solve the problem.
view:
<form method="post" action="#">
<input type="text" name="foo"/>
<input type="submit"/>
</form>
controller:
$this->load->model('yourmodel','model',true);
if(isset($_POST['text']))
$result = $this->model->doQuery($_POST['text']);
model:
function doQuery($text){
$result = $this->db->query("insert into table $text");
return $result;
}
(edit) upload:
use $_FILES and move_uploaded file

Categories