vendredi 27 septembre 2019

laravel: show both a particular post based on id and featured posts in one page

I have one post information page. posts/show.blade.php.

In this page,

I have one post information field and featured posts field.

In my controller, I have already put following code to show one particular post based on ID.

ResultsController.php

public function show($id,Post $post)
    {
        $post= Post::find($id);
        $post->category;
        $post ->tags;
        return view('posts.show',compact('post'));
    }

But also, I need to show featured events cards bottom of the same page(show.blade.php). I am thinking to add this code in my controller.

$posts = Post::latest()->limit(6)->get();
$categories = Category::latest()->limit(6)->get();

Because I want to show 6 featured posts.

So the question is how to show one particular post and the featured posts in one page.

I am glad if someone helps me out.

web.php

Route::get('results/{id}', 'ResultsController@show')->name('posts.show');

show.blade.php

//one of the cards for featured posts

 <div class="image">
                    <img src="../image/image2.jpg" alt="" width="300px" height="200px">
                </div>
                <div class="card-information">
                        <div class="event-name">
                            lorem
                        </div>
                        <div class="heart">
                            <i class="fas fa-heart fa-lg" style="color: #F70661"></i>
                        </div>

                    <div class="event-date">
                        2019.8.23
                    </div>
                    <div class="card-info">
                        <p>Lorem ipsum dolor sit amet in Lorem, ipsum dolor
                            <a href="#" style="color: white">...see more</a>
                        </p>
                    </div>
                </div>    


via Chebli Mohamed

Undefined variable: tags, display data from database on bootstrap modal

I have simple CRUD app , in which I want to display tags in bootstrap pop up modal.

Here is what I have tried so far.

Tags controller

class TagController extends Controller
{


    public function index()
    {
        $tags =Tag::all();
        return view('pages.index')->withTags($tags);
    }


    public function store(Request $request)
    {
        $this->validate($reguest, array('name'=>'required|max:255'));
        $tag = new Tag;
        $tag->name = $request->name;
        $tag->save();
        $request->session()->flash('succes', 'Tag was successful created!');
        return redirect()->route('pages.index');
    }


}

Now I want to display these tags to a view associated with another controller PageListController

Here are view pages.index

@extends('layouts.app', ['activePage' => 'table', 'titlePage' => __('Table List')])

@section('content')
<div class="card-body">
    <div class="table-responsive">
      <table class="table">
        <thead class=" text-primary">
          <th>
            ID
          </th>
          <th>
            Page Name
          </th>
          <th>
          Articles
          </th>
          <th>
            Tags
          </th>

        </thead>
        <tbody>
          @foreach ($pages as $page)

          <tr>
              <td>
                
              </td>
              <td>
                
              </td>
              <td>
                
              </td>
              <td>
                    <button type="button" class="btn btn-sm btn-success" data-toggle="modal" data-target="#tagsModel">
                            
                          </button>

                              <!-- Modal -->
                              <div class="modal fade" id="#tagsModel" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                <div class="modal-dialog" role="document">
                                  <div class="modal-content">
                                    <div class="modal-header">
                                      <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                                      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                        <span aria-hidden="true">&times;</span>
                                      </button>
                                    </div>
                                    <div class="modal-body">
                                        <table class="table">
                                            <thead class=" text-primary">
                                              <th>
                                                ID
                                              </th>
                                              <th>
                                               name
                                              </th>
                                            </thead>
                                            <tbody>
                                                @foreach ($tags as $tag)   
                                                <tr>
                                                    <td>
                                                        
                                                    </td>
                                                    <td>
                                                       
                                                    </td>
                                                </tr>
                                                @endforeach

                                            </tbody>
                                          </table>
                                    </div>
                                    <div class="modal-footer">
                                      <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                      <button type="button" class="btn btn-primary">Save changes</button>
                                    </div>
                                  </div>
                                </div>
                              </div>
                </div>
              </td>

          </tr>
          @endforeach
        </tbody>
      </table>
    </div>
  </div>
@endsection

Here is my routes file web.php

<?php

Route::get('/', function () {
    return view('welcome');
});
Auth::routes();

Route::get('/home', 'HomeController@index')->name('home')->middleware('auth');
Route::resource('pages', 'PageListController');
Route::resource('pages', 'TagController');


Route::group(['middleware' => 'auth'], function () {
    Route::get('table-list', function () {
        return view('pages.index');
    })->name('table');
});

Route::group(['middleware' => 'auth'], function () {
    Route::resource('user', 'UserController', ['except' => ['show']]);
    Route::get('profile', ['as' => 'profile.edit', 'uses' => 'ProfileController@edit']);
    Route::put('profile', ['as' => 'profile.update', 'uses' => 'ProfileController@update']);
    Route::put('profile/password', ['as' => 'profile.password', 'uses' => 'ProfileController@password']);
});

I just want to display tags using bootstrap modal pop up.

Now when I run my app I get the following error

Undefined variable: pages (View: C:\custom-xammp\htdocs\royalad-dashboard\resources\views\pages\index.blade.php)

What am I doing wrong in my code? any help or suggestions will be appreciated



via Chebli Mohamed

Laravel cannot set id on eloquent creating event

I'm trying to give my modal a custom id (no auto increment). So I've overwrite the boot method of my modal. The creating event is used like this:

public static function boot()
{
    static::creating(function ($modal) {
        $modal->id = $myID;
        return true;
    });
}

Now when I try to revert the id after saving an entry the id of the new entry is alwas null.

$modal = new Modal;
$modal->myValue = $myValue;
$modal->save();

dd($modal->id) // This will returns always null

The strange thing is that the record is successful written to the database with the right id.

What is wrong with my code?



via Chebli Mohamed

Multiple throttling rules for Laravel 5.2

I want to have multiple throttling rules (10 requests in 1 Minutes, 30 Requests in 60 Minutes). Is it possible to do so in Laravel 5.2



via Chebli Mohamed

Fetching data into routes.php

I am new at laravel.I have a table with contains the navigation titles. I want to fetch all the title's from the table into routes.php

Is it Possible to do that.



via Chebli Mohamed

Confirm form resubmission Laravel

I am redirecting users based on their roles, I changed my AuthenticatedUsers.php and added this to the authenticated function

protected function authenticated(Request $request, $user)
{
    if (Auth::user()->priority == 'HI') {
        return view ('dashboard');
    }else{
        return view ('home');
    }
}

Now I got it working to redirect based on roles, however when I refresh the page FOR THE FIRST TIME it shows

CONFIRM FORM RESUBMISSION



via Chebli Mohamed

Need to setup a coins system in laravel?

My requirement is like as follows:

1) User signup on my website gets 10 coins.

2) Creates a post and based on likes and comments , we can add into his wallet / coins history from admin like 10 more coins (SO total 10 coins on signup + 10 coins on creating a post based on likes and comments e.g 10 likes and 5 comments) from admin side.

3) If he invites other members gets 10 more coins, if other user register on his invite code, also gets 10 coins which is default (Automated 1st point mentioned).

4) There should be page where he can see history of coins credited with note.

5) Redeem of coins will be only when he reaches 100 coins. We will discuss this further.

Note: laravel developers are most welcome who can create this system.



via Chebli Mohamed