Raywachaga

Experience

3,070

0 Best Reply Awards

  • Member Since 6 Months Ago
  • 27 Lessons Completed
  • 5 Favorites

21st March, 2017

Raywachaga started a new conversation Count Duplicate Columns In Laravel • 3 months ago

Hi guys,

How do you count the duplicate columns in a database using eloquent model querying?

For example, in SQL, there's this command:

SELECT name, COUNT(email) 
FROM users
GROUP BY email 
HAVING ( COUNT(email) > 1 )

The code above will return the names from all columns with similar email addresses.

What I'm trying to achieve is to get the count of duplicate entries, i.e.

$noOfjobsFromSameCustomers = App\Workflow::count();

But I need an additional parameter to specify that the cust_id (a column in the workflows table) is duplicated, something like

$noOfjobsFromSameCustomers = App\Workflow::notDistinct()->orderBy('cust_id')->count();

9th March, 2017

Raywachaga left a reply on Exclude A Part From Master Layout • 3 months ago

Hi GEORGEK,

Did you find a solution for this? I'm at a crossroads, just like you were when you posted this.

5th March, 2017

Raywachaga left a reply on How To Change Timezone In App.php • 3 months ago

Thanks for this thread Mustafa. Changing the timezone saves you a lot of unnecessary code to achieve a simple thing.

14th January, 2017

Raywachaga left a reply on Laravel 5 Validation Request, How To Handle Validation On Update? • 5 months ago

Whoa! This is really handy especially for updating user accounts! Thanks guys.

Raywachaga left a reply on No Delete Method For Link + Delete Method For Form Button • 5 months ago

Thanks for your input guys. Turns out this code was just fine. The only blunder I made was "wrap" this form within another form in a vain attempt to style my form buttons. I should have just used the good old div tag! I think as a result DOM couldn't execute the hidden delete method even though it could read it.

10th January, 2017

Raywachaga started a new conversation No Delete Method For Link + Delete Method For Form Button • 5 months ago

Hi guys,

How can I get my button below to work and call my "deleteAccount" function below:

