how to do this in Laravel 5.2 - php

I have 2 public function in My Controller
public function index()
{
$projects = Project::personal()->get();
return view('projects.index')->withProject($projects);
}
other one is
public function show($id) {
$project = Project::find($id);
$tasks = $this->getTasks($id);
$files = $this->getFiles($id);
$comments = $this->getComments($id);
$collaborators = $this->getCollaborators($id);
return view('projects.show')->withProject($project)->withTasks($tasks)->withFiles($files)->withComments($comments)->withCollaborators($collaborators);}
I need get
$collaborators = $this->getCollaborators($id);
to My public function index() method to print collaborators in ('projects.index') view
how can do this?

Update your index function with this code, haven't tested it
public function index()
{
$projects = Project::personal()->get();
foreach($projects as $key => $project) {
$find = Project::find($project->id);
$tasks = $this->getTasks($project->id);
$files = $this->getFiles($project->id);
$comments = $this->getComments($project->id);
$collaborators = $this->getCollaborators($project->id);
$projects[$key]['collaborators'] = $collaborators;
}
return view('projects.index')->withProject($projects);
}
In ('projects.index') you will have collaborators value for each database record

Related

Finiding the error in my function Parse error: syntax error, unexpected '}' [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
Hi everyone i can't find the error in my function store():
This is the following code:
public function store(Request $request) {
$post= new Post();
$post->title = $request->input('title');
if (Auth::check()) {
$postPhoto = Auth::user()->photo;
$emailpost = Auth::user()->email;
} else {
$postPhoto = public_path('/images/def.jpg');
$emailpost = $request->input('username');
}
$post->photo = $postPhoto;
$post->username = $emailpost;
$post->body = $request->input('body');
$post->save();
return redirect ('/ed');
}
Can anyone help me to find it?
My whole controller PostsController.php:
<?php
namespace App\Http\Controllers;
use App\HomeModel;
use MaddHatter\LaravelFullcalendar\Event;
use DateTime;
use Illuminate\Http\Request;
//use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Auth;
//use Auth;
use App\Post;
use App\User;
use App\Role;
use App\Like;
use DB;
use JasonGrimes\Paginator;
class PostsController extends Controller
{
protected $table = 'events'; // you may change this to your name table
public $timestamps = true; // set true if you are using created_at and updated_at
protected $primaryKey = 'id'; // the default is id
protected $fillable = [
'title', 'username', 'body',
];
public function gerpub(){
$query = DB::table('posts')->where('posts.username','=',Auth::user()->email)
->select('posts.*');
$query = $query->get();
return view('gerpub',['posts'=> $query]);
}
public function gerpubele(){
$query = DB::table('posts')->where('posts.username','=',Auth::user()->email)
->select('posts.*');
$query = $query->get();
return view('gerpubele',['posts'=> $query]);
}
public function gerpubpar(){
$query = DB::table('posts')->where('posts.username','=',Auth::user()->email)
->select('posts.*');
$query = $query->get();
return view('gerpubpar',['posts'=> $query]);
}
public function gerpubresp(){
$query = DB::table('posts')->where('posts.username','=',Auth::user()->email)
->select('posts.*');
$query = $query->get();
return view('gerpubresp',['posts'=> $query]);
}
Public function posts() {
$posts = Post::all();
return view('ed', compact('posts'));
}
Public function postsad() {
$posts = Post::all();
return view('pubad', compact('posts'));
}
Public function postsens() {
$posts = Post::all();
return view('pubens', compact('posts'));
}
Public function postsele() {
$posts = Post::all();
return view('pubele', compact('posts'));
}
Public function postspar() {
$posts = Post::all();
return view('pubpar', compact('posts'));
}
Public function postsresp() {
$posts = Post::all();
return view('pubresp', compact('posts'));
}
Public function postsel() {
$posts = Post::all();
return view('edad', compact('posts'));
}
Public function events() {
$query = DB::table('events')->select('events.*');
$itemsPerPage = 2 ;
$currentPage = isset( $_GET['page'] ) && is_numeric( $_GET['page'] ) ? $_GET['page'] : 1;
$urlPattern = '/evenement?page=(:num)';
$totalItems = $query->count();
$donner = $query->offset( ( $currentPage - 1 ) * $itemsPerPage )->limit( $itemsPerPage )->get();
$paginator = new Paginator( $totalItems, $itemsPerPage, $currentPage, $urlPattern );
return view('evenement',['events'=> $donner,'paginator'=> $paginator]);
}
Public function posts1() {
$posts = Post::all();
return view('/home', compact('posts'));
}
Public function post(Post $post) {
// $post = DB::table('posts')->find($id);
return view('post', compact('post'));
}
Public function postad(Post $postad) {
//$postad = DB::table('posts')->find($id);
return view('postad', compact('postad'));
}
Public function postens(Post $postens) {
//$postad = DB::table('posts')->find($id);
return view('postens', compact('postens'));
}
public function store(Request $request) {
$post= new Post();
$post->title = $request->input('title');
if (Auth::check()) {
$postPhoto = Auth::user()->photo;
$emailpost = Auth::user()->email;
} else {
$postPhoto = public_path('/images/def.jpg');
$emailpost = $request->input('username');
}
$post->photo = $postPhoto;
$post->username = $emailpost;
$post->body = $request->input('body');
$post->save();
return redirect ('/ed');
}
public function storead(Request $request)
{
$post= new Post();
$post->title=$request->input('title');
if (Auth::check()) {
$postPhoto = Auth::user()->photo;
$emailpost = Auth::user()->email;
} else{
$postPhoto = public_path('/images/def.jpg');
$emailpost=$request->input('username')
}
$post->photo=$postPhoto
$post->username=$emailpost
$post->body=$request->input('body');
$post->save();
return redirect ('/ed');
}
public function store1(Request $request1)
{
$post1= new Post();
$post1->title=$request1->input('title');
$post1->username=$request1->input('username');
$post->photo=Auth::user()->photo;
$post1->body=$request1->input('body');
$post1->save();
return redirect ('/home');
}
Public function admin() {
$users= User::all();
return view('addmin', compact('users'));
}
Public function admin1() {
$users= User::all();
return view('membre2', compact('users'));
}
public function addRole(Request $request) {
$user = User::where('email', $request['email'])->first();
$user->roles()->detach();
if($request['role_elève'])
{
$user->roles()->attach(Role::where('name', 'Elève')->first());
}
if($request['role_ens'])
{
$user->roles()->attach(Role::where('name', 'Enseignant')->first());
}
if($request['role_parent'])
{
$user->roles()->attach(Role::where('name', 'Parent')->first());
}
if($request['role_admin'])
{
$user->roles()->attach(Role::where('name', 'Admin')->first());
}
return redirect()->back();
}
public function like(Request $request)
{
$like_s = $request->like_s;
$post_id = $request->post_id;
$change_like = 0;
$like = DB::table('likes')
->where('post_id', $post_id)
->where('user_id', Auth::user()->id)
->first();
if(!$like)
{
$new_like = new Like;
$new_like->post_id = $post_id;
$new_like->user_id = Auth::user()->id;
$new_like->like = 1;
$new_like->save();
$is_like = 1;
}
elseif ($like->like == 1)
{
DB::table('likes')
->where('post_id', $post_id)
->where('user_id', Auth::user()->id)
->delete();
$is_like = 0;
}
elseif ($like->like == 0)
{
DB::table('likes')
->where('post_id', $post_id)
->where('user_id', Auth::user()->id)
->update(['like' => 1]);
$is_like = 1;
$change_like = 1;
}
$response = array(
'is_like' => $is_like,
'change_like' => $change_like
);
return response()->json($response, 200);
}
public function dislike(Request $request)
{
$like_s = $request->like_s;
$post_id = $request->post_id;
$change_dislike = 0;
$dislike = DB::table('likes')
->where('post_id', $post_id)
->where('user_id', Auth::user()->id)
->first();
if(!$dislike)
{
$new_like = new Like;
$new_like->post_id = $post_id;
$new_like->user_id = Auth::user()->id;
$new_like->like = 0;
$new_like->save();
$is_dislike = 1;
}
elseif ($dislike->like == 0)
{
DB::table('likes')
->where('post_id', $post_id)
->where('user_id', Auth::user()->id)
->delete();
$is_dislike = 0;
}
elseif ($dislike->like == 1)
{
DB::table('likes')
->where('post_id', $post_id)
->where('user_id', Auth::user()->id)
->update(['like' => 0]);
$is_dislike = 1;
$change_dislike = 1;
}
$response = array(
'is_dislike' => $is_dislike,
'change_dislike' => 1,
);
return response()->json($response, 200);
}
public function statistics() {
$users = DB::table('users')->count();
$posts = DB::table('posts')->count();
$comments = DB::table('comments')->count();
$most_comments = User::withCount('comments')
->orderBy('comments_count', 'desc')
->first();
// dd($most_comments->name);
// $most_likes = User::withCount('likes')
// ->orderBy('likes_count', 'desc')
// ->first();
//dd($most_likes->name);
//$active_user =
return view('/statistics', compact('users', 'posts', 'comments'));
}
public function createresp()
{
return view('create1');
}
public function createeleve()
{
return view('create2');
}
public function create3()
{
return view('create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function isAllDay()
{
return (bool)$this->day;
}
public function store4(Request $request)
{
$time = explode(" - ", $request->input('daterange'));
$event = new HomeModel;
$event->author = Auth::user()->name;
$event->name = $request->input('name');
$event->valid = 1;
$event->title = $request->input('title');
$event->start = $time[0];
$event->end = $time[1];
$event->save();
$request->session()->flash('success', 'The event was successfully saved!');
return redirect('/list');
}
public function storeresp(Request $request)
{
$time = explode(" - ", $request->input('daterange'));
$event = new HomeModel;
$event->author = Auth::user()->name;
$event->valid = 1;
$event->name = $request->input('name');
$event->title = $request->input('title');
$event->start = $time[0];
$event->end = $time[1];
$event->save();
$request->session()->flash('success', 'The event was successfully saved!');
return redirect('/list1');
}
public function storeeleve(Request $request)
{
$time = explode(" - ", $request->input('daterange'));
$event = new HomeModel;
$event->author = Auth::user()->name;
$event->valid = 0;
$event->name = $request->input('name');
$event->title = $request->input('title');
$event->start = $time[0];
$event->end = $time[1];
$event->save();
$request->session()->flash('success', 'The event was successfully saved!');
return redirect('/list2');
}
public function list()
{
$data = [
'page_title' => 'Events',
'event' => HomeModel::orderBy('start')->get(),
];
return view('list', $data);
}
public function listresp()
{
$data = [
'page_title' => 'Events',
'event' => HomeModel::orderBy('start')->get(),
];
return view('list1', $data);
}
public function listeleve()
{
$data = [
'page_title' => 'Events',
'event' => HomeModel::orderBy('start')->get(),
];
return view('list2', $data);
}
public function listele()
{
$data = [
'page_title' => 'Events',
'event' => HomeModel::orderBy('start')->get(),
];
return view('eleveeve', $data);
}
public function edit($id)
{
$event = HomeModel::findOrFail($id);
$event->start;
$event->end;
$data = [
'page_title' => 'Edit '.$event->title,
'event' => $event,
];
return view('edit',$data);
}
public function editresp($id)
{
$event = HomeModel::findOrFail($id);
$event->start;
$event->end;
$data = [
'page_title' => 'Edit '.$event->title,
'event' => $event,
];
return view('edit1',$data);
}
public function editeleve($id)
{
$event = HomeModel::findOrFail($id);
$event->start;
$event->end;
$data = [
'page_title' => 'Edit '.$event->title,
'event' => $event,
];
return view('edit2',$data);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$time = explode(" - ", $request->input('daterange'));
$event = HomeModel::where('id', $id)->first();
$event->name = $request->input('name');
$event->title = $request->input('title');
$event->start = $time[0];
$event->end = $time[1];
$event->save();
return redirect()->back();
}
public function updateresp(Request $request, $id)
{
$time = explode(" - ", $request->input('daterange'));
$event = HomeModel::where('id', $id)->first();
$event->name = $request->input('name');
$event->title = $request->input('title');
$event->start = $time[0];
$event->end = $time[1];
$event->save();
return redirect()->back();
}
public function updateeleve(Request $request, $id)
{
$time = explode(" - ", $request->input('daterange'));
$event = HomeModel::where('id', $id)->first();
$event->name = $request->input('name');
$event->title = $request->input('title');
$event->start = $time[0];
$event->end = $time[1];
$event->save();
return redirect()->back();
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$event = HomeModel::find($id);
$event->delete();
return redirect('/list');
}
public function destroyresp($id)
{
$event = HomeModel::find($id);
$event->delete();
return redirect('/list1');
}
public function destroyeleve($id)
{
$event = HomeModel::find($id);
$event->delete();
return redirect('/list2');
}
}
You forgot to place the semi-colon on your code here $emailpost = $request->input('username') in your storead function. There seems to be many lines missing with semicolon such as the codes below:
$post->photo=$postPhoto
$post->username=$emailpost
In addition, try to practice indenting codes uniformly so that other people who would see the code wouldn't have a hard time reading it.
Try to have a keen eye on simple things like this next time since sometimes the little things are what matters most. Try checking on #PaulT's link :)

what's wrong with my foreach in laravel?

I'm going to pass data in array
but here's the code
public function test($pid){
$row_for_menu = Menu::where('parent_id', '=', $pid)->get();
$menu = array();
foreach ($row_for_menu as $menu_one_by_one){
$menu[] = array('title' => $menu_one_by_one->name, 'nodes' => $this->test($menu_one_by_one->id));
}
dd($menu);
}
public function index(Request $request)
{
$this->test(1);
//$perPage = 25;
//$menu = Menu::paginate($perPage);
//return view('admin.menu.index', compact('menu'));
}
my foreach in first round worked well
but in the end responsed [ dd($menu) ] empty array and error 500 !
my english is not well sorry ;)
try this demo working fine
public function test($pid){
$row_for_menu = Menu::where('parent_id', '=', $pid)->get()->lists('id','name');
$menu = array();
//foreach ($row_for_menu as $menu_one_by_one){
//$menu[] = array('title' => $menu_one_by_one->name, 'nodes' => $this->test($menu_one_by_one->id));
//}
dd($menu);
}
$menu is empty ie. array() , because $row_for_menu is empty
public function test($pid){
$row_for_menu = Menu::where('parent_id', '=', $pid)->get();
$menu = array();
foreach ($row_for_menu as $menu_one_by_one){
$menu[] = array('title' => $menu_one_by_one->name, 'nodes' => $this->test($menu_one_by_one->id));
}
if(count($menu)){
dd($menu);
}
}
public function index(Request $request)
{
$this->test(1);
//$perPage = 25;
//$menu = Menu::paginate($perPage);
//return view('admin.menu.index', compact('menu'));
}
You should define a relation between your menu items on the model instead of recursively looping to build the menu.
// Menu model
public function parent ()
{
return $this->belongsTo(__CLASS__, 'parent_id');
}
public function children ()
{
return $this->hasMany(__CLASS__);
}
Then in your view:
#foreach ($menu->children as $child)
// $child->name
// $child->link
#endoreach
Your controller would simply be:
// assuming you pass a menu id for specific page or similar
public function index(Request $request) {
$menu = Menu::find($request->getAttribute('menu_id'))->toArray();
return view('admin.menu.index', compact('menu'));
}
the test function must return $menu after foreach like this
public function test($pid){
$menus = Menu::where('parent_id', '=', $pid)->get();
$menu = array();
foreach ($menus as $menu_one_by_one){
$menu[] = array('text' => $menu_one_by_one->name, 'nodes' => $this->test($menu_one_by_one->id));
}
return $menu;
}
public function index(Request $request)
{
$x = $this->test(1);
dd($menu)

reuse mysql query code among few methods in same controller - Laravel 5.4

I have few queries which I would like to use it to few methods in the same controller.for example below code:
$lastlogin = User::select('lastlogin')->where('id',Auth::user()->id)->get()->pluck('lastlogin');
$bio = User::where('id',Auth::user()->id)->value('bio');
$photo = User::where('id',Auth::user()->id)->value('photo');
$notifications = Notification::where('created_at','>',$lastlogin)->get();
$status = User::where('id',Auth::user()->id)->value('search_status');
I need to call above query in 4 methods in UserController.
I thought of doing something like:
public function john_doe()
{
$lastlogin = User::select('lastlogin')->where('id',Auth::user()->id)->get()->pluck('lastlogin');
$bio = User::where('id',Auth::user()->id)->value('bio');
$photo = User::where('id',Auth::user()->id)->value('photo');
$notifications = Notification::where('created_at','>',$lastlogin)->get();
$status = User::where('id',Auth::user()->id)->value('search_status');
}
Then
UserController
public abc (){john_doe();}
public def (){john_doe();}
public ghi (){john_doe();}
public jkl (){john_doe();}
But I get an error. How do I do this so when I change the code in one place it reflects everywhere?
Updated question
public function notify()
{
$bio = User::where('id',Auth::user()->id)->value('bio');
$photo = User::where('id',Auth::user()->id)->value('photo');
$friends = Friend::where('user_id',Auth::user()->id)->where('reqs_status',2)->get();
$notifications = Notification::where('created_at','>',Auth::user()->lastlogin)->get();
$status = User::where('id',Auth::user()->id)->value('search_status');
}
public function index()
{
$this->notify();
return view('/users/index',compact('send_requests','accept_rejects','sent_requests','users','bio','photo','friends','status','seeks','filters','notifications'));
}
That is a poorly written code. You get everything except notifications from the logged in user model.
public function fetchData()
{
$user = auth()->user();
$notifications = Notification::where('created_at', '>' , $user->lastlogin)->get();
$data = [
'lastlogin' => $user->lastlogin,
'bio' => $user->bio,
'photo' => $user->photo,
'search_status' => $user->search_status,
'notifications' => $notifications,
];
return (Object)$data;
}
public function test()
{
$data = $this->fetchData();
// $data->lastlogin;
// $data->bio;
// $data->photo;
// $data->search_status;
// $data->notifications;
}
Your UserController could looks like this
class UserController extends BaseController {
public function notify()
{
$array['bio'] = User::where('id',Auth::user()->id)->value('bio');
$array['photo'] = User::where('id',Auth::user()->id)->value('photo');
$array['friends'] = Friend::where('user_id',Auth::user()->id)->where('reqs_status',2)->get();
$array['notifications'] = Notification::where('created_at','>',Auth::user()->lastlogin)->get();
$array['status'] = User::where('id',Auth::user()->id)->value('search_status');
return $array;
}
public function index()
{
return view('/users/index', $this->notify());
}
}

how to filter table data according to Auth::user->id(); in Laravel

I am using this Controller to get permission to users to there projects
public function show($id)
{
if (Permission::where('status', 1)->where('project_id', $id)->exists()) {
// if((Permission::where('status', '=', '1')->first()) && (Permission::where('project_id','=',$id)->first())){
$project = Project::find($id);
$tasks = $this->getTasks($id);
$files = $this->getFiles($id);
$comments = $this->getComments($id);
$collaborators = $this->getCollaborators($id);
$permissions = $this->getPermissions($id);
returnview('collaborators.show')->withProject($project)->withTasks($tasks)->withFiles($files)->withComments($comments)->withCollaborators($collaborators);
}
else if
//return('hi');
(Permission::where('status', 2)->where('project_id', $id)->exists()) {
$project = Project::find($id);
$tasks = $this->getTasks($id);
$files = $this->getFiles($id);
$comments = $this->getComments($id);
$collaborators = $this->getCollaborators($id);
$permissions = $this->getPermissions($id);
return view('collaborators.manager')->withProject($project)->withTasks($tasks)->withFiles($files)->withComments($comments)->withCollaborators($collaborators);
}
My permission model is
<?php
namespace App;
use Auth;
use Illuminate\Database\Eloquent\Model;
class Permission extends Model
{
protected $table = 'permissions';
protected $fillable = ['status','project_id','collaborator_id'];
public function scopeProject($query, $id)
{
return $query->where('project_id', $id);
}
public function scopeProjectp($query, $cid)
{
return $query->where('collaborator_id', $cid);
}
public function scopePermissioneditt($query, $id, $projectId)
{
return $query->where('collaborator_id',$id)->where('project_id',$projectId);//->exists();
}
public function scopeProjectac($query)
{
return $query->where('collaborator_id', Auth::user()->id);
}
/* public static function permission($collaboratorId, $projectId)
{
return Permission::where('collaborator_id', $collaboratorId)->where('project_id', $projectId);
}*/
public function user()
{
return $this->belongsTo(User::class, 'collaborator_id');
}
/* public function project()
{
return $this->belongsToMany('App\Project');
}*/
public function project_collaborator()
{
return $this->belongsToMany('App\Collaboration','collaborator_id');
}
//
}
I need check and access permission table according to current user Auth::user->id; to give permission users.

laravel eloquent : Increments visits column by one for every visits

I have model for my post in my site.My post model has a column that stores visits.when user visit my post should increments by 1.
I exactly knows how to do this, but my problem is when I increment it by one in my model, it's increments by 2!!!!!
I wrote this code in my controller:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->increment('visit');
This is my controller:
public function get_id($id) {
// cat
$cat = Category::join('catrelations', 'catrelations.idcat', '=', 'categories.id')->orderBy('categories.id', 'ASC')->get();
// menu
$nav = Nav::orderBy('index', 'DESC')->get();
$post = Post::find($id);
$setting = Settings::find(1);
$comments = Comment::where('post_id', '=', $id)->orderBy('id', 'asc')->get();
$get_reply = array();
$get_reply_id = array();
//$counter = 0;
//var_dump($comments);
foreach ($comments as $comment) {
$reply = Reply::where('parentId', '=', $comment->id)->get();
if (!$reply->isEmpty()) {
//$arr_reply[$comment->id] = $reply;
//echo $comment->id.' has reply!!!!!<br>';
//$reply_parent_id = $comment->id;
$counter = 0;
foreach ($reply as $replying) {
$get_reply[$comment->id][$counter] = Comment::where('id', '=', $replying->comment_id)->get();
//$comment_reply_id = $replying->comment_id;
//$reply_arr = array();
//$reply_arr[$comment->id] = $comment_reply_id;
//echo 'The reply is: '.$comment_reply_id.'<br>';
foreach ($get_reply[$comment->id][$counter] as $key => $value) {
//($value->text);
$get_reply_id[] = $value->id;
}
$counter++;
}
//$counter++;
}
}
$post_owner_info = User::select('id', 'first_name', 'last_name', 'image', 'desc')->find($post->user_id);
$arr = array();
$arr['comments'] = $comments;
$arr['post'] = $post;
//$arr['reply'] = $reply;
//var_dump($get_reply);
//var_dump($get_reply_id);
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->increment('visit');
return View::make('blogsingle', compact('arr'))->with('setting', $setting)->with('post', $post)->with('nav', $nav)->with('get_reply', $get_reply)->with('get_reply_id', $get_reply_id)->with('cat', $cat)->with('post_owner_info', $post_owner_info);
}
And this is my Post model:
class Post extends Eloquent
{
public function User()
{
return $this->belongsTo('User');
}
public function Categories()
{
return $this->belongstomany('Category');
}
public function comments()
{
return $this->hasMany('Comment');
}
}
you can try like this
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->increment('visit',1);
Try this:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->visit+=1;
$new_post_inst->save();
OR this:
$new_post_inst = Post::where('id','=',$id)->first();
$new_post_inst->update(array('visit' => $new_post_inst->visit+1));
I searched around for a little bit and this could be the favicon icon that is making a second request. How did you declare the favicon? Or maybe you could remove it to try and see if it is indeed the favicon?

Categories