BezhanSalleh

Experience

57,990

39 Best Reply Awards

  • Member Since 2 Years Ago
  • 335 Lessons Completed
  • 1 Favorite

13th June, 2018

BezhanSalleh left a reply on How To Identify User When Sending A Data Via Socket • 1 week ago

i haven't used electron but i think this package might give an idea on how to implement your specific scenario.

https://www.npmjs.com/package/socketio-auth

just fyi PWAs might be easy then electron...just a thought

~cheers

BezhanSalleh left a reply on Laravel Update From Logged In User • 1 week ago

buddy your relationship are not properly defined. from what i can see users-locations relationship is m-m and then you a have created a model for the pivot table which is location_user... so in your

//User model
    public function locations()
    {
        return $this->belongsToMany(Location::class,'location_users')
                ->withPivot('departure_date');
    }

//Location model
    public function users()
    {
        return $this->belongsToMany(User::class,'location_users')
                ->withPivot('departure_date');
    }
//LocationUser model
    //you don't have to define these when you use belongsToMany
    //either use the 1-m relation between them and then define the inverse
    // but if you need them while using belongsToMany for some reason 
    //define them properly
    public function user() {
        return $this->belongsTo(User::class, 'id', 'user_id');
    }

    public function location() {
        return $this->belongsTo(Location::class, 'id', 'location_id');
    }
// now from what i see in your controller your are trying the 1-m approach so

public function edit(LocationUser $LocationUsers)
{
    // this way when the user clicks on a departure other his own
    // he/she will be redirected with Failure msg
    if(auth()->Id() != $locationUsers->user->id))
    {
            return redirect()->back()->withFailure('You are Not Authorized');   
    }
    return view('departure.edit', compact('LocationUsers'));    
    
}

public function update(Request $request, LocationUser $LocationUsers)
{   
    // to make it more secure here you could validate the requested
    // departure_date and since you have only one field its better to only request
    // that particular field instead given the user a chance to send malicious data
    $LocationUsers->update(['departure_date' => $request->input('departure_date')]);

    return redirect()->route('home', $LocationUsers)
        ->withSuccess('Departure updated!');
}

The above will solve your problem given you should check and correct your relationships and follow the laravel grammer. but the best way to do this would be to update your index so the logged in user should only see his/her own departure_dates only

//your index method for departure might look something like following i suppose

public function index()
{
    $LocationUsers = LocationUser::latest()->paginate(10);//or ->get();
    
    return view('departure.index',compact('LocationUsers'));
}

now updating the above as follow won't give the use a chance to see others departure dates

public function index()
{
    $locationUsers = LocationUser::where('user_id',auth()->id())
                        ->paginate(10);
    //or
    //$locationUsers = LocationUser::whereHas('user',function($user){
    //                              $user->whereId(auth()->id());
    //                          })->paginate(10);//->get();
    
    return view('departure.index',compact('LocationUsers'));
}

my advice would be to master the eloquent relationships first so you wouldn't have any problems in the feature... it might look a little bit confusing at first but once you get to know them its really fun. now i don't wanna go through the m-m situation which might confuse just search and i hope it helps. Happy Laraveling! ~cheers

BezhanSalleh left a reply on Laravel Update From Logged In User • 1 week ago

could you share your models for user and LocationUsers??? it seems while editing you are not checking whether the user is authorized or not;

in your edit you could do if(auth()->user()->locationUser == $LocationUser) { return... }else{ // return with unauthorized }

10th April, 2018

BezhanSalleh left a reply on Restful API In Laravel 5.6 • 2 months ago

welcome :)

BezhanSalleh left a reply on Why My Laravel Website Is So Slow? • 2 months ago

combine your assets into a single file... if you are using laravel-mix it will be a breeze.

BezhanSalleh left a reply on Property [name] Does Not Exist On This Collection Instance. • 2 months ago

because the relations is m2m, you can't just say $user->roles->name. try the following

<td>
    @foreach($user->roles as $role)
        {{$role->name.' - ' }} //or however you display them.
    @endforeach
</td>
~~

BezhanSalleh left a reply on Relations. Get Only One Item. • 2 months ago

that's because your relations are not properly defined. in your MagasineSubcategory update the products method as follow

    public function products()
    {
        return $this->belongsToMany('App\Product', 'product_has_subcategories', 'subcategory_id','product_id');
    }

