“如何修复location.href中的laravel错误”

时间:2019-07-06 15:48:32

标签: location

1。我在控制器中有两个视图,一个用于通过ajax搜索数据。 另一个是通过使用一个公用表来进行欺诈操作。 无法转到搜索结果的显示页面”

我尝试将id}}}“>标记路由到id的搜索结果以转到显示页面,但不起作用,相同的路由在CRUD操作中效果很好。 然后我给onclick函数安装了href,并尝试定位。href=“ {{route('crud.show',$ row-> id)}}”“; 没有成功

<?php

namespace App\Http\Controllers;
use App\Crud;
use App\Pat;
use Illuminate\Http\Request;
use Image;
use DB;
class CrudsController extends Controller
{

    public function view(){
          return view('crud.view');
    }
    public function index()
    {
        $data=Crud::latest()->paginate(5);
        return view('crud.index',compact('data'))
        ->with('i', (request()->input('page', 1) - 1) * 5);       
    }
    function action(Request $request)
    {
         if($request->ajax())
         {
          $output = '';
          $query = $request->get('query');
          if($query != '')
          {
           $data = DB::table('cruds')
             ->where('id', 'like', '%'.$query.'%')
             ->orWhere('first_name', 'like', '%'.$query.'%')
             ->orWhere('last_name', 'like', '%'.$query.'%')             
             ->orderBy('id', 'desc')
             ->get();             
          }
          else
          {
           $data = DB::table('cruds')
             ->orderBy('id', 'desc')
             ->get();
         }
          $total_row = $data->count();
          if($total_row > 0)
          {
           foreach($data as $row)
           {
            $output .= '
         <tr >
         <td  > '.$row->id.'</td>
         <td onclick="my()">
          <a href="{{route('crud.show', $row->id)}}">'.$row>first_name.'</a>
         </td>         
         <td >'.$row->last_name.'</td>                 
         </tr>
         ';
       }
      }
      else        
        {       
       $output = '
       <tr>
        <td align="center" colspan="5">No Data Found</td>
       </tr>
       ';
      }
      $data = array(
       'table_data'  => $output,
       'total_data'  => $total_row
      );
      echo json_encode($data);
     }
    }    
    public function create()
    {
        return view('crud.create');
    }    
    public function store(Request $request)
    {
        $request->validate([
            'first_name'    =>  'required',
            'last_name'     =>  'required',
            'image'         =>  'required|image|max:2048'
        ]);

        $image = $request->file('image');

        $new_name = rand() . '.' . $image->getClientOriginalExtension();
        $image->move(public_path('images'), $new_name);
        $form_data = array(
            'first_name'       =>   $request->first_name,
            'last_name'        =>   $request->last_name,
            'image'            =>   $new_name
        );

        Crud::create($form_data);

        return redirect('crud')->with('success', 'Data Added successfully.');
    }


    public function show($id )
    {
     $data = Crud::find($id);

        return view('crud.show',compact('data'));
    }


    public function edit($id)
    {
        $data = Crud::find($id);
        return view('crud.edit', compact('data'));
    }   
    public function update(Request $request, $id)
    {
       $image_name = $request->hidden_image;
        $image = $request->file('image');
        if($image != '')
        {
            $request->validate([
                'first_name'    =>  'required',
                'last_name'     =>  'required',
                'image'         =>  'image|max:2048'
            ]);

            $image_name = rand() . '.' . $image->getClientOriginalExtension();
            $image->move(public_path('images'), $image_name);
        }
        else
        {
            $request->validate([
                'first_name'    =>  'required',
                'last_name'     =>  'required'
            ]);
        }

        $form_data = array(
            'first_name'       =>   $request->first_name,
            'last_name'        =>   $request->last_name,
            'image'            =>   $image_name
        );

        Crud::whereId($id)->update($form_data);

        return redirect('crud')->with('success', 'Data is successfully updated');
     }    
    public function destroy($id)
    {
        $data = Crud::findOrFail($id);
        $data->delete();

        return redirect('crud')->with('success', 'Data is successfully deleted');
    }



}
****************************************************************************
ajax for search
*************************
<script>
$(document).ready(function(){

 fetch_customer_data();

 function fetch_customer_data(query = '')
 {
  $.ajax({
   url:"{{ route('view.action') }}",
   method:'GET',
   data:{query:query},
   dataType:'json',
   success:function(data)
   {
    $('tbody').html(data.table_data);
    $('#total_records').text(data.total_data);
   }
  })
 }

 $(document).on('keyup', '#search', function(){
  var query = $(this).val();
  fetch_customer_data(query);
 });
});
</script>

<script>
  function my() {
   locatio.href="{{route('crud.show', $row->id)}}"; 
  }


</script>
****************************************************************************
show.blade.php
*******************

  @if ($errors->any())
<div class="alert alert-danger">
    <ul>
        @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
        @endforeach
    </ul>
</div>
@endif

<br>
    <br>
<div class="row" style=" background: lightgreen;">

    <div class="col-12" style="border: 2px solid red;">
        <h3 align="center">Patient's Profile</h3>
        <div class="card col-md-6 col-md-offset-4" style="width: 40rem;  padding-top:15px;" align="center">
            <img src="{{ URL::to('/') }}/images/{{ $data->image }}" class="img-rounded" width="300" class="card-img-top" alt="...">
            <div class="card-body">
                <h5 class="card-title text:center;">
                    <h1 align="center">Hi,</h1>
                </h5>

            </div>
            <ul class="list-group list-group-flush">
                </li>
                <li class="list-group-item">

                    <li class="list-group-item">
                        <label  class="col-sm-4 control-label">Last Name</label>
                        <input type="text" name="first_name" value="{{ $data->first_name }}" /> </li>
                    <li class="list-group-item">
                        <label for="inputPassword3" class="col-sm-4 control-label"> First Name</label>
                        <input type="text" name="first_name" value="{{ $data->last_name }}" />
                    </li>

            </ul>
            <div class="card-body">
                <a href="{{ route('crud.index') }}" class="btn btn-primary">Back</a>

            </div>
        </div>
    </div>
</div>
</div>
</div>


I want goto show page from searched result by clicking result row.

ErrorException (E_ERROR)
Trying to get property of non-object (View: C:\Users\king\Desktop\livenn\resources\views\crud\show.blade.php)
***********************************************************

0 个答案:

没有答案