2

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".

1
  • 1
    "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..." I can't really get what you are trying to do here, can you elaborate or give an example ? Commented Aug 26, 2022 at 16:28

3 Answers 3

1

If I am not wrong, you are tying to make single marka show page.

In Laravel, generally there are two ways to do that:

 1. Without Route Model Binding
 2. With Route Model Binding

routes\web.php

use App\Models\Marka;

// Without Route Model Binding
Route::get("marka/{markaID}", function($markaID) {

   $marka = Marka::findOrFail($markaID);

   return view('marka.show', compact('marka'));

});

// With Route Model Binding
Route::get("marka/{marka}", function(Marka $marka) {

   return view('marka.show', compact('marka'));

})->name('marka.show'); // use name for easiness

resources\views\marka\index.blade.php

@foreach($ispis as $marka)

    {{-- Without Route Model Binding --}}
    <a href="/marka/{{ $marka->MarkaID }}">...</a>

   {{-- With Route Model Binding --}}
    <a href="{{ route('marka.show', ['marka' => $marka]) }}">...</a>

@endforeach

resources\views\marka\show.blade.php

@extends('layouts.app')

@section('title', 'Show marka page')

@section('content')

   {{-- Display all information about marka as shown below --}}
   {{ $marka->MarkaID }}
   {{ $marka->ImeMarke }}

@endsection

You can also move $callback function code in your MarkaController.php

routes\web.php

use App\Http\Controllers\MarkaController;

Route::get("marka/{markaID}", [MarkaController::class, 'show])->name('marka.show');

App\Http\Controllers\MarkaController.php

<?php

namespace App\Http\Controllers;

use App\Models\Marka;

class MarkaController extends Controller
{   
    public function show(Marka $marka)
    {
       // With Route Model Binding

         return view('marka.show', compact('marka'));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can simply create a new view file called something like marka.blade.php, and then use the @include blade directive.

Example:

@include('marka', ['query' => $query])

Then $query will automatically be available inside your marka.blade.php view file because we have explicitly passed it through the @include directive.

Or, you can use blade components for this, which is the more modern approach.

Learn here: https://laravel.com/docs/9.x/blade#components

Comments

0

If I am understanding that you want users to click on the link and go to a new view, then you could perhaps pass the $query as a parameter in the href:

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

and cater for that in your web.php via:

Route::get('/marka/{query}', 'YourController@dosomething');

and your controller function would have something like:

    public function dosomething($query)
    {
        //use $query
    }

(this is just a rough idea but might point you in the right direction)

EDIT: JS Tech said it much better

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.