BezhanSalleh left a reply on How To Pass Id From View To Route And From Route To Controller • 2 months ago

start with ~~~
    paste your code
end with ~~~

BezhanSalleh left a reply on Relations. Get Only One Item. • 2 months ago

what's your goal here? you haven't really cleared what it is that you want. you want one product from returned or Multiple? if its the latter then you need to update this line in your controller:

$produkty_subkategoria = MagazineSubcategory::with('products')
                            ->where('name', 'wiertarki')
                            ->get();
return $produkty_subkategoria;

7th April, 2018

BezhanSalleh left a reply on How Do You Change The Default Id That Gets Passed Into The Controller? • 2 months ago

@p0t4t0 you wanna use the route model binding, specifically, implicit route model binding... and you want username instead of the default id ...which is that method does. again read this... https://laravel.com/docs/5.5/routing#implicit-binding

//User.php

...
public function getRouteKeyName()
{
    return 'username';
}
...

Route::get('profile/{user}', '[email protected]');

public function(User $user) {
    //
    $user->username
}
// now you will be able to access
site.com/profile/potato

BezhanSalleh left a reply on How Do You Change The Default Id That Gets Passed Into The Controller? • 2 months ago

in your user model,

public function getRouteKeyName()
{
    return 'username';
}

in your controller

public function profile($username){
    $product = Product::whereUserName($username)->firstOrFail();
}

just refer to the docs for explanation https://laravel.com/docs/5.6/routing#customizeing-the-key-name

10th March, 2018

BezhanSalleh left a reply on Multiple And Advance Authorization With Different Tables • 3 months ago

checkout the article PovilasKorop wrote on laravel-news.com. all your questions will be answered. https://laravel-news.com/two-best-roles-permissions-packages

BezhanSalleh left a reply on Select Game With Screenshot Relationship But Only Where Screenshots Exist. Query Not Working • 3 months ago

@madsem accepted or not doesn't matter, all that matter is that it helped you get what you were looking for and i'm glad that it did. cheers mate!

8th March, 2018

BezhanSalleh left a reply on Eloquent Api Resource Upgrade To 5.6 • 3 months ago

@consigliere you are welcome!

BezhanSalleh left a reply on Select Game With Screenshot Relationship But Only Where Screenshots Exist. Query Not Working • 3 months ago

try this...

Game::whereHas([
                'screenshots' => function ($query) {
                    $query->where('size_label', '=', 't_screenshot_huge');
                }
            ])->where('hypes', '>', 1)->orderByRaw('RAND()')->take(3)->get();

BezhanSalleh left a reply on Eloquent Api Resource Upgrade To 5.6 • 3 months ago

as in the upgrade guide: The original property of resource responses is now set to the original model instead of a JSON string / array. This allows for easier inspection of the response's model during testing.

checkout the upgrade guide https://laravel.com/docs/5.6/upgrade

BezhanSalleh left a reply on User / Admin Auth In Laravel 5.2 • 3 months ago

@Ramnish this an old thread buddy... next time make a new thread and post your question there... anyways it depends on what kinda of functionality you want... you can give a look the following and chose whichever one best suits your need... keep in mind though some of them includes extra features that you might not need... ~ cheers mate! https://github.com/appzcoder/laravel-admin https://github.com/viralsolani/laravel-adminpanel http://laraadmin.com/ https://tutorials.kode-blog.com/laravel-5-admin-panel https://github.com/Labs64/laravel-boilerplate https://github.com/sebastienheyd/boilerplate

22nd February, 2018

BezhanSalleh left a reply on Combine Where And Has • 4 months ago

just a side note you CAN use whereHas too...

$user->organisation->records()->with('payments','checks.location')
->whereHas('checks',function($checks){
        $checks->where('yourConditionHere',$);
    })
->paginate();

but as @Snapey said, its not clear, you should post your models and a sample of what are you trying to achieve. ~cheers

9th February, 2018

BezhanSalleh left a reply on Dynamic Table Name • 4 months ago

yeah the controller should be what you said and yeah it will since you are changing the table for the model.

// Controller.php
$product = new Product;
$product->bind('mysql', $category->name.'_products' );

$products[] = $product->where('user_id',auth()->id())->get();

