Liam Savage Liam Savage - 9 months ago 52
MySQL Question

Blade Query with multiple where and orWhere clause matching certain id

I have a laravel project that I want to check if the user either owners or rents a property from a MySQLDB, I have a rewrite rule that allows them to visit a page and edit this property

Route::get('/housing/manage/{id}', function() {
return view('pages.housing_management.housing_management');

Now, I have this query that checks if they either own or rent it, but thats to check if theres any properties that match that rule, is where a way I can make sure that they own or rent the propertie with id = {id} that is sent with the page?

@if (count(Properties::where('owner', '=', Auth::user()->id)->orWhere('rented_by', '=', Auth::user()->id)->orderBy('id', 'DESC')->limit(1)->get()) > 0)
<p>Authentication Passed, you either own or rent this property.</p>

Answer Source

Your query can be written in routes.php file as:

Route::get('/housing/manage/{id}', function($id) {
        $exists = Properties::where('id', '=', $id)
                            ->where(function($q) {
                                $q->where('owner', '=', Auth::user()->id)
                                    ->orWhere('rented_by', '=', Auth::user()->id);
        return view('pages.housing_management.housing_management', compact('exists'));

And in your blade file, you can write as:

@if ($exists)
<p>Authentication Passed, you either own or rent this property.</p>