Home > database >  Differentiate two views with buttons
Differentiate two views with buttons

Time:02-23

I have a view in which I display two data that I take from a table of a DB and next to these data I have two buttons that take me to the same view in which I need to display other things. What I can't figure out is, how do I differentiate the view AFTER pressing the button? Here there are the images of the two view: https://imgur.com/a/i5BcPaw

The code is the following:

Controller:

<?php

namespace App\Http\Controllers;

use App\Models\Device;
use App\Models\DataFromRasp;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

class DeviceController extends Controller
{
    public function index()
    {
        $data=Device::all();
        return view('backend.auth.user.device', compact("data"));

    }

    public function create()
    {
        
    }

    public function store(Request $request)
    {
        
    }

    public function show(Device $deviceID)
    {
        
    }

    public function edit(Device $device)
    {
        //
    }

    public function update(Request $request, Device $device)
    {
        //
    }

    public function destroy(Device $device)
    {
        //
    }

    /**
     * Displays all data present in the table data_from_rasps
     * 
     * The data are all the devices that the raspberry can capture 
     */
    public function visualizeData() 
    {
        $data=DataFromRasp::paginate(10);
        return view('backend.auth.user.dictionary', compact("data"));
    }

    /**
     * Raspberry capture and send the data to the DB and save in another
     * table of the same DB the MAC addresses of interest
     */
    public function getData(Request $request)
    {   
        $m_data = $request->get('m_data');
        $r_data = $request->get('r_data');
        DataFromRasp::create(['MAC' => $m_data, 'RSSI' => $r_data]);
        if(($m_data == 'C4:A5:DF:24:05:7E') and Device::where('MAC_ADDR', $request->m_data)->doesntExist()){ 
            Device::create(['USERNAME'=>'Device1','MAC_ADDR' => $m_data]);
        }
        if(($m_data == '70:1C:E7:E4:71:DA') and Device::where('MAC_ADDR', $request->m_data)->doesntExist()){ 
            Device::create(['USERNAME' => 'Device2','MAC_ADDR' => $m_data]);
        }
    }

    public function scan()
    {
        $process = new Process(['C:\Simone\Università\Tirocinio\laravel-boilerplate-master', 'prova.py']);
        $process->run();
        if (!$process->isSuccessful()) { throw new ProcessFailedException($process); }
        return redirect()->route('dict');
    }

    public function singleDev(Device $deviceID){
        $data = DataFromRasp::select('RSSI', 'created_at')->where('MAC', 'C4:A5:DF:24:05:7E')->get();
        $time_array = [];
        $rssi_array = [];
        $cnt = 0;
        foreach($data as $dataItem){
            array_push($time_array, $dataItem->created_at->format('H:i:s'));
            array_push($rssi_array, $dataItem->RSSI);
        }

        return view('backend.auth.user.singleDevice', compact("time_array", "rssi_array"));
    }
}

View where I select the device:

@extends('backend.layouts.app')

@section('content')

<table >
  <thead>
    <tr>
      <th scope="col">ID</th>
      <th scope="col">USERNAME</th>
      <th scope="col">MAC ADDRESS</th>
    </tr>
  </thead>
  <tbody>
    @foreach ($data as $item)
    <tr>
      <th scope="row">{{$item->id}}</th>
      <td>{{$item->USERNAME}}</td>
      <td>{{$item->MAC_ADDR}}</td>
      <td>
         <a href="{{ url('admin/singleDevice/'.$item->id) }}" >Select</a>
      </td>
    </tr>
    @endforeach
  </tbody>
</table>
@endsection

Does anyone know how to do this? Thanks in advance

CodePudding user response:

Try to use this code

<a href="/admin/singleDevice/{{ $item->id }}" >Select</a>

Instead of

 <a href="{{ url('admin/singleDevice/'.$item->id) }}" >Select</a>

Updated!

I a little bit didn't get what you trying to achieve, but why didnt your try to add another button next to it, to show another view?

CodePudding user response:

You could create an if statement to determine if one of the buttons is pressed whenever you load the page.

In blade

<button type="submit" name="device_id" value="$item->id">Select</button>

In controller

$data=Device::all();
$singleDevice = Device::where('id', '=', Input::get('device_id'))->first();
if($singleDevice === null)
{
  return view('backend.auth.user.device', compact("data"));
}
else
{
        $time_array = [];
        $rssi_array = [];
        $cnt = 0;
        foreach($singleDevice as $dataItem){
            array_push($time_array, $dataItem->created_at->format('H:i:s'));
            array_push($rssi_array, $dataItem->RSSI);
        }

        return view('backend.auth.user.singleDevice', compact("time_array", "rssi_array"));
}
  • Related