the newInstance method part is for when you want to perform any insert on the model after you dynamically change the table. for situations like

$product = new Product;
$product->getTable(); // products
$product->bind('mysql', $category_product);
$product->get(); // select * from $category_product
$product->first(); // select * from $category_product limit 1

// with the newInstance method you will be able to do the following.
$product->column = 'test; //$category_product->column
$product->save();
// if you just wanna perform selects then you can remove that part but if you later on need to change something leave it as it is.

8th February, 2018

BezhanSalleh left a reply on How Do You Send A First Time User A Password Reset Link? ( • 4 months ago

in this case on creation send an email with their username and system generated password and when a user is authenticated using their system generated password show he reset form.

BezhanSalleh left a reply on How Do You Send A First Time User A Password Reset Link? ( • 4 months ago

yeah, but if you are verifying the email then you send the user to the reset page and after reset to the login page. and you probably have a change password option for the authenticated user. for a first time user seems lots of hoop to jump over.

My way Verify->Authenticate->Reset by leverage the change password option if exist->boom!

BezhanSalleh left a reply on How Do You Send A First Time User A Password Reset Link? ( • 4 months ago

i would show the reset password for the new registered user after he/she is authenticated (or verified their email if verification is involved) for the first time and after that bussiness as usual.

BezhanSalleh left a reply on Dynamic Table Name • 4 months ago

// BindsDynamically.php
trait BindsDynamically
{
    protected $connection = null;
    protected $table = null;

    public function bind(string $connection, string $table)
    {
        $this->setConnection($connection);
        $this->setTable($table);
    }

    public function newInstance($attributes = [], $exists = false)
    {
        // Overridden in order to allow for late table binding.

        $model = parent::newInstance($attributes, $exists);
        $model->setTable($this->table);

        return $model;
    }

}
//Product.php 
namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{

    use BindsDynamically;

    public function users()
    {
        return $this->belongsTo( User::class );
    }

}
// YourController.php
        $categories = Category::orderBy( 'name', 'desc' )->get();

        foreach ($categories as $category)
        {
        $product = auth()->user()->products()->first();
            $product->getTable();
        $product->bind('connection', $category.'_products');
        //$product->get(); //select * from $category_products
            $products[] = auth()->user()->products()->get();
        }

this should work...

BezhanSalleh left a reply on Permissions Optimization In Blade • 4 months ago

You need to separate the logic for your modules or submodules that are accessible only by permission. Suppose in your example for you wanted your users to be able to view reports no matter what their roles are, given you want them to be able to view the reports based on the permission alone; you would need to implement the logic as follow:

<h1>Purchases</h1>
    <ul>
    //first checks the role and then the permission if has it allow if not you know
    @if( $user->isAn('role1', 'role2', 'etc'))

            @can('view-orders')
            <li>
                Icon - Orders
            </li>
        @endcan

            @can('view-suppliers')
            <li>
                Icon - Suppliers
            </li>
            @endcan
    @endif
    //here only checks the permission if has the permission allows it.
    @can('view-reports')
    <li>
        Icon - Reports
        <ul>
            <li>Report 1</li>
            <li>Report 2</li>
            <li>Report 3</li>
        </ul>
    </li>
    @endcan
</ul>

cheers mate!

31st January, 2018

BezhanSalleh left a reply on Why Laravel Error File Permission • 4 months ago

@deansatch you are right. but I didn't intend to do that..., he didn't really explain what he wanted, i thought he just wanted to delete his app. and given his os was windows so...

BezhanSalleh left a reply on Why Laravel Error File Permission • 4 months ago

he's on windows... :P

BezhanSalleh left a reply on Why Laravel Error File Permission • 4 months ago

@jaffar98 that's what you wanted. right?

BezhanSalleh left a reply on Why Laravel Error File Permission • 4 months ago

open command prompt as admin then then rd F:\xampp\htdocs\Fani-Server-Side /S /Q

BezhanSalleh left a reply on Laravel Searching • 4 months ago

@bobbybouwmann this one Eloquent Techniques: Dedicated Query String Filtering https://laracasts.com/series/eloquent-techniques/episodes/4

BezhanSalleh left a reply on Single Page App With Autocompletion And Browser Back Button! • 4 months ago

if you only need the back button then you can use a url helper url()->previouse().

<a href="{{ url()->previous() }}">Back</a>

BezhanSalleh left a reply on Is It A Good Idea To Have Controller Files For Admin User Stored In A Separate Files Called Admin. • 4 months ago

Since you have created the admin directory inside the Controllers directory you don't need to copy or move the base controller class. remove the Controller.php from your admin directory and your problem will be solved.

BezhanSalleh left a reply on Show In Blade • 4 months ago

having fun guys ha! .. you guys are terrible... @Dickyperdian

// update your code to
$data = [
            [
                "title"=>"PKS",
                "total"=>100
            ],
            [
                "title"=>"Gerinda",
                "total"=>30
            ]
        ];
$hasilSum = 0;
foreach($data as $value){
    $hasilSum += $value["total"];
}

return view('your_view_name', compact('data','hasilSum'));

// then in your_view_name.blade.php
    
@foreach($data as $value)
    {{"persentase partai ".$value["title"].": ".number_format(($value["total"]/$hasilSum)*100)."%"}} <br>
@endforeach

30th January, 2018

BezhanSalleh left a reply on Foreach Array • 4 months ago

post your could as I showed you earlier...

BezhanSalleh left a reply on Deploying Laravel On Shared Hosting Apache Server - Making Public Is The Root Document • 4 months ago

yes, since your domain points to the public folder for accessing your web app you need to amend the paths accordingly, otherwise it won't work.

checkout this http://blog.netgloo.com/2016/01/29/deploy-laravel-application-on-shared-hosting/ everything is explained here and the four methods you can deploy on shared hosting and what's best and why...

BezhanSalleh left a reply on Validation Is Not Working Within Foreach!! • 4 months ago

first where is your opening and closing form tag, your form method and route? post your route and add the the opening and closing form tag with the method and action which is the route or url your posting the data to.

not that it matters right now, you are sending null values. but once your route and form tags have been cleared then it would create a problem.

try this

//you should have a route
Route::post('/whatever/the/url', '[email protected]')->name('productAttributes.store');
//your views
{!! Form::open(['route' => 'productAttributes.store','method'=>'POST']) !!}
    @foreach($attrs as $attr)
        {!! Form::text('attrvalue_1', $attr->attribute_1, array('placeholder' => '','class' => 'form-control')) !!}
        {!! Form::text('attrvalue_2', $attr->attribute_2, array('placeholder' => '','class' => 'form-control')) !!}
        {!! Form::text('attrvalue_3', $attr->attribute_3, array('placeholder' => '','class' => 'form-control')) !!}
    @endforeach

{!! Form::close() !!}

i assume you are posting not updating...

BezhanSalleh left a reply on How To Validate Date After_or_equal To A Column • 4 months ago

if your condition is gte than unique is out of the question unless you combine some other column with it.

BezhanSalleh left a reply on Retrieve Object Throught Many Models • 4 months ago

no worries mate, happens to the best of us...

~cheers

BezhanSalleh left a reply on How To Validate Date After_or_equal To A Column • 4 months ago

if you are using Carbon then, that won't be a problem! the chances of creating two revisions for the same post at the same time is pretty slim, although it depends on the logic of how you handling the revision creation. If you allow creating revisions for the same post by many users then ok, but if you add some kind of logic that if someone is creating a revision for a post suppose post1 then make the next users wait till its done and that will take care of the date thingie.

but for your peace of mind you could do something as follow:

$post = Post::find(1);
//for post 1 get last revision's effective_date
$last_effective_date = $post->revisions()->select('effective_date')->latest()->first()->created_at;
//check to see if the last effective date for post one is greater than or equal to the current date
if(Carbon::now()->gte($last_effective_date))
{
    //if the condition passes create a new revision for the post 1
    Revision::create([
        'other_fields' => $someValue,
        'effective_date' => Carbon::now();
    ]);
}

29th January, 2018

BezhanSalleh left a reply on Need To Put These Requests In To An Array To Insert Multiple Rows In To A Table • 4 months ago

it does but only if you need to repeat them twice.

if not let me know.

BezhanSalleh left a reply on Insert Null Value With Query Builder • 4 months ago

if the default is set to null then ($test) ? $test : NULL; will work fine.

Edit Your Profile
Update

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