How to open link when add new group? laravel - laravel

I have two records.
When I click on +. Must open the desired group. But I see it that way. That is, it opens both groups.
slider_gorups table
Schema::create('slider_groups', function (Blueprint $table) {
$table->id();
$table->string('title')->nullable();
$table->timestamps();
});
sliders table
Schema::create('sliders', function (Blueprint $table) {
$table->id();
$table->foreignId('userId')->constrained('users')->cascadeOnDelete();
$table->unsignedBigInteger('groupId')->nullable();
$table->string('src')->nullable();
$table->timestamps();
});
groups.blade.php
<a href="{{ route('admin::sliders.index', $sliderGroup->id) }}" class="mr-1">
<i class="la la-plus text-grey text-shadow-custom font-medium-4 font-weight-normal"></i>
</a>
web.php
Route::resource('slider-groups', 'Admin\SliderGroupController');
Route::prefix('slider-groups/{sliderGroupId}')->group(function(){
Route::resource('sliders', 'Admin\SliderController');
});

put your groups code
i think problem is in their id attribute
each group must has its own id and same with its own "+" id!
in this case when you click on "+", it will open group with same id

Related

Order by activity date instead of users followed

I have two tables, the activities table and followings table. I want to get "following" activity the date it was created instead of by the date the user was created.
Schema::create('activities', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id')->index();
$table->integer('activity_id')->index();
$table->string('activity_type', 50);
$table->timestamps();
$table->index(['activity_id', 'activity_type']);
});
Schema::create('followings', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id');
$table->unsignedBigInteger('follower_id');
$table->dateTime('followed_date')->nullable();
$table->dateTime('unfollowed_date')->nullable();
$table->timestamps();
$table->unique(['user_id', 'follower_id']);
});
Right now I have this code:
Controller:
$followings = $user->following()->get();
View:
#foreach($followings->sortByDesc('id') as $following)
#foreach($following->user->activity->sortByDesc('id') as $activity)
#if($activity->activity_type == 'App\Thread')
<li class="activity">
<div class="activity-user">
<p class="avatar" style="background-color: {{ $activity->user->color }}">
{{ substr($activity->user->name, 0, 1) }}</p>
</div>
<div class="activity-body">
<h4>{{ $activity->user->name }}
published <a
href="/forums/{{ $activity->activity->channel->slug }}/{{ $activity->activity->slug }}/">
{{ $activity->activity->title }}</a></h4>
<p>
{!! BBCode::parse($activity->activity->body) !!}
</p>
<p class="created-at">
{{-- {{ $activity->activity->created_at->format('l') }}
at
{{ $activity->activity->created_at->format('h:i A') }} --}}
{{ $activity->activity->created_at->format('M j, Y') }}
</p>
</div>
</li>
#endif
#endforeach
#endforeach
Model:
public function following()
{
return $this->hasMany(Following::class);
}
Is there anyway for me to reach the polymorphic table and order by the Activity date instead of by the following() date?
Right now, it returns records based on the user's name in the date they posted it, but I would like it strictly by the activity.
For your scenario, you will be better of with a custom DB query like this, I haven't tested it so you will need to adjust for your use case but basic concept for Laravel is pretty much the same.
$following = Following::join('users', 'followings.user_id', '=','users.id')
->join('activites', 'users.id', '=', 'activities.user_id')
->orderBy('activities.created_at')
->select('followings.*')
->get();
Here's how I did it:
Controller
$followings = $user->following()->get();
foreach($followings as $following) {
foreach($following->user->activity as $activity) {
$followingArray[] = $activity->id;
}
}
//dd($followingArray);
if(!empty($followingArray)){
$following = Activity::whereIn('id', $followingArray)->orderBy('created_at', 'desc')->get();
} else {
$following = '';
}
View:
#if(!empty($following))
#foreach($following as $activity)

Relationships in laravel 5.4

when the admin select a category in <select></select> i want to get its id for insert it in a question table
table questions:
Schema::create('questions', function (Blueprint $table) {
$table->increments('id');
$table->string('question_text');
$table->string('type');
$table->integer('points');
$table->integer('temps_reponse');
$table->integer('categories_id')->unsigned();
$table->foreign('categories_id')->references('id')->on('categories');
$table->timestamps();
});
table categorie :
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('categorie');
$table->timestamps();
});
Question Model
class Question extends Model
{
public function categorie()
{
return $this->belongsTo(Categorie::class, 'categories_id');
}
}
Categorie Model
class Categorie extends Model
{
public function question()
{
return $this->hasMany(Question::class, 'question_id')->withTrashed();
}
}
I am blocked with it for a few hours please any help to solve it
my html
<label class="form-label">Choisir la categorie :</label>
<select class="" id="s2example-1" name="categorie">
<option></option>
#foreach ($categories as $categorie)
<option>{{ $categorie->categorie }}</option>
#endforeach
</select>
In order to send the id instead of the string back to the server, supply the value attribute on each option element: <option value={{$categorie->id}}>{{ $categorie->categorie }}</option>