{{ Form::open(array('action' => [email protected]')) }}

{{ method_field('delete') }}

<button class="tiny alert button" id = "accDel">Delete Account!</button>

{{ Form::close() }}

This works with the link tag but I can't specify the delete method with the link tag which ends up calling the show method instead that uses the link tag's default get method

<a href="myaccount/deleteAccount">Delete Account!</a>

It must be slight syntax error I figure but I'm open to suggestions.

22nd December, 2016

Raywachaga left a reply on Grouping Data For View Form- LOOPS, IF • 6 months ago

This is how I did it:

I set up my Controller (index function) as follows

public function index(){

    $categories=User_service::distinct()->select("service_category")-   >orderBy("service_category")->get();

    $servicesList=User_service::select("service_category",  "service_name")->latest("updated_at")->get();

    return view ("services.index", compact("categories", "servicesList"));


}

I then setup my index view (blade) as follows:

@extends("app")

@section("content")

<h1>Services available</h1>

<hr/>

@foreach($categories as $category)     

<article>

<h3>

CATEGORY:  {{$category->service_category}} 

</h3>

@foreach($servicesList as $list)

@if($list->service_category==$category->service_category)

<h4>

{{ $list->service_name }}

</h4>

@endif

@endforeach

</article>

@endforeach

@stop

20th December, 2016

Raywachaga started a new conversation Grouping Data For View Form- LOOPS, IF • 6 months ago

How would I go about setting up my controller/view so that all the services under a particular category are grouped together? (A SINGLE DATABASE TABLE called USER_SERVICES)

That is, represent the view the below...

CATEGORY: Building-cleaning services and property management services -We manage properties

CATEGORY: Air transport services of passengers and freight, except transport of mail -Take a flight

CATEGORY: Computer and related services -Server maintenance

CATEGORY: Computer and related services -Web Development

As (notice the last 2 services grouped together):

CATEGORY: Building-cleaning services and property management services -We manage properties

CATEGORY: Air transport services of passengers and freight, except transport of mail -Take a flight

CATEGORY: Computer and related services -Server maintenance -Web Development

My controller is setup as follows:

    public function index(){

    $user_services=User_service::latest("updated_at")->get();

    return view ("services.index", compact("user_services"));

    }

My view (index blade) is as follows:

@extends("app")

@section("content")

<h1>Services available</h1>

<hr/>

@foreach($user_services as $user_service)     

<article>

<h3>

CATEGORY:  {{ $user_service->service_category }}

</h3>

<!-- To see how to categorize this listing -->

<h4>

{{ $user_service->service_name }}

</h4>

</article>

@endforeach

@stop

I've tinkered around with the foreach loop and if function but to no avail so far.

18th December, 2016

Raywachaga left a reply on Middleware • 6 months ago

Thanks for pointing me in the right direction guys.

Here's how I did it.

public function handle($request, Closure $next)
{

    $service_URLlid = $request->segment(2);

    $user_service = User_service::findOrFail($service_URLlid);

    $serviceId=$user_service->user_id;
    
    if (\Auth::user()->email == $serviceId) {

    return $next($request); 
    }

    return redirect()->to('/home');
    
}

I had to pull in my eloquent model to use eloquence within the middleware though:

use App\User_service;

Raywachaga left a reply on Middleware • 6 months ago

True @Snapey @craigpaul, using dd() I've indeed seen there's no input

So how can I prevent a user from editing another user's service? They shouldn't even be able to open the edit form.

Raywachaga left a reply on Middleware • 6 months ago

@craigpaul Hmm. That dd() is pretty handy. I remember trying to know which part of the logic was glitchy but didn't know how.

dd(\Auth::user());

User {#217 ▼ #fillable: array:7 [▼ 0 => "name" 1 => "email" 2 => "password" 3 => "phone_number" 4 => "address" 5 => "county" 6 => "town"

dd($request->user_id);

null

//The problem lies with fetching the user_id from the request

Raywachaga left a reply on Middleware • 6 months ago

@craigpaul That didn't work either :(

@DmytroOlefyrenko Laravel allows us to define the routes in the controller as I did above. But all the same, I followed the "long way" and still got the same result.

17th December, 2016

Raywachaga started a new conversation Middleware • 6 months ago

Hi guys,

Why does my middleware below keep skipping the "return $next($request);" bit

public function handle($request, Closure $next)
{
    
    if (\Auth::user() == $request->user_id) {

        return $next($request); 
    }

    return redirect()->to('/home');
    
}

My controller has a constructor as follows:

public function __construct(){             

    $this->middleware("auth", ["only"=>["create","store"]]);

    $this->middleware(["auth.poster"], ["only"=>["edit","update","destroy"]]);

}

I've also registered the middleware under the kernel $routeMiddleware:

'auth.poster' => \App\Http\Middleware\PostsProtection::class,

16th December, 2016

Raywachaga left a reply on Nested Resources- Controllers & Views • 6 months ago

As promised:

class ReviewsController extends Controller
{
/*For this controller apply this middleware; meaning all the "sub-routes"      under ServicesController will 
only work for authorized, signed-in users. Note that the __construct is made up of two underscores*/

public function __construct(){             

    $this->middleware("auth", ["only"=>["create","edit"]]);

}


//INDEX METHOD IS COMPLETE

public function index($serviceId){                                 

    $service_reviews=Service_review::latest("updated_at")->where("user_service_id", $serviceId)->get();

    return view ("reviews.index", compact("service_reviews", "$serviceId"));

}



//SHOW METHOD IS COMPLETE

public function show($serviceId, $reviewId){

    $service_review = Service_review::findOrFail($reviewId);

    return view ("reviews.show")

    ->withService_review($service_review)
    ->withUser_serviceId($serviceId);

}


//CREATE METHOD IS COMPLETE

public function create($id){                                              

    $user_service = User_service::findOrFail($id);
    
    return view ("reviews.create")->withUser_service($user_service);

}



//STORE METHOD IS COMPLETE

public function store(ReviewsRequest $request, $id){                      

    $service_review = new Service_review($request->all());

    $user_service = User_service::findOrFail($id);

    $service_review->user_id = \Auth::user()->email;

    $user_service->service_reviews()->save($service_review);

    flash()->success("Your service review has been posted");

    return redirect ("services/$id/reviews");

}



//EDIT METHOD IS COMPLETE

public function edit($serviceId, $reviewId){                                  

    $service_review = Service_review::findOrFail($reviewId);

    return view("reviews.edit")
    ->withService_review($service_review)
    ->withUser_serviceId($serviceId);


} 



//UPDATE METHOD IS COMPLETE

public function update($serviceId, $reviewId, ReviewsRequest $request){       


    $service_review = Service_review::findOrFail($reviewId);

    $service_review->update($request->all());

    flash()->success("Your service review has been updated");

    return redirect ("services/$serviceId/reviews");

} 



//DESTROY METHOD IS COMPLETE

public function destroy($serviceId, $reviewId){                                

    $service_review = Service_review::findOrFail($reviewId)->delete();

    flash()->success("Your service review has been removed");

    return redirect ("services/$serviceId/reviews");

}  

}

In hindsight though, I now realize that once you setup your models correctly you won't have much of a challenge pulling data from your database in all manner of imaginable ways. The main oversight on my part was that my foreign keys were not setup in the traditional (table1_id<->table2_id) format so after checking the documentation I found this gem:

//in your model

return $this->hasOne('App\Phone', 'foreign_key', 'local_key');

The above let's you specify your foreign-local key relations manually and finally unlock the power of eloquence. With this the controller above can be simplified to fewer lines of code and so can it's respective views.

1st December, 2016

Raywachaga left a reply on Nested Resources- Controllers & Views • 6 months ago

We're getting warmer here.

I also subscribed to Treehouse and got these two videos:

https://teamtreehouse.com/library/laravel-4-basics/relating-data/model-relation

https://teamtreehouse.com/library/nested-crud-with-laravel-4/nested-routes-controllers/nested-routes

I'll be preoccupied with exams for a couple of weeks but I'll be sure to report back soon. I'm certain your suggestion @schmidtke182 coupled with these two will allow me to build a solution.

29th November, 2016

Raywachaga left a reply on Nested Resources- Controllers & Views • 6 months ago

Hi @schmidtke182,

I already came by that page. In fact you'll see that I commented on that link. What I need is an actual detailed video with explanations like Jeff does. I think there must be one on here even if it's for Laravel 4, I can always implement it in 5.3 somehow. For what it's worth though, I'll try and slowly go through the link again and see if I can implement that on my project.

Raywachaga started a new conversation Nested Resources- Controllers & Views • 6 months ago

Hi guys,

Is there a detailed video here on Laracasts on how to setup the Controllers as well as the CRUD Views (create, edit, index, show) for a nested resource like the one below?

Route::resource("services.reviews", "ReviewsController");

I already have a ServicesController as below:

Route::resource("services", "ServicesController"); //Works perfectly

So the ReviewsController gets the serviceId for use within it's methods.

I saw a video on Treehouse (link below) that seems to explore this but I figured I'd get more coverage here since it's a "more official" tutorial point for Laravel.

P.S. I signed-up for Laracasts not Treehouse.

https://teamtreehouse.com/library/nested-crud-with-laravel-4/nested-routes-controllers/nested-routes

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.