Home > front end >  laravel Update and read on same page
laravel Update and read on same page

Time:09-23

I am currently making a admin dashboard to view the users. i want to make a Full crud on 1 page, i have the C, R, D part of it already finished and working. now i want to add a Update function. but this does not work, whatever i try.

Main page:


@include('admin.user.create')

  
<div >
<div >
    <h1 >Users</h1>
      
    @if(session()->get('success'))
    <div >
      {{ session()->get('success') }}  
    </div>
  @endif
  <table >
    <thead>
        <tr>
          <td>ID</td>
          <td>name</td>
          <td>picture</td>
          <td>company</td>
          <td>username</td>
          <td>email</td>
          <td>role_id</td>
          <td colspan = 2>Actions</td>
        </tr>
    </thead>
    <tbody>
        @foreach($users as $user)
        <tr>
            <td>{{$user->id}}</td>
            <td>{{$user->name}} </td>
            <td><img src="/image/{{$user->picture}}" width="100px"/> </td>
            <td>{{$user->company}} </td>
            <td>{{$user->username}} </td>
            <td>{{$user->email}}</td>
            <td>{{$user->role_id}} </td>

            <td>
                <a href="{{ route('admin.user.edit',$user->id)}}" >Edit</a>
            </td>
            <td>
                <form action="{{ route('admin.user.destroy', $user->id)}}" method="post">
                  @csrf
                  @method('DELETE')
                  <button  type="submit">Delete</button>
                </form>
            </td>
        </tr>
        @endforeach
    </tbody>
  </table>
<div>
</div>
@include('admin.user.edit')

it does include the code of admin.user.edit, but when i click on the edit button in the Read table i just keep getting errors.

controller:

    public function index()
    {
        $users = User::all();

         return view('admin.user.index', compact('users'));
    }

    public function create()
    {
    
        
    }

  
    public function store(Request $request)
    {
       $request->validate([
        'name'=>'required',
        'picture'=> '',
        'company'=> '',
        'username'=> '',
        'email'=>'required',
        'password'=>'required',
        'role_id'=> 'required',
       ]);

       $user = new User([
        'name'=> $request->get('name'),
        'picture'=> $request->get('picture'),
        'company'=> $request->get('company'),
        'username'=> $request->get('username'),
        'email'=> $request->get('email'),
        'password'=> $request->get('password'),
        'role_id'=> $request->get('role_id'),
       ]);
       $user->save();
       return redirect('admin/user');
    }

   
    public function show($id)
    {
        //
    }

 
    public function edit($id)
    {
        $users = User::find($id);
        return view('admin.user.index', compact('users'));

        
    }

Edit page i include in the main page:

<div >
    <div >
        <h1 >Editing user</h1>
 
        @if ($errors->any())
        <div >
            <ul>
                @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
        <br /> 
        @endif
        <form method="post" action="{{ route('admin.user.update', $user->id) }}">
            @method('PATCH') 
            @csrf
            <div >
 
                <label for="name"> Name:</label>
                <input type="text"  name="email" value="{{ $user->name }}" />
            </div>
 
            <div >
                <label for="email">email </label>
                <input type="email"  name="email" value="{{ $user->email }}" />
            </div>
 
           
            <button type="submit" >Update</button>
        </form>
    </div>
</div>

CodePudding user response:

Ohh I got your problem please check your mistake you used $user instead of $users in blade file for example

<form method="post" action="{{ route('admin.user.update', $user->id) }}">

you pass "users" in your controller and used $user in blade file

public function edit($id)
{
    $users = User::find($id);
    return view('admin.user.index', compact('users')); 
}

for single record please use to prefer $user in both controller and blade file

CodePudding user response:

i made 2 vars instead of giving 1 back in the edit function. this worked for me since i did not overwrite users

  • Related