Save one to many without creating

I just have a little problem. I have a one to many relationship. A team has many users. Many users have a team.
I have six teams of which the user has to choose one. How can I connect the team with the user without creating a new entry in the database 'team'?
User:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id')->unique();
$table->string('name');
$table->integer('team_id')->unsigned()->nullable();
$table->foreign('team_id')->references('id')->on('users')->onDelete('cascade');
$table->string('username')->nullable()->unique();
$table->string('email')->unique();
$table->string('phone')->nullable()->unique();
$table->string('avatar');
$table->string('slug');
$table->string('password');
$table->date('birthday');
$table->boolean('gender');
$table->rememberToken();
$table->timestamps();
});
}
Team:
public function up()
{
Schema::create('teams', function (Blueprint $table) {
$table->increments('id')->unique();
$table->string('name');
$table->string('image');
$table->string('image_thumbnail');
$table->timestamps();
});
}
Controller:
public function storeTeam($request)
{
$user = Auth::user();
$team = Team::findOrFail($request->team_id);
$user->team()->associate($team);
return redirect()->back()->with('success', lang::get('messages.team'));
}
View:
<div class="col-md-12">
<form method="POST" action="{{ URL::route('store.team') }}">
{{ csrf_field() }}
<ul class="row clients-dotted list-inline text-center">
#foreach ($teams as $team)
<li class="col-md-4 col-sm-3 col-6">
<div class="cc-selector">
<input id="team{{$team->id}}" type="radio" name="team" value="{{$team->id}}" />
<label style="background-image:url({{ $team->image_thumbnail }});" class="team-cc" for="team{{$team->id}}"></label>
<p>{{$team->name}}</p>
</div>
</li>
#endforeach
</ul>
<button type="submit" class="mb-50 float-right mr-20 btn btn-shadow-1 btn-primary">weiter</button>
</form>
route:
Route::post('/welcome/team', 'ProfilesController#storeTeam')->name('store.team');
i have a little problem with this and get the error:
Type error:
Type error: Too few arguments to function App\Http\Controllers\ProfilesController::storeTeam(), 0 passed and exactly 1 expected
Assuming that you're storing the team_id on the user model, you can use the associate method to set the primary key on the child -- user in this case
$user->team()->associate($team);
You would pass the selected team to the controller. Doesn't matter how. You can pass it as part of a route, or you can pass the team ID from a form and get it from the request.
You're expecting a variable to literally be passed to the controller called $request. What you want is to inject $request as a dependency. You can do that by typehinting the resolved variable like so:
public function storeTeam(Request $request)
Mind you, you'll need to have use Illuminate\Http\Request; at the top of your file, otherwise you will get errors regarding the Request class.

create authentication laravel 5

