boby

boby

Member Since 1 Month Ago

Experience Points
260
Total
Experience

4,740 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
0
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start-engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 1
260 XP
Oct
13
5 days ago
Activity icon

Replied to Forcing A Unique Rule To Ignore A Given Id Proper Way

If I remove my $id then I have error Undefined property: App\Http\Controllers\UserController::$user for:

'username' => ['required', 'string','min:3', 'max:255', Rule::unique('users')->ignoreModel($this->user)],
Activity icon

Replied to Forcing A Unique Rule To Ignore A Given Id Proper Way

what "Rule" should I import?

in

public function update(UpdateUserRequest $request, User $user) 

I need my $id parameter as well. If I do this:

public function update(Request $request, $id, User $user)

then I have error to many arguments

Activity icon

Replied to Forcing A Unique Rule To Ignore A Given Id Proper Way

web.php:

Route::resource('users', 'UserController');
Auth::routes();

and UserController:

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function index()
    {
    //not relevant
    }

    public function create()
    {
        return view('users.create');
    }

    public function store(Request $request)
    {
        $attributes = $this->validateUser();

        $user = User::create($attributes);
    
        return redirect ('/users')->with('success','User added successfully.');
    }

    public function show($id)
    {
        //not relevant
    }

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

    public function update(Request $request, $id)
    {
        $attributes = $this->validateUser();
        
        $user = User::find($id)->update($attributes);

        return redirect ('/users')->with('success','User changed successfully.');        
    }

    public function destroy($id)
    {
        //
    }

    protected function validateUser()
    {
        $rules = [    
            'username' => ['required', 'string','min:3', 'max:255', 'unique:users'],
            'first_name' => ['required', 'string','min:3', 'max:255'],
            'last_name' => ['required', 'string','min:3', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'date_of_birth' => ['required', 'date'],
            'image'  => ['image','mimes:jpeg,png,jpg,gif,svg','max:4098',
                        'dimensions:min_width=250,min_height=350',
                        'dimensions:max_width=1200,max_height=1500'],            
        ];

        if (! $this->user) {
            $rules['username'] = ['required', 'string', 'min:3', 'max:255'];
            $rules['email'] = ['required', 'string', 'email', 'max:255'];
        }

        return request()->validate($rules);

    }
}
Activity icon

Started a new conversation Forcing A Unique Rule To Ignore A Given Id Proper Way

I saw there is a 15+ threads about this, but I just can't get it work!

This is my validation for user create and edit:

protected function validateUser()
    {
        $rules = [    
            'username' => ['required', 'string','min:3', 'max:255', 'unique:users'],
            'first_name' => ['required', 'string','min:3', 'max:255'],
            'last_name' => ['required', 'string','min:3', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'date_of_birth' => ['required', 'date'],
            'image'  => ['image','mimes:jpeg,png,jpg,gif,svg','max:4098',
                        'dimensions:min_width=250,min_height=350',
                        'dimensions:max_width=1200,max_height=1500'],            
        ];

        if (! $this->user) {
            $rules['username'] = ['required', 'string', 'min:3', 'max:255'];
            $rules['email'] = ['required', 'string', 'email', 'max:255'];
        }

        return request()->validate($rules);

    }

So I want to ignore id of a current user for a username and email. I have also tried:

...
'username' => ['required', 'string','min:3', 'max:255', 'unique:users,' . $id]
...

but that can't find $id ofc as it is not passed to function.

First method complains about Undefined property: App\Http\Controllers\UserController::$user

I just don't know how to make this work

Oct
07
1 week ago
Activity icon

Replied to Method Destroy() Not Picking Up ID

I have models: Book, Owe and User. Controllers: BooksController, OwesController,UserController. Note that first two are plural, while UserController is not.

route:list show this line:

owes/{owe}       | owes.destroy     | App\Http\Controllers\[email protected]

and for the book:

books/{book}    | books.destroy    | App\Http\Controllers\[email protected]

Now tell me please why on books destroy method I have:

public function destroy(Book $book)

and on owe destroy method I have:

public function destroy($owe)

they are not the same. That confuses me, what are the rules for parameters when resource is used for controllers.

Oct
06
1 week ago
Activity icon

Replied to Method Destroy() Not Picking Up ID

I am aware of route:list, but still this is not clear to me.

E.g. my book destroy method looks like this:

public function destroy(Book $book)

while destroy for Owe looks like this:

public function destroy($owe)

and owe and book are not my model names.

Sorry for complicating this, but I am just trying to understand how this works, as it look like that I don't know. :(

Activity icon

Replied to Method Destroy() Not Picking Up ID

Ok, that worked! Could you tell me why, how? :)

Activity icon

Replied to Method Destroy() Not Picking Up ID

Hi geowrge, this is my web .php:

Route::resource('owes', 'OwesController');

