Skip to content

Commit cb49f14

Browse files
committed
edit and add article
1 parent 44b9eeb commit cb49f14

File tree

7 files changed

+175
-7
lines changed

7 files changed

+175
-7
lines changed

app/Http/Controllers/ArticleController.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use Illuminate\Http\Request;
66

77
use App\Articale;
8+
use App\Category;
89
use Facade\FlareClient\Http\Response;
10+
use Facade\FlareClient\View;
911
use Illuminate\Support\Facades\Auth;
1012

1113
class ArticleController extends Controller
@@ -31,7 +33,7 @@ public function index()
3133
*/
3234
public function create()
3335
{
34-
//
36+
return view('articles.addArticle');
3537
}
3638

3739
/**
@@ -42,21 +44,20 @@ public function create()
4244
*/
4345
public function store(Request $request)
4446
{
45-
$request->validate([
47+
$request->validate([
4648
'name' => ['required', 'max:255'],
4749
'category_id' => ['required'],
4850
'content' => ['required'],
4951
]);
50-
52+
5153
$article = new Articale;
5254
$article->name = $request->name;
5355
$article->content = $request->content;
5456
$article->category_id = $request->category_id;
5557
$article->user_id = Auth::id();
5658
$article->save();
57-
return response()->json([
58-
'msg' => 'article created successfully'
59-
]);
59+
60+
return redirect('addArticle');
6061
}
6162

6263
/**
@@ -81,6 +82,8 @@ public function show($id)
8182
*/
8283
public function edit($id)
8384
{
85+
// dd(Articale::find($id));
86+
return view('articles.editArticle',['article' => Articale::find($id)]);
8487
}
8588

8689
/**
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace App\Http\View\Composers;
4+
5+
use App\Category;
6+
use App\Repositories\UserRepository;
7+
use Illuminate\View\View;
8+
9+
class CategoriesComposer
10+
{
11+
/**
12+
* The user repository implementation.
13+
*
14+
15+
*/
16+
protected $categories;
17+
18+
/**
19+
* Create a new profile composer.
20+
*
21+
* @param UserRepository $users
22+
* @return void
23+
*/
24+
public function __construct()
25+
{
26+
// Dependencies automatically resolved by service container...
27+
$this->categories = Category::all();
28+
}
29+
30+
/**
31+
* Bind data to the view.
32+
*
33+
* @param View $view
34+
* @return void
35+
*/
36+
public function compose(View $view)
37+
{
38+
$view->with('categories', $this->categories);
39+
}
40+
}

app/Providers/AppServiceProvider.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Providers;
44

5+
6+
use Illuminate\Support\Facades\View;
57
use Illuminate\Support\ServiceProvider;
68

79
class AppServiceProvider extends ServiceProvider
@@ -23,6 +25,9 @@ public function register()
2325
*/
2426
public function boot()
2527
{
26-
//
28+
View::composer(
29+
['articles.addArticle', 'articles.editArticle'],
30+
'App\Http\View\Composers\CategoriesComposer'
31+
);
2732
}
2833
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
@include('partials.head')
6+
</head>
7+
8+
<body>
9+
<div class="container">
10+
<div class="row d-flex flex-column justify-content-center">
11+
<div class="offset-md-4 col-md-4">
12+
<form action=" {!! action('ArticleController@store') !!}" method="POST">
13+
@csrf
14+
<div class="form-group">
15+
<label for="name">Name</label>
16+
<input type="text" name="name" id="name" class="form-control" placeholder="Enter article name" aria-describedby="helpId">
17+
@error('name')
18+
<small id="helpId" class="alert-danger form-control mt-1">{{ $message }}</small>
19+
@enderror
20+
21+
</div>
22+
<div class="form-group">
23+
<label for="content">Description</label>
24+
<textarea name="content" id="content" cols="30" class="form-control" rows="10"></textarea>
25+
@error('content')
26+
<small id="helpId" class="alert-danger form-control mt-1">{{ $message }}</small>
27+
@enderror
28+
</div>
29+
<div class="form-group">
30+
<label for="category"></label>
31+
<select class="form-control" name="category_id" id="category">
32+
@foreach ($categories as $category)
33+
<option value={{$category->id }}>{{$category->name }} </option>
34+
@endforeach
35+
</select>
36+
</div>
37+
38+
<button type="submit" class="btn btn-primary form-control" onclick="clickAdd(e)">Add Article</button>
39+
</form>
40+
41+
</div>
42+
</div>
43+
</div>
44+
<script>
45+
// function clickAdd(e){
46+
// e.preventDefault();
47+
48+
// }
49+
</script>
50+
</body>
51+
52+
</html>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
@include('partials.head')
6+
</head>
7+
8+
<body>
9+
<div class="container">
10+
<div class="row d-flex flex-column justify-content-center">
11+
<div class="offset-md-4 col-md-4">
12+
<form action=" {!! action('ArticleController@update',$article->id) !!}" method="POST">
13+
@method('PUT')
14+
@csrf
15+
<div class="form-group">
16+
<label for="name">Name</label>
17+
<input type="text" name="name" id="name" class="form-control" placeholder="Enter article name" value={{$article->name}} aria-describedby="helpId">
18+
@error('name')
19+
<small id="helpId" class="alert-danger form-control mt-1">{{ $message }}</small>
20+
@enderror
21+
22+
</div>
23+
<div class="form-group">
24+
<label for="content">Description</label>
25+
<textarea name="content" id="content" cols="30" class="form-control" rows="10" value={{$article->content}}>{{$article->content}}</textarea>
26+
@error('content')
27+
<small id="helpId" class="alert-danger form-control mt-1">{{ $message }}</small>
28+
@enderror
29+
</div>
30+
<div class="form-group">
31+
<label for="category"></label>
32+
<select class="form-control" name="category_id" id="category">
33+
@foreach ($categories as $category)
34+
@if ($category->id == $article->category_id )
35+
<option value={{$category->id }} selected>{{$category->name }} </option>
36+
@else
37+
<option value={{$category->id }}>{{$category->name }} </option>
38+
@endif
39+
@endforeach
40+
</select>
41+
</div>
42+
43+
<button type="submit" class="btn btn-primary form-control" onclick="clickAdd(e)">Add Article</button>
44+
</form>
45+
46+
</div>
47+
</div>
48+
</div>
49+
<script>
50+
51+
</script>
52+
</body>
53+
54+
</html>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<meta charset="utf-8">
2+
3+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
4+
5+
<meta name="description" content="">
6+
7+
<meta name="author" content="">
8+
<!-- Bootstrap core CSS -->
9+
10+
11+
<script src="{{ asset('js/app.js') }}" defer></script>
12+
<link href="{{ asset('css/app.css') }}" rel="stylesheet">

routes/web.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
});
1919
// Route::resource('articles', 'ArticleController');
2020
Route::get('articles', 'ArticleController@index');
21+
Route::get('addArticle', 'ArticleController@create');
2122
Route::post('articles', 'ArticleController@store')->middleware('auth');
2223
Route::put('articles/{id}', 'ArticleController@update')->middleware('auth');
24+
Route::get('articles/{id}', 'ArticleController@edit')->middleware('auth');
2325
Route::delete('articles/{id}', 'ArticleController@destroy')->middleware('auth');
2426
Route::get('categories', 'CategoryController@index');
2527
Route::get('categories/{id}', 'CategoryController@show');

0 commit comments

Comments
 (0)