I have these 2 tables with many to many relationship connected using a junction table. The idea is that I can get the user data to make the user an author in a journal data and it works so far.
User table :
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->integer('phone')->nullable();
$table->string('address')->nullable();
$table->string('password');
$table->rememberToken();
$table->enum('level', ['admin', 'author']);
$table->timestamps();
});
}
Journal table :
public function up()
{
Schema::create('journal', function (Blueprint $table) {
$table->increments('id');
$table->string('title', 255);
$table->text('abstract');
$table->text('file');
$table->integer('id_edition')->unsigned();
$table->timestamps();
});
}
Junction table :
public function up()
{
Schema::create('penulis', function (Blueprint $table) {
// Create tabel penulis
$table->integer('id_user')->unsigned()->index();
$table->integer('id_journal')->unsigned()->index();
$table->timestamps();
// Set PK
$table->primary(['id_user', 'id_journal']);
// Set FK penulis --- user
$table->foreign('id_user')
->references('id')
->on('users')
->onDelete('cascade')
->onUpdate('cascade');
// Set FK penulis --- journal
$table->foreign('id_journal')
->references('id')
->on('journal')
->onDelete('cascade')
->onUpdate('cascade');
});
}
Now I have this view that shows journals data along with the buttons to edit or delete it. What I want to make is that only the user that are entitled as the author of the journal that has the capacity to access these buttons. How do I make it ? below is the view code :
<tbody>
<?php foreach ($journal_list as $journal): ?>
<tr>
<td style="">{{ $journal->title }}</td>
#if (Auth::check())
<td style="width: 130px; overflow: hidden;">
<div class="box-button">
{{ link_to('journal/' . $journal->id . '/edit?edition=' . $edition->id, 'Edit', ['class' => 'btn btn-warning btn-sm']) }}
</div>
<div class="box-button">
{!! Form::open(['method' => 'DELETE', 'action' => ['JournalController#destroy', $journal->id]]) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger btn-sm']) !!}
{!! Form::close() !!}
</div>
</td>
#endif
</tr>
<?php endforeach ?>
</tbody>
Sorry for my bad English and if my question is stupid. Thanks!
You need to use a combination of middleware and Gate facade.
Generate a policy
Write a policy
Like this:
public function edit-journal(User $user, Journal $journal)
{
return $user->id === $journal->user_id;
}
public function delete-journal(User $user, Journal $journal)
{
return $user->id === $journal->user_id;
}
3. You can now use the Gate facade with blade
Like this:
#can('edit-journal', $journal)
<div class="box-button">
{{ link_to('journal/' . $journal->id . '/edit?edition=' . $edition->id, 'Edit', ['class' => 'btn btn-warning btn-sm']) }}
</div>
#endcan
#can('delete-journal', $journal)
<div class="box-button">
{!! Form::open(['method' => 'DELETE', 'action' => ['JournalController#destroy', $journal->id]]) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger btn-sm']) !!}
{!! Form::close() !!}
</div>
#endcan
You will have to register a middleware for your edit and delete route. Your routes should look like:
//Routes
Route::get('journal/' . {$journal_id} . '/edit', ['as'=>'editJournal','middleware' => 'journal:edit', 'uses'=>'JournalController#edit']
//You need to change your delete form so the action points to that route
Route::delete('journal/' . {$journal_id}, ['as'=>'deleteJournal','middleware' => 'journal:delete', 'uses'=>'JournalController#destroy']
In your middleware, you should have something like:
//Journal Middleware
public function handle($request, Closure $next, $role)
{
$parameters = $request->route()->parameters();
$journal = Journal::findOrFail($parameters['journal_id']);
if (Gate::allows($role.'-journal', $journal)) {
return $next($request);
}else{
abort(403, "You do not have the permission to ".$role." this journal")
}
}
What I;ve done in similar cases is to add a function inside the model that you want to check with all the logic. So for example in your case would be something like:
/Model/Journal.php
public function canBeModifiedByUser($user_id){
//Check all the things that you want
}
Then in the view you can do something like:
if($journal->canBeModifiedByUser($journal->user->id))
Also I would suggest you to check some ACL packages, it might be an overkill for you atm but it might just be what you need.
I would suggest using gates
in your auth service provider you can do
$gate->define('can-modifiy', function ($user) {
// whatever code you want to determine if the user can eg
return $user->hasRole('admin');
});
then in your views you can use #can
#can ('can-modify')
<button>delete</button>
#endcan
This can also be used in your controllers with
$this->authorize('can-modify');
or
Gate::allows('can-modify');
This is in the docs at https://laravel.com/docs/5.3/authorization#writing-gates

How to retrieve data from a form ( type = "text" ) and store in database in laravel 5.2

I want to retrieve data for a user's comment using a form but I'm not too sure how to do that even after looking at the laravel documentation and videos.
my "CommentsController" code snipit looks like this
public function submitComment(Request $request){
$this->validate($request, [
'comment'=> 'required|max:500'
]);
$comment= new Comments;
$comment->comments=$request->input->('comment');
DB::table('comments')->insert(
array('user_id'=>1,
'post_id'=>1,
'comment'=> $comment)
);
, my form snipit looks like this
<?php
echo '<form method="POST" action="comments"> ';
echo '<input name="comment" type="text" cols="40" rows="5" style="width:200px; height:50px;" placeholder="Type Here">';
echo '<input type="submit">' ;
echo '</form>';
?>
and my table in the database looks like this
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('post_id')->unsigned();
$table->string('comments');
$table->timestamps();
//Foreign Keys
$table->foreign('post_id')
->references('id')->on('posts')
->onDelete('cascade');
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
});
Try changing
$comment->comments=$request->input->('comment');
to
$comment->comments =$request->comment;
Basically you can get a input by $request->formName
I also tent to create new records like
$record = Comment::create([ 'user_id' => $userId, 'post_id' => $postId, 'comments' => $request->comments]);
But you would need to create a model to do so like that.
php artisan make:model Comment

Resources