so I am trying to use default approach like on other controllers and that is action="owes/{{$owe->oweId}}" but I don't know how to get this oweId on destroy method.

Activity icon

Replied to Method Destroy() Not Picking Up ID

Hey man, thx for answer. I believe that bracket was typo. At this stage (before submit) I have ID, e.g.:

/owes/12

but once in destroy method, I don't have it, it is null. I'll try once more.

For the user, the thing is, I don't want ID from the logged in user, but the ID from the user that this book is rented to. This is how I list rented books:

@foreach($owes as $owe)
    <div class="row owe-row">
        <div class="col-4 owe-row-content"> 
            {{ $owe->title }} - <i> {{ $owe->first_name }} {{$owe->last_name}}</i> 
        </div>
        <div class="col owe-row-button">
            <form method="POST" action="{{ route('owes.destroy', $owe->oweId) }}">
                @method('DELETE')
                @csrf
                <input type="hidden" name="member" value="{{ $owe->userId }}">
                <button type="submit" class="btn btn-primary">
                            {{ __('Return book') }}
                    </button>
            </form>
        </div>
    </div>                          
@endforeach 
Activity icon

Started a new conversation Method Destroy() Not Picking Up ID

So, I am working on something that should be like online books library. I have controllers Books and Users, and one as a relation between them, called OweController. I use CRUD on this controllers. When I navigate to Owes page I fill it like this:

public function index()
    {
        $owes = Owe::where('owes.active', '1')
                    ->leftjoin('books','owes.book_id', '=', 'books.id')
                    ->leftjoin('users','owes.user_id', '=', 'users.id')
                    ->select('owes.id as oweId', 'books.id as bookId','books.title','users.id as userId','users.first_name','users.last_name')
                    ->get();

        return view('owes.index', compact('owes'));
    }

then on index I have a form for book return:

<form method="POST" action="{{ route('owes.destroy', $owe->oweId) }}">
@method('DELETE')
@csrf
    <input type="hidden" name="member" value="{{ $owe->userId }}">  
        <button type="submit" class="btn btn-primary">
                {{ __('Return book') }}
            </button>
</form>

So question: if I use as above: route('owes.destroy', $owe->oweId) my destroy method picks this oweId as:

public function destroy($oweId)
{
        $owe = Owe::findOrFail((int)($oweId));
        $owe->update(['active' => false]);
    ...

but if I do it like on other controllers using: action="/owes/$owe->oweId) - this oweID is sent to destroy but not picked up from it. It is null. Of course my destroy is different in that case: destroy(Request $request, Owe $owes)

Second question is, can I send userId without using hidden in above form as I think that is not a proper way to do it.

So sorry for such a long post, but I wanted to provide as much info as possible.

Sep
15
1 month ago
Activity icon

Replied to Adding New Method To Controller

I just did. Is there something like close thread or mark as solved, or that is it?

Activity icon

Replied to Adding New Method To Controller

Ok, this worked! Thank you so much!

Activity icon

Replied to Adding New Method To Controller

Hi Nakov, thank you very much for detailed instruction. I just don't understand one thing:

Route::post('rent/{book}', '[email protected]')->name('rent.book');

this route assumes that I have rent page, right? But I want to do it from books.show.blade.php page where I have already book details:

@extends('layouts.app')

@section('content')
    
    <h1> {{ $book->title }} </h1>
    <h2> <i> {{ $book->author }} </i> </h2>
        
    <p>
        <a href = "/books/{{ $book->id }}/edit"> Edit </a>
    </p>

<p>Select user for book rental:</p>
<form action="/books/{{ $book->id }}" method="POST" accept-charset="utf-8">
    @method('PATCH')
    @csrf
    <select class="form-control" >
        <option value=""></option>
        
        @foreach($users as $user)   
            <option value="{{ $user->id }}">{{ $user->first_name }} {{ $user->last_name }}</option>
        @endforeach
    </select>
    <div class="field">     
        <div class="control">
            <button type="submit" class="button is-link">Rent</button>
        </div>
    </div>
</form>
@endsection

This is an ugly show page, but that is not the point atm.

Thanks

Activity icon

Started a new conversation Adding New Method To Controller

Hi all. This is my first post on this forum, and I am very new to laravel. What I am trying to do is this:

I am building something like books library. I have created BookController which has standard methods as: create, update, destroy, etc... and in web.php there is "Route::resource('books', 'BooksController');"

Now when I am on specific book page, I am listing my users from DB to whom I want to rent this book. I have form on this page, but I don't know where I should point it, maybe, action=/book/{$book-id}/rent? Then I would need rent page, which I don't want. I want to do a insert query user_id & book_id pair to DB, and go back to start page /books.

Can I add rent method to BookController and how to point to it from rent form? How to send book_id and user_id which I have on that page where form is?

Sorry for such long post.

Thanks, Slobodan