John Davidson

php - How can I pass a variable from one view to another

0 comments
Message:


I'm trying to pass a data from a variable in one view to another without using a form, this is the code:


@foreach($ispis as $marka)
@php
$query=$marka->MarkaID;
@endphp
<a href="/marka" class="link">
<div class="column">
<img src="/logoimg/{{$marka->ImeMarke}}.png" alt="">
</div>
</a>
@endforeach

I want to take the variable $query with the id from the current loop cycle and send/create another view displaying all information from the table with the same id as the $query variable.
Im not sure if i need to make a new controller, what do i need to use in web.php to take the data from the variable and send it from a controller to a new view called "/marka".



My Answer:
To pass a variable from one view to another in Laravel, you can use the session or flash data. Here's how you can achieve this:

1. In your current view, set the variable in the session:

php
@foreach($ispis as $marka)
@php
$query=$marka->MarkaID;
session(['query' => $query]);
@endphp





@endforeach


2. In your controller, retrieve the variable from the session and pass it to the new view:

php
public function showMarka()
{
$query = session('query');
$markaData = YourModel::where('MarkaID', $query)->get();

return view('marka', compact('markaData'));
}


3. Create a new route in your web.php file to handle the request:

php
Route::get('/marka', 'YourController@showMarka');


4. Create a new view called marka.blade.php and display the data:

php
@foreach($markaData as $data)
// Display the data from the $markaData variable
@endforeach


This way, you can pass the variable from one view to another without using a form.

Rate this post

4 of 5 based on 2638 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us